Help making game

I have recently decided to amke a game with a lcd 16X2 fnd two buttons. The goal of the game is to score as many clicks as possible. The problem is that I don't understand how to make the second millis for the game timer. Help me please.

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
unsigned int numbcount1 = 0;
unsigned int numbcount2 = 0;
boolean button1;
boolean button2;
#define player1button 2
#define player2button 3
unsigned long debounce;
unsigned long time;
void setup() {
  pinMode(player1button, INPUT_PULLUP);
  pinMode(player2button, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Player1:");
  lcd.setCursor(0, 1);
  lcd.print("Player2:");
}

void loop() {
   button1 = !digitalRead(player1button);
   button2 = !digitalRead(player2button);
   if(button1 == 1 && millis() - debounce > 110){
   debounce = millis();
   numbcount1++;
   lcd.setCursor(9, 0);
   lcd.print(numbcount1);
   }
   if(button2 == 1 && millis() - debounce > 110){
   debounce = millis();
   numbcount2++;
   lcd.setCursor(9, 1);
   lcd.print(numbcount2);
   if(millis() - time > 10000){
     delay(1500);
     lcd.clear();
     if(numbcount1 > numbcount2){
     lcd.setCursor(4, 0);
     lcd.print("PLAYER 1");
     lcd.setCursor(6, 1);
     lcd.print("WINS");
     }
     else{
    lcd.setCursor(4, 0);
     lcd.print("PLAYER 2");
     lcd.setCursor(6, 1);
     lcd.print("WINS");
     delay(900000);
     }
   }
  }
}

Welcome to the forum
Thank you for using code tags in your first post

What exactly do you want the "second millis" to do ?

Incidentally, it seems very clumsy to use millis() for timing in the sketch then to use delay(). I also note that the delay(900000) only occurs if player 2 wins. Is that what you intended ?

Thank you.
I have finally understood my mistake in delay in the end of code.
I wanted to make something like ending by using delay.
Could you help me with this?

I am not clear what "this" is

What does the sketch do now and what do you want to happen that is different than that ?

One problem is that you have the game timer inside the 'if' for button2 being pressed. That mean's that if button2 is never pressed, the game can never end.

Change this part:

  if (button2 == 1 && millis() - debounce > 110)
  {
    debounce = millis();
    numbcount2++;
    lcd.setCursor(9, 1);
    lcd.print(numbcount2);
    if (millis() - time > 10000)
    {
  if (button2 == 1 && millis() - debounce > 110)
  {
    debounce = millis();
    numbcount2++;
    lcd.setCursor(9, 1);
    lcd.print(numbcount2);
  }

  if (millis() - time > 10000)
  {

(That will leave you with an extra '}' near the bottom that you will have to remove.)

Your delay() is only in the case Player 2 wins. Change it to:

     lcd.print("PLAYER 2");
     lcd.setCursor(6, 1);
     lcd.print("WINS");
     }

    delay(900000);
    // If you want the game to re-start after the 
    // huge delay, this is where you would set 
    // 'time' to millis() to re-start the timer.

Thanks for these two parts. But there is one more problem. If i hold the button points start counting without a stop. How i can fix this?

See the "StateChangeDetection" example for how to act when the button CHANGES from unpressed to pressed, rather than repeating for as long as the button is pressed.

1 Like

So now everything is alright.
Final code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
unsigned int numbcount1 = 0;
unsigned int numbcount2 = 0;
boolean button1;
boolean button2;
#define player1button 2
#define player2button 3
unsigned long debounce;
unsigned long time;
bool buttonflag1 = 0;
bool buttonflag2 = 0;
void setup() {
  pinMode(player1button, INPUT_PULLUP);
  pinMode(player2button, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Player1:");
  lcd.setCursor(0, 1);
  lcd.print("Player2:");
}

void loop() {
   button1 = !digitalRead(player1button);
   button2 = !digitalRead(player2button);
   if(button1 == 1 && buttonflag1 == 0 && millis() - debounce > 110){
   buttonflag1 = 1;
   debounce = millis();
   numbcount1++;
   lcd.setCursor(9, 0);
   lcd.print(numbcount1);
   }

   if(button1 == 0 && buttonflag1 == 1 && millis() - debounce > 110){
  buttonflag1 = 0;
  }

   if(button2 == 1 && buttonflag2 == 0 && millis() - debounce > 110){
     buttonflag2 = 1;
   debounce = millis();
   numbcount2++;
   lcd.setCursor(9, 1);
   lcd.print(numbcount2);
   }
   if(button2 == 0 && buttonflag2 == 1 && millis() - debounce > 110){
  buttonflag2 = 0;
   }
   if(millis() - time > 20000){
     delay(1500);
     lcd.clear();
     if(numbcount1 > numbcount2){
     lcd.setCursor(4, 0);
     lcd.print("PLAYER 1");
     lcd.setCursor(6, 1);
     lcd.print("WINS");
     }
     else{
    lcd.setCursor(4, 0);
     lcd.print("PLAYER 2");
     lcd.setCursor(6, 1);
     lcd.print("WINS");
     }
     delay(900000);
   }
  
}

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