Need help to atleast understand this issue and resolve it

Hi im trying to code my own game that is a LED chaser game where i have 5 led in a row, two button inputs for 2 players and a lcd 16x2 non i^2c. I have started this project a few weeks ago and couldnt make it work so now im experiementing with some parts of what ill be using like the lcd. Now ive written some code when the board is powered the lcd will display "Game name" (i know very creative) and an if statement when either on of the two buttons are pulled high it has to change to "Game started" and the else statement look kinda unnecessary the more i look at it. The problem it just skips ahead of the if statement from my understanding. Any help? Note: Im kinda new to programming maybe not the basics but with statements and stuff like that.


#include <LiquidCrystal.h>
int button1 = 4;
int button2 = 2;

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);


void setup()
{
 lcd.begin(16,2);
 lcd.setCursor(1,1);
}
 void loop()
{
 lcd.print("Game name");
if(digitalRead(button1 == HIGH)||(button2 == HIGH))
 {
   lcd.clear();
   lcd.setCursor(0,1);
   lcd.print("Game started");
   }
   else
  {  
    lcd.print("Start failed");
  }//closes else statement

  }//code end

Welcome to the forum

What is keeping the inputs LOW when the buttons are not pressed ? I guess that they are floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing

So, what to do ?
To avoid extra components, set the pinMode()s of the 2 inputs to INPUT_PULLUP to turn on the internal pullup resistors that will keep the pins HIGH until something happens.

Change the wiring to take the inputs LOW when the button is pressed and change the program logic to test for LOW to indicate that a button has become pressed

And one other thing. This is very wrong

if(digitalRead(button1 == HIGH)||(button2 == HIGH))

It should be

if(digitalRead(button1) == HIGH || digitalRead(button2) == HIGH)

but you are going to change that to test for LOW if you follow my advice above

why will button go high? how you connected them? show.
second thing: you have no idea how to handle a LCD. watch 5 videos and 5 tutorials first before try it self.

Post a schematic showing how you have wired it, from the text it is confusing.

That's an interesting definition of "not the basics"

but with statemens and stuff like that.
What is more basic than "statements"?
The minimum to change is proper if-conditions

You should use ctrl-T for autoformatting your code

You want to read in the logic state of the IO-pins to which you have the buttons connected
you coded

(digitalRead(button1 == HIGH)

which means read in the state of the button with IO-number "button1 == HIGH"
as button1 is a 4 you read in
digitalRead (4 == 1) which of course is false and results in zero
so all in all you do
digitalRead (0)
What you really want to do is
reading in the IO-pin button1
which is done this way
digitalRead(button1) and then compare the result of the reading with HIGH
digitalRead(button1) == HIGH

if ( digitalRead(button1)     ==     HIGH  ) {
#include <LiquidCrystal.h>
int button1 = 4;
int button2 = 2;

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);


void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(1, 1);
}

void loop()
{
  lcd.print("Game name");
  //if (digitalRead(button1 == HIGH) || (button2 == HIGH))
  if (digitalRead(button1)  ==  HIGH)  ||  digitalRead(button2)  ==  HIGH)
  {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Game started");
  }
  else
  {
    lcd.print("Start failed");
  }//closes else statement

}//code end

best regards Stefan

Hello @frog_bot,
Here are suggestions and stuff like that for your game. Have fun with it!

#include <LiquidCrystal.h>
int button1 = 4; // button assigned a pin number
int button2 = 2; // button assigned a pin number

int RS = 13, EN = 12, D4 = 11, D5 = 10, D6 = 9, D7 = 8; // assign names to pins
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); // create LiquidCrystal object to control

void setup()
{
  lcd.begin(16, 2); // start the object
  lcd.clear(); // clear the object
  lcd.setCursor(4, 0); // place the cursor on column 4, row 0
  lcd.print("Game name"); // moved this line from loop() to print it only once

  // button wired to ground, add "input-pullup resistor" to button pin for "HIGH" when NOT pressed
  pinMode(button1, INPUT_PULLUP); // assign pinMode to the button and tie it HIGH
  pinMode(button2, INPUT_PULLUP); // assign pinMode to the button and tie it HIGH
}

void loop()
{
  // lcd.print("Game name"); // moved to setup();
  // if (digitalRead(button1) == HIGH || digitalRead(button2) == HIGH) // original
  if (digitalRead(button1) == LOW || digitalRead(button2) == LOW) // waiting button press LOW
  {
    lcd.setCursor(0, 1); // place the cursor at column 0, row 1
    lcd.print("Game start .");
    lcd.print(digitalRead(button1));
    lcd.print(" :");
    lcd.print(digitalRead(button2));
  }
  else
  {
    lcd.setCursor(0, 1); // place the cursor at column 0, row 1
    lcd.print("No button pressd");
    // lcd.print("Start failed    "); // original
  }//closes else statement
}//code end

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