I need help to make this code work!

So hey guys,

this is my first post here , I got an arduino a month ago and I learned how to code simple thing and I wanted to make a project with buttons and a lcd display and two teams and a timer so when i press a button team a score will go up by 1 and displayed in the lcd display but it didnt work so can you show me my mistakes? btw the code runs fine but it just doesn't do what I want it to do when I press a button nothing happens,
Thanks!

#include <LiquidCrystal.h>
int Avalue = digitalRead(13);
int Bvalue = digitalRead(10);
const int bpA = 13;
const int bpB = 10;
int a_Score = 0;
int b_Score = 0;
int time = 120;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  pinMode(bpA, INPUT_PULLUP);
  pinMode(bpB, INPUT_PULLUP);
  lcd.begin(16, 2);
  Serial.begin(115200);
  Serial.print("A:");
  Serial.println(a_Score);
  Serial.print("B: ");
  Serial.println(b_Score);
  while (time <= 120) {
    time += -1;
    delay(1000);
    lcd.setCursor(0, 1);
    lcd.print("Time left: ");
    lcd.setCursor(11, 1);
    lcd.print(time);
    lcd.setCursor(0, 0);
    lcd.print("A: ");
    lcd.setCursor(2, 0);
    lcd.print(a_Score);
    lcd.setCursor(5, 0);
    lcd.print("B: ");
    lcd.setCursor(7, 0);
    lcd.print(b_Score);
  }
}

void loop() {

  if (digitalRead(bpA) == LOW) {
    a_Score += 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Team A Scored");
    delay(5000);
    lcd.setCursor(0, 1);
    lcd.print("Time left: ");
    lcd.setCursor(11, 1);
    lcd.print(time);
    lcd.setCursor(0, 0);
    lcd.print("A: ");
    lcd.setCursor(2, 0);
    lcd.print(a_Score);
    lcd.setCursor(5, 0);
    lcd.print("B: ");
    lcd.setCursor(7, 0);
    lcd.print(b_Score);
  }
  if (digitalRead(bpB) == LOW) {
    b_Score += 1;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Team B Scored");
    delay(5000);
    lcd.setCursor(0, 1);
    lcd.print("Time left: ");
    lcd.setCursor(11, 1);
    lcd.print(time);
    lcd.setCursor(0, 0);
    lcd.print("A: ");
    lcd.setCursor(2, 0);
    lcd.print(a_Score);
    lcd.setCursor(5, 0);
    lcd.print("B: ");
    lcd.setCursor(7, 0);
    lcd.print(b_Score);
  }
  if (time = 0) {
    lcd.clear();
    if (a_Score > b_Score) {
      lcd.setCursor(0, 0);
      lcd.print("Team A Wins!");
      lcd.setCursor(0, 1);
      lcd.print("A: ");
      lcd.setCursor(2, 1);
      lcd.print(a_Score);
      lcd.setCursor(4, 1);
      lcd.print("B: ");
      lcd.setCursor(6, 1);
      lcd.print(b_Score);
    }
    if (a_Score < b_Score) {
      lcd.setCursor(0, 0);
      lcd.print("Team B Wins!");
      lcd.setCursor(0, 1);
      lcd.print("A: ");
      lcd.setCursor(2, 1);
      lcd.print(a_Score);
      lcd.setCursor(4, 1);
      lcd.print("B: ");
      lcd.setCursor(6, 1);
      lcd.print(b_Score);
    }
    if (a_Score == b_Score) {
      lcd.setCursor(0, 0);
      lcd.print("It's a tie!");
      lcd.setCursor(0, 1);
      lcd.print("A: ");
      lcd.setCursor(2, 1);
      lcd.print(a_Score);
      lcd.setCursor(4, 1);
      lcd.print("B: ");
      lcd.setCursor(6, 1);
      lcd.print(b_Score);
    }
  }
}

One issue i seen is that you don't have the pinmodes named in your setup for the buttons. If you name your pinmodes as INPUT_PULLUP, the switches would be connected to 10 and 13, and ground for both.

And then change the line if(digitalRead(bpA) == HIGH) to =LOW instead of HIGH. and the same for your other switch.

The input_pullup are already on HIGH on idle, and when you press the button it changes to LOW and runs the supplied code.

Thanks for the reply,
Can you give me an example because I don't know how to do it.

Also, your code will be easier to read if you format it properly. Place the cursor in the IDE's source code window and press Ctrl-T. That will format the code into a common C style.

What wildcat99 is saying is to change your pin mode calls to:

  • pinMode(bpA,INPUT_PULLUP);*

and then test them with:

  • if(digitalRead(bpA) == LOW) {* // Test for LOW, which is a switch closure

okay i just tried that but it still doesn't say that team A or B scored,
here is my wiring ( sorry if it's messy i just did real fast before going to school to test things out before trying it on my arduino)
is the scoring part below of the code correct?
i tried doing ctrl +t but it didnt work im using visual studio code i think thats why its not working
the display just counts down and displays this and doesnt change when i press the buttons

Oh my. :’(

Learn to check for switch changes rather than switch levels.

See change in state example.

Confirm your switches go from 5v to 0v with a DVM.

another thing you can try which i think which may fix it. in this spot:

if (digitalRead(bpA) == LOW) {
a_Score += 1;

change it to a_Score++; instead of +=1;

and the same to b_Score

and then on top you change (int a_ and b_score=1) instead of =0 So the first button press will be 1, and each press adds 1 to the counter.

i'm working on a similar problem myself, but i'm trying to go up and down on counting and it keep track.

okay ill try that and thanks everyone for helping me!