Why my button didn't worked?

When I run Arduino, sentences automatically go over the screen and the button does not run.
#include <LiquidCrystal_I2C.h>
#include <Button.h>
#include <Adafruit_MLX90614.h>
#include<Wire.h>

#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); //I2C LCD 媛앹껜 ?좎뼵

Button button1(8);
Button button2(9);
Button button3(10);

int firstRun = 0;
int yb = 0; //yes_button
int nb = 0; //no button
int cb = 0; //clear button
int yb_now = 0;
int nb_now = 0;
int yb_last = 0;
int nb_last = 0;
int numsT;
int cnt;
String strTemp = String("");

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void readTmp() {
lcd.clear();

for (int i = 0;i < 6;i++) {
    strTemp += (int)(mlx.readObjectTempC()); // 二쇰? ?⑤룄瑜??쎌뒿?덈떎.
    lcd.setCursor(2, 0); // 2??0??
    lcd.print("Temperature"); // Temperature ?쒖떆
    lcd.setCursor(6, 1); // 6??1??
    lcd.print(strTemp); // 痢≪젙 ?⑤룄 ?쒖떆
    lcd.setCursor(8, 1); // 8??1??
    lcd.print("'C"); // ?⑤룄 ?⑥쐞 ?쒖떆 
    delay(1000);
    lcd.clear();

    i++;
}

}

void buttonState() {
delay(100);
yb = button1.pressed();
nb = button2.pressed();
nb = button3.pressed();

}

void smile() {
lcd.clear();
lcd.setCursor(0, 3);
lcd.print("^ ^");
lcd.setCursor(1, 6);
lcd.print("---");
}

void sad(){
lcd.clear();
lcd.setCursor(0, 3);
lcd.print("* *");
lcd.setCursor(1, 6);
lcd.print("---");
delay(5000);
lcd.clear();
}

void tmpBefore() {
lcd.setCursor(0, 0); // 1踰덉쨪
lcd.print("first, Measured");
lcd.setCursor(0, 1); // 1踰덉쨪
lcd.print("body tempreture");
delay(2000); //2珥덇컙 ?€湲?
lcd.clear(); //LCD紐⑤뱺 ?댁슜 吏€?€
}

boolean debounce(boolean last, int BUTTON) {
boolean current = digitalRead(BUTTON);
if (last != current) {
delay(5);
current = digitalRead(BUTTON);
}
return current;
}

void painScale() {
yb_now = debounce(yb_last, yb);
if (yb_last == 0 && yb_now == 1) cnt++;
yb_last = yb_now;

nb_now = debounce(nb_last, nb);
if (nb_last == 0 && nb_now == 1) cnt--;
    nb_last = nb_now;
lcd.setCursor(0, 0);
lcd.print("your pain Scale");
lcd.setCursor(0, 1);
lcd.print(cnt);
lcd.print("                ");
    if (cb = 1) {
        firstRun = 4;
    }

}

void setup()
{
Serial.begin(9600);

lcd.begin();
lcd.backlight();        //諛깅씪?댄듃 耳쒓린 

mlx.begin();  // mlx 紐⑤뱢???쎄린 ?쒖옉.
delay(500);
lcd.clear();

}

void loop() {
buttonState();
if (firstRun == 0)
{
lcd.clear();
firstRun = 1;

    lcd.setCursor(0, 0);     // 1踰덉쨪
    lcd.print("hello im prim");
    delay(2000);      //2珥덇컙 ?€湲?
    lcd.clear();      //LCD紐⑤뱺 ?댁슜 吏€?€


    lcd.setCursor(0, 0);     //1踰덉쨪
    lcd.print("you want checkup");
    lcd.setCursor(0, 1);     //2踰덉쨪
    lcd.print("press button"); //12
    delay(2000);
    lcd.clear();
    smile();
}

if (yb == 1 || nb == 1) {
    if (firstRun == 1) {
        lcd.print("close your hand");
        delay(1000);
        lcd.clear();
        readTmp();

        lcd.clear();
        lcd.setCursor(0, 0);     // 1踰덉쨪
        lcd.print("your tmp is "); //?뱀떊???⑤룄??__
        lcd.setCursor(13, 0);
        lcd.print(strTemp);
        lcd.setCursor(15, 0); // 15??0??
        lcd.print("'C");
        delay(3000);
        lcd.clear();      //LCD紐⑤뱺 ?댁슜 吏€?€  

        numsT = strTemp.toInt();
        if (numsT > 39) {
            firstRun = 2;

            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("tmp is 2 high!");
            delay(1000);
            lcd.clear();

            lcd.setCursor(0, 0);
            lcd.print("if U need doc");
            lcd.setCursor(0, 1);
            lcd.print("press yes button");

            
        }

        firstRun = 3;
    }
}

    if (yb == 1) {
        if (firstRun == 2) {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Im calling a doc");
            lcd.setCursor(0, 1);
            lcd.print("wait a minute...");

            sad();
            delay(5000);

            firstRun = 0; // go to another Patient
        }
    }

    if (nb == 1) {
        if (firstRun == 2) {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("okay, whenever U");
            lcd.setCursor(0, 1);
            lcd.print("need doc,call 4U");
            

            firstRun = 3; // Back to firstRun=3
        }
    }

    if (firstRun == 3) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Pain scale");
        delay(2000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Press the button");
        lcd.setCursor(0, 1);
        lcd.print("between 1-10");
        delay(3000);
        lcd.clear();

        painScale();
    }

    if(firstRun==4){
        lcd.clear();

        if (cnt < 4) {
            lcd.print("Good!");

            firstRun = 5;
        }

        else if (cnt > 7){
            firstRun=2;

            lcd.setCursor(0, 0);
            lcd.print("R U okay?");
            delay(1000);
            lcd.clear();

            lcd.setCursor(0, 0);
            lcd.print("if U need doc");
            lcd.setCursor(0, 1);
            lcd.print("press yes button");
        }

        else {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Need painkiller?");
            if (yb == 1) {
                lcd.clear();
                lcd.setCursor(0, 0);
                lcd.print("okay");
                lcd.setCursor(0, 1);
                lcd.print("I'll tell doctor");
            }

            if (nb == 1) {
                firstRun = 5;
            }

        }

        
    }

    if (firstRun == 5) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("All check up");
    lcd.setCursor(0, 1);
    lcd.print("is done");
    delay(2000);
    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("If U need help");
    lcd.setCursor(0, 1);
    lcd.print("whenever call us");
    delay(2000);
    lcd.clear();

    smile();
    delay(3000);
    lcd.clear();

    firstRun = 0;

}


}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You seem to be using a mixture of reading inputs using the Button library and direct digitalRead()s. Why is that ?

I am particularly suspicious of the debounce() function when you call it using a parameter like yb as the pin number which has previously been used to hold the state of a button input. Did you mean to use the same variable for 2 completely different purposes ?

Hello
Your sketch looks like spaghetti code mixed together.
What are your expections?
Have a nice day and enjoy coding in C++.

Will it be okay to separate variables?

please format your code using code tags before asking further questions

should button3 set "cb" instead of "nb"

if your having problems with buttons, why not write some simpler code to verify their operation and your understanding

but the logic is confusing, hard to follow. it appears that firstRun may be a state variable that determines a mode of operation presumably changed by button presses.

instead, the code has multiple instances where it check for particular button pressed and executes some action depending on firstRun.

a more conventional approach is to have a single block of code for each state (value of firstRun) that handles each button press

BTW, click the "</>" icon for code tags

Hi, @ttt0802
Welcome to the forum.

Can you tell us what your code is supposed to do?
Did you write it yourself or from examples and tutorials?

Have you written it in stages so that you have code that works before adding the next part?

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

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