How do I make the levels go in order?

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,
Hendo03

I don't see where you are tracking the state, or in other words, what level you are running. Your code runs from start to level1 to level2 to level3 to level4 to endgame. There is nothing to keep you in any level until it's completed.

What engineering discipline? Mechanical, Electrical, Software, Nanotechnology?

If its Software Engineering, you should be familiar with Finite State Machine programming

1 Like

I'm new to this so I don't know how do that. My engineering class is an intro class that is giving us the building blocks for coding. We've learned a decent amount of techniques but I'm not familiar with what you mentioned.

So, look up state machine, or FSM.

Basically you keep track of which level your code should be in and only execute the functions that level calls for.

For example, there is nothing in level1 to stop the code flow from just continuing to Level2 regardless if you press a button or not. I have no clue how your buttons are wired since you didn't provide a schematic, but in level1 you lcd.print one of two messages then exit the function. Then loop() immediately calls the level2 function and the first thing it does is to clear the LCD. It happens so fast that you never see the level1 texts and if the button was pressed or not doesn't matter.

You need to review your logic flow. For each level, what do you want to happen that moves you to the next level? That's kind of what a state machine does.

The levels are running in order... but you are not checking that a level is complete before moving to the next level. You need some kind of loop within each level to keep it there until the task required is completed.

Example.

boolean levelComplete = false;

while (levelComplete == false)
{
  if (distance < 5)
  {
    lcd.print("Done");
    delay(1000);
    levelComplete = true;
  }
  else
  {
    lcd.print("Keep trying");
  }
}

You should also remove the call to level 1 from the start routine. Level 1 will already happen after the start as it is called next in the main loop.

How do i chain them together? do I have to do an individual loop for each.

They are already chained together... in the main loop.