Hello All,
I am working on a bop it game for my engineering class. My game has 4 game levels and then a start and finish level. After completing each game level you are given a character displayed by the lcd. At the end of the game you are told all the characters.
The start level is supposed to tell you to press the start button to begin the game entering level 1. Level 1 is supposed to make you press the button and when you do it will give you a character moving on to the next stage but if you don't you're given a message telling you what to do. Level 2 is supposed to make you cover the photoresistor and when you do it will give you a character moving on to the next stage but if you don't you're given a message telling you what to do. Level 3 is supposed to make you be within 5 in of the distance sensor and when you do it will give you a character moving on to the next stage but if you don't you're given a message telling you what to do. Level 4 is supposed to make you be within 5-10 in of the distance sensor when you do it will give you a character moving on to the next stage but if you don't you're given a message telling you what to do. Endgame is supposed to tell you the characters you received from the game.
As this is a game I want to make sure these levels run in order, currently they run all at the same time, please help me if you can.
#include <LiquidCrystal.h> //the liquid crystal library contains commands for printing to the display
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // tell the RedBoard what pins are connected to the display
int photoresistor = 0; //this variable will hold a value based on the brightness of the ambient light
int threshold = 700; //if the photoresistor reading is below this value the the light will turn on
const int gameButton = 3; //gameButton pin 3
const int startButton = 4; //startButton pin 4
const int trigPin = 7; //connects to the trigger pin on the distance sensor
const int echoPin = 6; //connects to the echo pin on the distance sensor
float distance = 0; //stores the distance measured by the distance sensor
void setup() {
Serial.begin(9600); //start a serial connection with the computer
lcd.begin(16, 2); //tell the lcd library that we are using a display that is 16 characters wide and 2 characters high
lcd.clear(); //clear the display
pinMode(13, OUTPUT); //set pin 13 as an output that can be set to HIGH or LOW
//set the button pins as inputs
pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity
pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor
}
void loop() {
start();
level1();
level2();
level3();
level4();
endgame();
}
//------------------FUNCTIONS-------------------------------
//Start
void start() {
// to start round
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Press start "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("to play! ");
if (digitalRead(startButton) == HIGH) {
level1();
}
}
//Round1
void level1() {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Press game "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("button! ");
if (digitalRead(gameButton) == HIGH) {
lcd. clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Correct!"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("Character: C"); //print the number of seconds that have passed since the last reset
} else {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Please press"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("Game button"); //print the number of seconds that have passed since the last reset
}
}
//Round2
void level2() {
//read the brightness of the ambient light for photoresistor
photoresistor = analogRead(A0); //set photoresistor to a number between 0 and 1023 based on how bright the ambient light is
Serial.println(photoresistor); //print the value of photoresistor in the serial monitor on the computer
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Cover the "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("photoresistor! ");
//if the photoresistor value is below the threshold turn the light on, otherwise turn it off
if (photoresistor < threshold) {
Serial.println(photoresistor);
delay (100);
lcd. clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Correct!"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("Character: P");
} else {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Please cover"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("photoresistor"); //print the number of seconds that have passed since the last reset
}
}
//Round3
void level3 () {
//for distance sensor
distance = getDistance(); //variable to store the distance measured by the sensor
Serial.print(distance); //print the distance that was measured
Serial.println(" in"); //print units after the distance
delay (100);
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Be less than "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("5 in away! ");
if (distance < 5) { //if the object is close
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Correct! "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("Character: N"); //print the number of seconds that have passed since the last reset
} else {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Please move"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("closer");
//if the object is a medium distance
}
}
//Round4
void level4 () {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Be within "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("5-10 in ");
if (5 < distance && distance < 10) { //if the object is close
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Correct! "); //print hello, world! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("Character: D"); //print the number of seconds that have passed since the last reset
} else if ( 0 < distance && distance < 5) {
lcd.clear();
lcd.setCursor(0, 0); //set the cursor to the 0,0 position (top left corner)
lcd.print("Please move"); //print correct! starting at that position
lcd.setCursor(0, 1); //move the cursor to the first space of the bottom row
lcd.print("back");
//if the object is a medium distance
}
}
//End
void endgame() {
lcd.clear(); //clear the screen
lcd.setCursor(0, 0); //move the cursor to the top center of the screen
lcd.print("Characters:"); //print "You"
lcd.setCursor(0, 1); //move the cursor to the bottom center of the screen
lcd.print("CPND"); //print "WIN!"
}
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calculatedDistance; //variable to store the distance calculated from the echo time
//send out an ultrasonic pulse that's 10ms long
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
//pulse to bounce back to the sensor
calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
return calculatedDistance; //send back the distance that was calculated
}
Thanks in advance,
RH