Pushbutton is weird

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;
}

Is with your tinkercad code from your other post?

i changed some stuff from the other one but im still having a hard time it still wont work :smiling_face_with_tear:

You should not have created a second topic.
You need to state that is for tinkercad and show your schematic again.

Oh okay my bad i didnt know sorry anyways here you go

What is the purpose of the confirm button?
How is it supposed to work?

its like a select button basically if the user wants one time he switches to one time and the selects it using the confirm button. I just named it like that for now.

do you have a resistor to pull the signal down? do you have schematic? and it seems like you don't have a debounce delay

Well for one thing you changed the way your buttons work, Before they would go LOW when you pushed, now they will go HIGH. So you need to change the buttonpressed function.

1 Like


yeah here you go not really sure though how to add a debounce delay

you can add delay(50) or delay(100) on the end of the code

You already have it
See post #9

Don't show the same diagram twice unless you have made changes.
Have you changed anything?

For the mode button, add delay(300) after toggling to debounce it. For the hour buttons, check the range properly with countdownHours >= 1 and countdownHours <= 10. If the dispense button isn’t working, try adding Serial.print statements to debug.

You already have debounce don't add more delays in your code.

It seems that the push buttons have a external pull-down resistor. This means when the buttons are not pushed the button input wil read low and when pushed high.
As far as i can see in your sketch (i'm a semi-beginner myself) there needs to happen something if you push a button:

if (buttonPressed(modeButton)) {
            dailyMode = !dailyMode;
            displayModeSelection();
            delay(300);

but the pushbutton function:

bool buttonPressed(int button) {
    if (digitalRead(button) == LOW) {
        delay(50);
        if (digitalRead(button) == LOW) {
            while (digitalRead(button) == LOW);
            return true;
        }
    }
    return false;
}

..will return true when the button is NOT pushed:

while (digitalRead(button) == LOW);
            return true

See post #9 about how you changed the pushbuttons.

Sorry i didn't notice your post. Apologies

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.