Im trying to make a feeder and im not quite sure how to code my buttons the way i want it. The mode button is suppose to switch between one time and daily but it only switches to daily and doesnt go back to one time. The hour up button only goes to 2 hrs and the down goes to 1hr. and my dispense button doesnt work pls help guys im still pretty new to this arduino stuff and coding
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Servo mainServo;
Servo servo1;
Servo servo2;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int position1 = 45;
int position2 = 135;
int startPosition = 180;
const int modeButton = 2;
const int hourButtonUp = 3;
const int hourButtonDown = 4;
const int confirmButton = 5;
const int startButton = 6;
bool modeSelected = false;
bool hoursSelected = false;
bool timeSelected = false;
bool dailyMode = false;
int countdownHours = 1;
void setup() {
Serial.begin(9600);
mainServo.attach(9);
servo1.attach(10);
servo2.attach(11);
lcd.init();
lcd.backlight();
pinMode(modeButton, INPUT);
pinMode(hourButtonUp, INPUT);
pinMode(hourButtonDown, INPUT);
pinMode(confirmButton, INPUT);
pinMode(startButton, INPUT);
displayModeSelection();
}
void loop() {
if (!modeSelected) {
if (buttonPressed(modeButton)) {
dailyMode = !dailyMode;
displayModeSelection();
delay(300);
}
if (buttonPressed(confirmButton)) {
modeSelected = true;
displayTimeSelection();
}
} else if (!hoursSelected) {
if (buttonPressed(hourButtonUp) && countdownHours < 10) {
countdownHours++;
displayTimeSelection();
delay(300);
}
if (buttonPressed(hourButtonDown) && countdownHours > 1) {
countdownHours--;
displayTimeSelection();
delay(300);
}
if (buttonPressed(confirmButton)) {
hoursSelected = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Start");
}
} else if (!timeSelected) {
if (buttonPressed(startButton)) {
timeSelected = true;
startCountdown(countdownHours);
}
}
}
void displayModeSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Mode:");
lcd.setCursor(0, 1);
lcd.print(dailyMode ? "Daily" : "One-Time");
}
void displayTimeSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pick hours:");
lcd.setCursor(0, 1);
lcd.print(countdownHours);
lcd.print(" hrs");
}
void startCountdown(int hours) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown: ");
lcd.setCursor(0, 1);
for (int i = hours; i > 0; i--) {
lcd.setCursor(0, 1);
lcd.print(i);
lcd.print(" hrs left ");
delay(3600000); // 1 hour delay
}
triggerServo();
}
void triggerServo() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing...");
mainServo.write(position1);
delay(1000);
mainServo.write(startPosition);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Done!");
}
bool buttonPressed(int button) {
if (digitalRead(button) == LOW) {
delay(50);
if (digitalRead(button) == LOW) {
while (digitalRead(button) == LOW);
return true;
}
}
return false;
}
