Lesson 12 - Bonus

Counting rounds

Now that we’ve implemented a remote start, let’s expand our programming knowledge even more! In this lesson, we will learn about variables.

We will display a “rounds counter” on the LCD screen. (Each time we start a race, we want a new round to start.)

What are variables?

Variables are a fundamental part of programming languages. A variable is simply a location in a computer’s memory that can contain a value.

Imagine we are in a huge storage room (the storage room is Arduino’s memory). There are many shelves with boxes on them. Each box is labeled with a unique name. When we create a variable, we put a new box on the shelf. We will name our box “round number.” When Arduino opens the box, there will be a value inside.

If we put the value “1” in the box, we can tell Arduino to replace it later with another number (“2,” “3,” “4,” etc.).

Now back to reality with what we’ve learned. To summarize, variables are places in a computer’s memory. They are given a unique name and contain values.

Variables have countless uses in programming. In the following lessons, we will explore a few more of them!

Let’s start with a simple variable (“round number”) to store the number of rounds we’ve raced.

Step 1: Creating a variable

We need to create a new variable to use it. When we create a variable, we tell Arduino to save some space for the variable in its memory.

Create a variable and name it “round number” as shown in the video:

Step 2: Define initial value

Now, we will give our variable an “initial value.” The initial value is the first value Arduino sees when you turn on the race track. Because the race will not have started yet, we want the “round number” to be “0.”

Assign a value to the new variable as shown in the video:

Step 3: Increase value

Next, we will increase the variable’s value by +1 every time a new round starts.

Let’s count rounds with the start button. Each time we click the start button, Arduino will add +1 to our variable.

mBlock has a special block for increasing variable values. Select and set this block as shown in the video

Step 4: Display round counter on the screen

We now have a variable containing the current round number, but we can’t see this number on our race track.

Let’s use the LCD screen to display the round number.

We are already using the first line of the LCD screen to print messages. So, we will show the round number on the second line.

Make the changes to your code as shown in the video:

After uploading the code, race a few rounds. The counter on the screen will change:

Step 5: Rounding

mBlock will print numeric variable values with decimal places. But we don’t need decimals because a round-counter is always an integer.

To turn the decimal number into an integer (2.00 -> 2), we will use a “round” block. (for mathematical “rounding,” not to be confused with a round of our race!)

Make the changes to your code as shown in the video:

This is the result you should get:

Step 6: Formatting the display

The round number displayed on the second line of our screen looks a little lonely. It’s also hard to understand what this number means to a new player.

Let’s add a label to this number and place it in the center of the line.

This is the result we want:

How will we make our round counter centered?

Under the LCD menu there are two similar blocks:

The first block only allows us to chose a row. By default, the message will print at the beginning of the line.

However, the second block also lets us pick a position on the line to print our message. Let’s use the second block to print the round counter in the middle of the line.

Make the changes to your code as shown in the video:

Lesson completed!

Well done! You have finished another lesson!

In the next lesson, we will use our new knowledge of variables to display the time it takes the winning car to complete the race!