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
}