0% found this document useful (0 votes)
12 views3 pages

Automatic Food Dispenser Code

This document contains an Arduino sketch for an automatic food dispenser using an LCD and a servo motor. It allows users to set a dispensing interval using buttons for hours, minutes, and seconds, and displays a countdown until the next dispensing. The servo moves to dispense food and returns to the closed position after a set duration.

Uploaded by

smitpatel76207
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Automatic Food Dispenser Code

This document contains an Arduino sketch for an automatic food dispenser using an LCD and a servo motor. It allows users to set a dispensing interval using buttons for hours, minutes, and seconds, and displays a countdown until the next dispensing. The servo moves to dispense food and returns to the closed position after a set duration.

Uploaded by

smitpatel76207
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);


Servo myServo;

// Button pins
const int btnHour = 2;
const int btnMin = 3;
const int btnSec = 4;
const int btnSet = 5;

int setHour = 0, setMin = 0, setSec = 0;


unsigned long intervalSeconds = 0;
unsigned long lastDispenseTime = 0;
bool intervalSet = false;

void setup() {
[Link](16,2);
[Link]();
[Link](9);
[Link](0); // Closed

pinMode(btnHour, INPUT_PULLUP);
pinMode(btnMin, INPUT_PULLUP);
pinMode(btnSec, INPUT_PULLUP);
pinMode(btnSet, INPUT_PULLUP);

[Link](0, 0);
[Link]("Automatic Food");
[Link](0, 1);
[Link](" Dispenser");
delay(2000);
[Link]();
}

void loop() {
if (!intervalSet) {
if (digitalRead(btnHour) == LOW) {
delay(200);
setHour = (setHour + 1) % 24;
}
if (digitalRead(btnMin) == LOW) {
delay(200);
setMin = (setMin + 1) % 60;
}
if (digitalRead(btnSec) == LOW) {
delay(200);
setSec = (setSec + 1) % 60;
}
if (digitalRead(btnSet) == LOW) {
delay(200);
intervalSeconds = setHour * 3600UL + setMin * 60UL + setSec;
if (intervalSeconds == 0) intervalSeconds = 60; // avoid 0
interval
lastDispenseTime = millis() / 1000;
intervalSet = true;
[Link]();
}

[Link](0, 0);
[Link]("Set Time:");
[Link](0, 1);
[Link](formatTime(setHour) + ":" + formatTime(setMin) + ":" +
formatTime(setSec));
return;
}

unsigned long currentSeconds = millis() / 1000;


unsigned long elapsed = currentSeconds - lastDispenseTime;

if (elapsed >= intervalSeconds) {


// Dispense food now
dispenseFood();
lastDispenseTime = millis() / 1000; // Restart timer immediately
return; // Skip countdown update this cycle
}

// Show countdown
unsigned long remaining = intervalSeconds - elapsed;
int remH = remaining / 3600;
int remM = (remaining % 3600) / 60;
int remS = remaining % 60;

[Link](0, 0);
[Link]("Next in:");
[Link](0, 1);
[Link](formatTime(remH) + ":" + formatTime(remM) + ":" +
formatTime(remS) + " ");
}

// Dispensing behavior
void dispenseFood() {
// Move servo 0 to 60
[Link]();
[Link](0, 0);
[Link]("Dispensing...");
[Link](60);
delay(3000);

// Move servo back 60 to 0


[Link](0);
delay(1000);

[Link]();
}

String formatTime(int val) {


return (val < 10 ? "0" : "") + String(val);
}

You might also like