Hey everyone i want to make my lcd I2c display show something when i push the button but it is not working

#include <LiquidCrystal_I2C.h>

#define LCD_ADDR 0x27

#define LCD_COLUMNS 16

#define LCD_ROWS 2

int button = 4;

LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLUMNS, LCD_ROWS);

void setup() {

// put your setup code here, to run once:

lcd.begin(16,2);

lcd.init();

lcd.backlight();

pinMode(button, INPUT);

lcd.setCursor(5,0);

lcd.print("hello");

lcd.setCursor(0,1);

lcd.print("The best kebron");

int buttonstate = digitalRead(button);

if(buttonstate == HIGH){

lcd.clear();

lcd.print("whats your name");

}

}

void loop() {}

1 Like

Please try to edit and fix your post. Something is not quite right with the code tags.

All your code seems to be in setup(). There is no code in loop().

Do you understand the purpose of setup() and loop() and which code to put in which function?

I suspect your code might work as you expected if you hold down your pushbutton while pressing and releasing the reset button.

thia is my first time into coding so i dont know but here is the link of my project

I will reply again when you have fixed the code tags and answered my question.

So it is time to learn

it did work when i move only the if state into loop but why is that

Did you read the post #6 ?

sorry buddy i replay before i read it but still does not answer my question even if i put it on the setup() should't it work even for once

This happens because the sketch in the controller runs very quickly. The controller doesn't wait for you to press a button. The entire execution of the setup function takes a few thousandths of a second; you simply do not have time to press the button.

The setup function is not the place to place button work.

dame that make sense
hey since this is my first time could u recommend me a way to learn about arduino and the codes that come with it
maybe like a good website

1. Use pen-pencil-ruler and make a connection on your exercise book among Arduino UNO, I2CLCD, and PushButton (Fig-1).


Figure-1:

2. Check that your physical connection among K1, I2CLCD (I2C bus drievn LCD), and UNO agrees with Fig-1. The Rip (internal pull-resistor) is within the MCU.

4. Connect your UNO with PC using USB cable.
5. Open IDE and observe that the following two blank functions have appeared.

void setup()
{

}

void loop()
{

}

6. The tasks that will be executed only once or for a limited number of times will be put in the setup() function, and these are:
(1) Intialize Serial Port so that some message can be shown on the OutputBox of Serial Monitor.

Serial.begin(9600);

(2) Direction of DPin-4 as input with internal pull-up resistor enaled/connected.

pinMode(4, INPUT_PULLUP)

(3) Initialize I2LCD so that some message can be shown on it.

lcd.begin();
or
lcd.init();

(4) Place the following line (called header file) at the top of setup() function. This file contains the meanings of various symbolic names and codes for the functions like licd.begin().

#include<LiquidCrystal_I2C.h>

(5) Place the following line (called creation of lcd object) underneath the line of Step-(4).

LiquidCrystal_I2C lcd(0x27, 16, 2);    //if does not work, then chnage 0x27 to 0x3F

(6) Check again and again that K1 is closed and then show the message Hello on the Top Line of LCD.

while digitalRead(4) != LOW)
{
    ;  //check and wait until K1 is closed
}
lcd.setCursor(7, 0); // set cursor at the middle of Top Line
lcd.print("Hello")    //show message on LCD 

7. The tasks that will be executed again and again will be put in the loop() function.
(1) After showing Hello message on the LCD, keep blinking the onboard LED (L) of the Arduino UNO Board at 1-sec interval. (put this code: pinMode(13, OUTPUT); in the setup() function.)

digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);

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