How to program a photoresistor to input an LCD display

I really need help with trying to program a project i am doing.

The goal of the project is that a button is high for around 20 seconds. The photoresistor with read a value and everytime the value < 300, it will change the message on the LCD Display.

Once the button is low with a count 16 for instance, the button will display a certain message such as "final score:16". While the button is high and the count is 1 it will show "score 1". When the value goes over 300 5 times, the player wins the game meaning the button would have to go low before the 20 seconds is up.

How could I program something like this to happen?

my current code also includes a motor, buzzer, and LED as an OUTPUT for the button and the LCD as an output for the Display only.

Here is my code. Im able to press the button and it shows, "score 0", but wont read the code to go to score 1.

dont the mind the serial.prints i just needed to keep track of them.

#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.

int ButtonPin = 11;

//Red LED when the maximum score is done

int RedLED = 1;

// 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.

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.

pinMode(RedLED, OUTPUT);//will blink 0.5 hz when maximum score is completed.

//Speed of the motor per revolution:

myStepper.setSpeed(1000);

Serial.begin(9600);

pinMode (ButtonPin, INPUT);//push button will be the input for the step motor, and buzzer.

}

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);

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

lcd.setCursor(0,0);

//the light sensor will be an input to the LCD display. when it is a certain value.

int value = analogRead(A0);

Serial.println("Analog value: ");

Serial.println(value);

if(value < 90){

count++;}

delay(10);

Serial.println("button value: ");

Serial.println(buttonState);

if(buttonState==LOW){

digitalWrite(RedLED, LOW);

digitalWrite(RedLED, LOW);

lcd.clear();

lcd.setCursor(3,0);

//”this is the game” will appear on the top row on the LCD.

lcd.print("this is the game");

lcd.setCursor(1,3);

//”Skee-Ball Game” will appear on the bottom row on the LCD.

lcd.print("Skee-Ball Game");

delay(2000);

//”This is the game” 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);

if(count==1){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks 4 playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 1");

delay(4000);

Serial.println("1 is here------------");

}

}

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

buttonState = digitalRead (ButtonPin);

if(buttonState==HIGH){

lcd.setCursor(1,0);

//The Maximum score for our game is 11.

lcd.print("Max Score: 5");

lcd.setCursor(4,1);

lcd.print("Score: 0");

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

myStepper.setSpeed(10);

digitalWrite(buzzer,LOW);

Serial.println("Analog value : ");

Serial.println(value);

delay(10);

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

myStepper.setSpeed(10);

digitalWrite(buzzer,LOW);

Serial.println("Analog value : ");

Serial.println(value);

delay(10);

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

myStepper.setSpeed(10);

digitalWrite(buzzer,LOW);

Serial.println("Analog value : ");

Serial.println(value);

delay(10);

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

myStepper.setSpeed(10);

digitalWrite(buzzer,LOW);

digitalWrite(RedLED, LOW);

Serial.println("Analog value : ");

Serial.println(value);

delay(10);

Serial.println("counterclockwise");

myStepper.step(-stepsPerRevolution);

myStepper.setSpeed(10);

digitalWrite(buzzer,LOW);

Serial.println("Analog value : ");

Serial.println(value);

delay(10);

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

}

//Score 1

while(count==1){

lcd.clear();

lcd.setCursor(1,0);

lcd.print("Max Score: 11");

lcd.setCursor(4,1);

lcd.print("Score: 1");

Serial.println("THIS IS ONEEEEEEEEEEEEE");

}

//Score 2

if(count==2){

lcd.clear();

lcd.setCursor(1,0);

lcd.print("Max Score: 11");

lcd.setCursor(4,1);

lcd.print("Score: 2");

Serial.println("TWWWWWWWWWWWOOOOOO");

}

/*

//Score 2

if(count==2){

lcd.clear();

lcd.setCursor(1,0);

lcd.print("Max Score: 11");

lcd.setCursor(4,1);

lcd.print("Score: 2");

}

//Score 3

if(count==3){

lcd.clear();

lcd.setCursor(1,0);

lcd.print("Max Score: 11");

lcd.setCursor(4,1);

lcd.print("Score: 3");

}

//Score 4

if(count==4){

lcd.clear();

lcd.setCursor(1,0);

lcd.print("Max Score: 11");

lcd.setCursor(4,1);

lcd.print("Score: 4");

}

if(count==5){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Congratulations!");

lcd.setCursor(1,1);

lcd.print("Final Score: 5");

delay(5000);

digitalWrite(RedLED, HIGH);

delay(250);

digitalWrite(RedLED, LOW);

delay(250);

digitalWrite(RedLED, HIGH);

delay(250);

digitalWrite(RedLED, LOW);

delay(250);

digitalWrite(RedLED, HIGH);

delay(250);

digitalWrite(RedLED, LOW);

delay(250);

digitalWrite(RedLED, HIGH);

delay(250);

digitalWrite(RedLED, LOW);

delay(250);

//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 high loop

if(count==1){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 1");

delay(4000);

}

}

if(count==2){

while(buttonState==LOW){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 2");

delay(4000);

count==0;

}

}

if(count==3){

while(buttonState==LOW){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 3");

delay(4000);

count==0;

}

}

if(count==4){

while(buttonState==LOW){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 4");

delay(4000);

count==0;

}

}

if(count==5){

while(buttonState==LOW){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 5");

delay(4000);

count==0;

}

}

if(count==10){

while(buttonState==LOW){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Thanks for playing");

delay(2000);

lcd.setCursor(1,1);

lcd.print("Final Score: 1");

delay(4000);

count==0;

}

}

*/

//end of low if loop

}//end of void loop

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the </> icon above the compose window) to make it easier to read and copy for examination

the first thing to do is to RE-edit your first posting so that your code is in a code-section. You can read here how you can RE-edit a posting

A "button" is neither HIGH nor LOW a button is pressed or unpressed

Your description massively lacks of a lot of details.
You are using a stepper-motor and a buzzer
Your description should include what the stepper-motor and the buzzer is doing in which situation.
How to code the rest depends on what the stepper-motor and the buzzer shall do!
This is the reason why these details are improtant too!

best regards Stefan

20 seconds pressing a button is a looooong time. (x 20)

And it would a really good idea to study millis() timing, and simple state-machines.

Otherwise - in another month you’ll have no idea what your code does, or why.

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