Reset program after the condition is met

Hello

For a school project I’m making a arcade game. I’ve never worked with arduino before so I barely know how to work with the program.

I’m trying to make a program where the scoreboard counts up everytime a point gets scored.
When the max score is reached, I want to reset the scoreboard to 0 so the game is ready to play again. I’ve been able to program the count up function but I don’t know how to reset the program when the max score have been reached.

Can anyone help me with this please ?

Here you see my program so far.

As you can see I’ve been trying to reset by bringing the counter value back to 0 but this doesn’t work at all. I know my program isn’t the best you’ve seen, but I hope someone can help me out with this.

Thank you in advance.

could you post the entire program as text, not an image. after pasting, select the code and select the “</>” icon so that it’s listed as code without UTF characters

Hello,
does the compiler generates errorless a program ?

I’m new to Arduino, but I can’t imagine you can do this:

Y+A+Z = 0;

and I’d be surprised if it compiled without errors.

You need to set each individual variable to zero.

Try

Y=A=Z=0;

You need to reset the counters… not just the accumulated totals YAZ.
Yes, there are better ways to do what you want, but you’re learning - that’s good !

#include <LiquidCrystal.h>
int button1=8;
int button2=13;
int button3=9;
int val1;
int val2;
int val3;
int count1;
int count2;
int count3;
int press;
int Y;
int A;
int Z;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button2,INPUT);
}

void loop() {
  val1=digitalRead(button1);
  val2=digitalRead(button2);
  val3=digitalRead(button3);
  if(val1==HIGH){
    press=count1++;
    Y=press*250;
    delay(250);
  }  
  if(val2==HIGH){
    press=count2++;
    A=press*100;
    delay(250);
  }   
  if(val3==HIGH){
    press=count3++;
    Z=press*500;
    delay(250);
  }
  if((Y+A+Z)>=2000){
    Y=A=Z=0;
    count1=count2=count3=0;
    
  }
  lcd.setCursor(0, 0);
  lcd.print("Button Count");
  lcd.setCursor(0, 1);
  lcd.print(Y+A+Z);

}

I’m able to simulate the program so I think it’s errorless.
(I only use 2 buttons here just to simulate)

Now try it with switch() case logic.
Also you can break out the common code…
delay() because it’s common to all the conditions.

This is my program so far by following your sugestions but I’m not there yet.
The score that has to be reached is 2000. As you can see there are 3 different points to score (100,250,500). When I’m at 1750 points, and I score +500 points, the score would be 2250 but the max is set on 2000. If my program worked as it should, the count value should be brought back to 0 and the game should be ready to play again.
What happens right now is when I reach for example 2250 points, the lcd shows me 0250. If i continue to play, the score counts up with an extra digit. So instead of +500, the counter goes up with 5000 points. I’ve been looking for solutions for hours but it’s not that simple because I don’t really know where the problem is.

(I hope my information about the problem is clear enough)

#include <LiquidCrystal.h>
int button1=8;
int button2=13;
int button3=9;
int val1;
int val2;
int val3;
int count;
int press;


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button2,INPUT);
}

void loop() {
  val1=digitalRead(button1);
  val2=digitalRead(button2);
  val3=digitalRead(button3);
  
 
  delay(200);
  
  switch(count >= 2000){
    default:
  if(val1==HIGH)
  {
    press=count;
    count = count +250; 
  }  
 
  if(val2==HIGH)
  {
    press=count;
    count= count +500; 
  }   
  
  if(val3==HIGH)
  {
    press=count;
    count= count +100; 
  }
  break;
  
 case 1: 
  {count= 0;
   
   break;   
  }
  }
   lcd.setCursor(0, 0);
  lcd.print("Button Count");
  lcd.setCursor(0,1);
  lcd.print(count);
}

Hi, @Arthon
I can see you are just over printing your count on the LCD.
This means if you print a four digit number then a three digit number, the fourth digit will not change.
What you do is before you update the number, print blanks in its place, like below.

  lcd.setCursor(0, 0);
  lcd.print("Button Count");
  lcd.setCursor(0, 1);
  lcd.print("     ");
  lcd.setCursor(0, 1);
  lcd.print(Y+A+Z);

See if that helps.

I am not sure if your switch… case formatting is correct.

Tom… :grinning: :+1: :coffee: :australia:

Thank you! I didn’t knew the solution would be so "easy’ but my program finaly works :slight_smile:
I have no errors so I think the switch…case works, else it’s just pure luck the program still works as I hoped it would.

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