07 Mission Instructions

5. Code: Initialize the Variables

Since we are using variables, we will be using Advanced Bloxter. 

Just like the last time we used the light sensor, we need to find a threshold value by choosing a value of the light sensor reading that is between the value with the lights off and one with the lights on. You will need to write a program to check those readings and then choose your threshold. 


Now let’s look at our diagram again. We need to keep checking the light sensor. Every time we check, one of four things will be true: 

    • The light was previously on and stays on. 
    • The light was previously on, but it was turned off . 
    • The light was previously off and it stays off . 
    • The light was previously off, but it has been turned on. 

We need to know two things – what the light sensor sees right now, and what the light sensor saw last time we checked. For this, let’s create a variable called state. We are going to use state to keep track of the current state of the light. “Dark” will represent a dark room and “Light” will represent a light room. 

We should always initialize our variables at the beginning of a program, so let’s start out by setting state to “dark” (assuming we start in a dark room). We need to make sure we do this before we start our repeat loop. 

**We randomly chose “dark” as our “previous state”, which means the robot will be starting the program in either #3 or #4 of the list above.**

Create another variable for the threshold and set it to your desired threshold value. Last, create a count variable to keep track of how many times you turn the lights on. 

Let’s add a repeat loop below our variables and choose an arbitrary length of time that we want to run our state machine. Since we’re still testing and iterating, let’s keep it short. Let’s say 20 seconds for now.