How to make a value change while button is on/high

I need help doing a function for a project.

so far this code changes the "score" and keeps it there until the button goes low again. for example it will say:

"rockin r's skee-ball game" hold button to enter"

you will hold it until it saids "Max score: 5 Score_"

then it will already say "max score: 5, Score: 1"

it only reads the count and will display the next point after the button is pressed.

it will then after the buttonstate goes low. will say "thanks 4 playing Final score: _"

and go back to the "Game menu"(the rockin rs state) again as I want it to.

but again my question is:

how do I make the score go up when the value changes a certain amount of times while my buttonstate is high?

its a skee ball game so the motor will be moving the balls back to the player and the buzzer will help the player know that the game is over as well as sound differently when the player has won the game (reached Score: 5).

and how do i make the code go back to the beginning after the highest score was wone?

#include <LiquidCrystal.h>
#include <Stepper.h>

//Int for the stepper motor
int stepsPerRevolution = 2048;
int buttonState = 0;

//push button is in pin 11 on arduino.
const int ButtonPin = 11;

// the pins are 1N1:0 1N2:1 1N3:2, 1N4:3
Stepper myStepper(stepsPerRevolution, 10, 2, 13, 3);
//int for the LCD display
int rs=4;
int en=9;
int d4=7;
int d5=6;
int d6=5;
int d7=8;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
//int for buzzer. It will go off when the game is done and when the game is won.
const int buzzer = 12;//the pin of the active buzzer.


//Count for light sensor
 int count = 0;


void setup() {
// put your setup code here, to run once:
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(ButtonPin, INPUT);//when the push button is pressed, the motor will come on for 20
//seconds.
pinMode(buzzer,OUTPUT);// will go off 20 seconds after the push button is pressed and the
//motor stops for 3 seconds.


//Speed of the motor per revolution:
myStepper.setSpeed(1000);
Serial.begin(9600);


}

void loop() {
// put your main code here, to run repeatedly:
//The button is pressed to reset the game after the stepper motor has stopped.
buttonState = digitalRead (ButtonPin);

if(buttonState==LOW){
  lcd.clear();
lcd.setCursor(3,0);
//”Rockin R’s” will appear on the top row on the LCD.
lcd.print("Rockin Rs");
lcd.setCursor(1,3);
//”Skee-Ball Game” will appear on the bottom row on the LCD.
lcd.print("Skee-Ball Game");
delay(2000);
//”Rockin R’s and Skee-Ball Game” are cleared from the LCD Board
lcd.clear();
lcd.setCursor(0,0);
//”Press the button” appears on the top row on the LCD.
lcd.print("Hold the button");
lcd.setCursor(4,3);
//”to play” will appear on the bottom row on the LCD.
lcd.print("to play");
delay(2000);
//”Press the button” and “to play” are cleared from the LCD.
lcd.clear();
delay(30);
Serial.println("main menu");
}//end of low if loop

//The ACTUAL FUNCTION OF THE GAME! THIS WILL LAST 20 SECONDS.


  
   



 //note: line 1 is the second row, since counting begins with 0):

    
if(buttonState==HIGH){
  int value = analogRead(A0);
Serial.println(value);
   if(value < 400){
     count = count+1;
     lcd.setCursor(1,0);
     lcd.print("Max Score: 5");
     lcd.setCursor(4,1);
     lcd.print("Score: ");
     lcd.setCursor(11,12);
     lcd.print(count);
     delay(900);
   }

 
 while(count==5){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Congratulations!");
lcd.setCursor(1,1);
lcd.print("Final Score: 5");
delay(5000);
lcd.clear();
count==0;
      }
   lcd.setCursor(1,0);
//The Maximum score for our game is 5.
lcd.print("Max Score: 5");
;//the light sensor will be an input to the LCD display. when it is a certain value.
delay(100);

   myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);

 myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);

  myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);

  myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);

  myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);

  myStepper.step(-stepsPerRevolution);
 myStepper.setSpeed(10);
 digitalWrite(buzzer,LOW);
 delay(10);




 // if(millis()< (minutes * 20.333333)){// 20 seoconds.

   
     
  //}
   Serial.println("buzzer on");

digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
  

if(count==1){
lcd.setCursor(0,0);
lcd.print("Thanks 4 playing");
delay(2000);
lcd.setCursor(1,1);
lcd.print("Final Score: 1");
Serial.println("ONNNNNNNEEEEE");
delay(4000);
lcd.clear();
buttonState==LOW;
}


if(count==2){
lcd.setCursor(0,0);
lcd.print("Thanks 4 playing");
delay(2000);
lcd.setCursor(1,1);
lcd.print("Final Score: 2");
Serial.println("TWWWWWWOOOOOOOO");
delay(4000);
lcd.clear();
buttonState==LOW;
 }

if(count==3){
lcd.setCursor(0,0);
lcd.print("Thanks 4 playing");
delay(2000);
lcd.setCursor(1,1);
lcd.print("Final Score: 3");
Serial.println("TTTHHHHRREEEEEE");
delay(4000);
lcd.clear();
buttonState==LOW;
}


if(count==4){
lcd.setCursor(0,0);
lcd.print("Thanks 4 playing");
delay(2000);
lcd.setCursor(1,1);
lcd.print("Final Score: 4");
Serial.println("FOOUUUUUUUURURURUR");
delay(4000);
lcd.clear();
buttonState==LOW;
}
}

/*
else if(count==5){

myStepper.setSpeed(0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Congratulations!");
lcd.setCursor(1,1);
lcd.print("Final Score: 5");
delay(5000);

//Buzzer
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(200);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(300);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
delay(100);//wait for 1ms
//end of button state low loop
count==0;
}
}
*/
//high
//end of void loop
}

Sorry I'm pretty lost. What is the value that is being changed? Is this value supposed to increase the longer you hold your button down?

Can you maybe explain how this game works in a step-by-step process, and then explain the step that's troubling you?

sure.

this is how i want the game to go.

when you initially see the LCD display to start playing the game, you will see
"rockin r's Skee ball game. Hold button to play"

you will only hold the button until the game becomes "live" when the LCD changes to "Max Score: 5". The motor will start spinning and you at that point will have 20 seconds to get the ball in the hole up to 5 times. during these 20 seconds. the lcd will show your score if you get any balls in the hole(past the photoresister).

after 20 seconds if you don't get 5 points, the buzzer will make a noise, the motor will turn off, the LCD display will thank you for playing. show your final score and then go back to the" game menu"(rockin' rs skeeball game. Hold button to play).

the game menu is set to the buttonState==LOW aka the button being off because the game isnt active.

if you do reach 5 point (count==5). the display will congratulate you, show the final score of 5 and then take you back to game menu.

the step that is troubling me is that I cannot get the sensor to read the ball as it passes through the sensor to reward a point.

with this current code, the score only goes up once when the game is active. For example, it will only say "score: 3" the entire 20 seconds and then il restart the game by pressing the button again and then the score will go up and say "Score: 4" for 20 seconds.

it's difficult for me to follow the logic of your code.

it's odd that the button state (pressed or not pressed) governs whether the game is being play. it looks like the game is being played by default.

the many delays (53) seem to interfere with the play of the game, recognizing a button press or reading the sensor.

games are an interesting way to learn to program because they are so interactive

it would be too wordy to comment on the code. setting File->preferences->Compiler warnings to More identifies several problems with the code, the following doesn't do what you intended

consider the following attempt to implement what i think you game is in a more organized way. having the separate function: idle(), play() and score() make it easier to understand which part is doing what.
it also uses timers instead of delays

#undef MyHW
#ifdef MyHW
# include "simLib.hh"
const byte ButtonPin = A1;
const byte SensorPin = A2;

#else
# include <LiquidCrystal.h>
# include <Stepper.h>
const byte ButtonPin = 11;
const byte SensorPin = A0;
#endif

//Int for the stepper motor
int stepsPerRevolution = 2048;
int butState = 0;


// the pins are 1N1:0 1N2:1 1N3:2, 1N4:3
Stepper myStepper (stepsPerRevolution, 10, 2, 13, 3);

// LiquidCrystal lcd (rs,en,d4,d5,d6,d7);
LiquidCrystal lcd    ( 4, 9, 7, 6, 5, 8);

const byte BuzzerPin = 12;

int count = 0;

enum { Idle, Play };
int state;

unsigned long msec;
unsigned long msecLst;
unsigned long msecPeriod;

int idleState;

// -----------------------------------------------------------------------------
void idle ()
{
    if (msec - msecLst >= 2000)  {
        msecLst = msec;

        if (0 == idleState++)
            dispLcd ("Rockin Rs", "Press to start");
        else  {
            dispLcd ("Press buton", "to play");
            state = 0;
        }
    }
}

// -----------------------------------------------------------------------------
void play ()
{
    if (msec - msecLst >= 20000)  {
        score ();
        return;
    }

    myStepper.step (-stepsPerRevolution);

    int value = analogRead (SensorPin);
    Serial.println (value);

    if (value < 400)  {
        count++;

        lcd.setCursor (1,0);
        lcd.print ("Max Score: 5");
        lcd.setCursor (4,1);
        lcd.print ("Score: ");
        lcd.print (count);
    }

    if (count == 5)  {
        score ();
        return;
    }
}

// -----------------------------------------------------------------------------
void score ()
{
    if (5 == count)  {
        dispLcd ("Congratulations!", "You Won");
    }
    else  {
        lcd.setCursor (1,0);
        lcd.print ("Thanks 4 playing");
        lcd.setCursor (4,1);
        lcd.print ("Score: ");
        lcd.print (count);

        for (int n = 8; n > 0; n--)  {
            digitalWrite (BuzzerPin,HIGH);
            delay (300);
            digitalWrite (BuzzerPin,LOW);
            delay (100);
        }
    }

    state = Idle;
    delay (5000);
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();

    switch (state)  {
    case Idle:
        idle ();
        break;

    case Play:
        play ();
        break;
    }

    byte but = digitalRead (ButtonPin);
    if (butState != but)  {
        butState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            msecLst = msec;
            count   = 0;
            state   = Play;
        }
    }
}

// -----------------------------------------------------------------------------
void
dispLcd (
    const char *s0,
    const char *s1 )
{
    lcd.clear ();
    if (s0)
        lcd.print (s0);
    if (s1)  {
        lcd.setCursor (0, 1);   // 2nd row
        lcd.print (s1);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    lcd.begin (16, 2);

    pinMode (ButtonPin, INPUT_PULLUP);
    butState = digitalRead (ButtonPin);

    pinMode (BuzzerPin,    OUTPUT);

#ifdef MyHW
    pinMode (SensorPin, INPUT_PULLUP);
#endif

    myStepper.setSpeed (1000);
}