My Lcd wont turn on and if so my pushbuttons dont do any affect

here is the code

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

Servo mainServo;  
Servo servo1;     
Servo servo2;   

// Ensure the I2C address is correct
LiquidCrystal_I2C lcd(0x27, 16, 2);  

int position1 = 45;
int position2 = 135;
int startPosition = 180;

int feedHour = 12;
int feedMinute = 0;
int hourButtonUp = 2;
int hourButtonDown = 3;
int minuteButtonUp = 4;
int minuteButtonDown = 5;
int modeButton = 6;
bool dailyMode = false;
unsigned long previousMillis = 0;

void setup() {
    Serial.begin(9600); 
    mainServo.attach(9);
    servo1.attach(10);    
    servo2.attach(11);   
    
    // Initialize the LCD properly
    lcd.begin(16,2); // Fix: Correct initialization for I2C LCD
    lcd.backlight(); // Ensure backlight is turned on

    pinMode(hourButtonUp, INPUT_PULLUP);
    pinMode(hourButtonDown, INPUT_PULLUP);
    pinMode(minuteButtonUp, INPUT_PULLUP);
    pinMode(minuteButtonDown, INPUT_PULLUP);
    pinMode(modeButton, INPUT_PULLUP);
    
    updateLCD();
    mainServo.write(startPosition);  
    servo1.write(0); 
    servo2.write(0); 
    delay(1000);  
}

void loop() {
    unsigned long currentMillis = millis();
    static int currentHour = 0, currentMinute = 0;
    static unsigned long lastUpdateTime = 0;

    if (currentMillis - lastUpdateTime >= 60000) { 
        lastUpdateTime = currentMillis;
        currentMinute++;
        if (currentMinute >= 60) {
            currentMinute = 0;
            currentHour++;
            if (currentHour >= 24) {
                currentHour = 0;
            }
        }
    }

    if (digitalRead(hourButtonUp) == LOW) {
        feedHour = (feedHour + 1) % 24; 
        updateLCD();
        delay(300);
    }
    if (digitalRead(hourButtonDown) == LOW) {
        feedHour = (feedHour - 1 + 24) % 24; 
        updateLCD();
        delay(300);
    }
    if (digitalRead(minuteButtonUp) == LOW) {
        feedMinute = (feedMinute + 1) % 60;  
        updateLCD();
        delay(300);
    }
    if (digitalRead(minuteButtonDown) == LOW) {
        feedMinute = (feedMinute - 1 + 60) % 60;  
        updateLCD();
        delay(300);
    }
    if (digitalRead(modeButton) == LOW) {
        dailyMode = !dailyMode;
        updateLCD();
        delay(500);
    }

    if (currentHour == feedHour && currentMinute == feedMinute) {
        lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print("Feeding now...");
        
        mainServo.write(position1);
        delay(2000);
        servo1.write(90);
        delay(2000);
        servo1.write(0);
        delay(1000);

        mainServo.write(position2);
        delay(2000);
        servo2.write(90);
        delay(2000);
        servo2.write(0);
        delay(1000);

        mainServo.write(startPosition);
        delay(2000);
        updateLCD();

        if (!dailyMode) {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("One-time done.");
            while (1); 
        }
    }
}

void updateLCD() {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Feed Time: ");
    lcd.print(feedHour);
    lcd.print(":");
    if (feedMinute < 10) lcd.print("0");
    lcd.print(feedMinute);
    lcd.setCursor(0, 1);
    lcd.print("Mode: ");
    lcd.print(dailyMode ? "Daily" : "One-time");
}

Please show how it is all wired. Are you using hardware to debounce your buttons? If not, you may want to add debouncing logic to your code too.

here you go although im not quite sure though if its all correct im still pretty new to arduino and this kind of stuff

Ok, let’s start with your power situation. You have the Arduino, 3 servos and a 16x2 display all running off of your usb cable, that is not good.

Your servos alone should have their own power supply about 6 volts at 2amps, should be sufficient. While you can run the Arduino off the usb, I would advise you to use a second power supply for the Arduino and the LCD. The lcd power should be coming from the power supply and NOT the 5volt pin on the Arduino.

Also to keep in mind if you have separate power supplies, that you connect the ground from one to the ground on the other power supply. This would be known as a “Common Ground” and is vital for everything to work correctly.

Next your buttons, I noticed you are using pull-up resistors as well as have the pins in code be “pull-ups” too. You need only choose hardware pull-ups OR software pull-ups. While having both wont hurt, you should really just have the one.

Please sort these out first and then revise your diagram. Once that is done, we can focus on the code though I think your issue is power related and may very well not be code related… though that 60000 you have in your code does look iffy to me. It should be 60000UL, but based on the rest of the code, it may not be needed at all.

Is it okay if you can simplify what you mean? I'm sorry, I don't quite get Arduino and stuff I'm really new to this i was just tasked to do this for a project and I'm not stressing not quite sure what to do. If its alright with you thanks alot man

You need to fix how everything is getting power, so start there.

The usb cable going into the Arduino doesn’t have enough current to power anything but the Arduino itself. To fix this, you need another method to get power like batteries. Depending on the batteries you use, will determine how long (if at all) your project will last. Your best bet would be to use something like an old wall charger or a battery pack, one that can output 6volts at roughly 2000ma.

I like using these here for my projects.

But how do i it in the tinkercad? should I get one 6V battery ang connect it to a power bus of a breadboard and link the servos to the breadboard or like how do i connect them all together? Sorry again dude im very new to this and im not that also good in english so sorry really

Tinkercad is outside my scope of knowledge, I have never used it. I don’t trust simulations, I prefer actual hardware.

Maybe someone else can help you with Tinkercad.

oh alright thanks for the help anyways dude

Hi, @plumlich
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Can I suggest you remove all the hardware, except the display.
Forget your code for the moment.
Write some test code just for the display, lets get each of your hardware items working on their won first.
Your display is the first to be worked on.

The display library your are using should have some working examples for you to try.
"Hello World" display is one of them.

You also have input pullup resistors on the protoboard, BUT you also have then turned ON in the UNO, you don't need both.

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

@TomGeorge Hi
Thanks for welcoming me

I dont have any hand drawn but i do have my sketch online on tinkercad

It's not the best but im still learning. My problem right now is that the lcd keeps flickering "How many hours?" and my push buttons dont do anything. Im trying to make like an automatic feeder where the user inputs the time using the buttons when it will dispense. Im really new to arduino and i hope you can help me thanks :grinning_face_with_smiling_eyes:

Remove
lcd.begin(16,2);
Replace with
lcd.init();

Can't tell from your picture but make sure SCL->SCL and SDA->SDA


Have fun!

1 Like

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