Lesson 8

Start button

Instead of restarting Arduino to begin a new race, it is much better to use the onboard button!

In this lesson, we will program Arduino to “feel” button clicks and start a new race each time you push the button.

This is the final result you will achieve by the end of this lesson:

How does Arduino feel?


So far, we have used Arduino’s ability to activate various components such as a servo motor, buzzer, and an LCD screen.

Arduino has another excellent ability: it can ‘feel’ by getting data from sensors connected to it.

What is a sensor?
A sensor is an electronic component that can sense things happening in the environment.

Some examples of sensors:
-Temperature sensor
-Fire sensor
-Motion sensor
-Fingerprint sensor

Those are just some examples; there are thousands of different sensors for Arduino!

When Arduino and a sensor are connected, we can program Arduino to read data from the sensor, then react to the data. This creates endless possibilities for things we can build. Here are few examples:

–Automatic plant watering system
Connect Arduino to a soil moisture sensor and a water pump. When the moisture in the soil is low, Arduino will activate the pump.

–Alarm system
Connect Arduino to a motion sensor and a buzzer. When a motion is detected, Arduino will make noise using the buzzer.

–Smart lock
Connect Arduino to a fingerprint sensor and an electric motor. When a specific fingerprint is detected, Arduino can unlock the door.

In this lesson we will use a very simple sensor – button.
A button is a sensor because it can detect when you press/click it in the real world.

Let’s use the same principle explained in the examples above. We will get data from the sensor and react to the data. This will program Arduino to start a race when you press the button!

Step 1: Disconnect the code

We will first learn how to program the button. To keep things simple, we will disconnect the rest of the code for now.
Disconnect the code from the first block as shown on the video:

Why did we choose to disconnect and not delete?

By disconnecting the code from the first block, we tell Arduino to ignore that code. This way, our code is still saved, and we can reconnect it later.

Step 2: Adding IF condition block

What is a condition block?
This block tells Arduino, “do not execute a certain part of the code unless something else happens first.”

Why do we need a condition in our code?
We only want the race to start when we press the button. So, we will use a condition block to tell Arduino, “only start the race when the button is clicked.”

Conditions are very common in programming and allow programmers to create advanced, complex programs.

Let’s add a simple condition to our code to see how it works.

Add the if->then block as shown in the video:

Step 3: Define condition

Our if->then block has two parts:
Part 1) Where we need to define the condition
Part 2) Where we need to put the code which will only run if the condition is met.


Let’s start with the condition.

Our goal is to create a simple code that will do the following:

If the button is pressed,
then the buzzer will make noise

How will Arduino know if the button is pressed?
Before sending your kit, we connected the button-sensor to pin 14 of our Arduino for you.

Because the button and Arduino are already connected at pin 14, we can tell Arduino to watch what is happening with pin 14.

If the button is not being pressed, the state of pin 14 will be “0.”
But when the button is pressed down, the pin’s state will be “1”.

In other words, we tell Arduino: If you feel “1” at pin 14, activate the buzzer.

Let’s make the changes to the code so you will see it action and better understand how it works:

The equal(=) operator

First, we opened the operator menu and chose the equal(=) operator. We do this to check if pin 14 is equal to 1. Later we will also be using other operators such as bigger(>) or smaller(<).

Step 4: Adding buzzer code

We only want to activate the buzzer if the button is pressed (pin 14 is equal to “1”). So, we will put the buzzer code in the second part of the condition block.

Make the changes to your code according to the video:

Step 5: Checking our code

After you upload the code, try pressing the button. Surprisingly, nothing will happen! Why is that?

Our code is currently only running when Arduino starts up. After the code finishes running at startup, nothing else will happen. Arduino will only check our condition one time when it starts up. Then, our Arduino will rest.

Let try it again. Only this time, press the button and hold it down. While still pressing the button, restart Arduino (as shown in the video below). When Arduino starts back up, it will check the condition, and because you are holding the button down, the buzzer will sound.

Step 6: Improving our code

Now we need to tell Arduino to continuously check our condition, not just once when it starts. When finished, Arduino will always be ‘watching’ pin 14 when turned on.

To achieve this, we will use another important concept of programming – loops!

What are loops?
Loops allow us to repeat a specific section of our code. There are many different loops that programmers use, but we will begin with the simplest one – a loop that repeats forever.

Add the forever block and insert our condition as shown in the video:

This is the result you should get:

In this video, we inserted a block into another block for the first time. Let’s explore what is happening:

We have a loop that is casing the code inside it to repeat forever (until Arduino is turned of or restarted). Inside this loop we have our condition that is checking weather a button is pressed.

Since the condition is inside the loop, Arduino will constantly check the condition and activate the buzzer when the button is pressed.

Step 7: Adding the button to our race

Now, all we need to do is combine our new button code with our old race code.
Make the changes according to the video:

This is the result you should get:

Step 8: Fixing a bug

We have a small bug in our code (“bug” means that our code is not working as we planned).

What is the problem?

In the first race, everything works as expected. But if we push the button again to start a new race, the green light is still on from the previous race. This bug causes the red and green lights to be on at the same time.

Let’s make a small fix to the code, follow the video:

In this video, we fixed the bug by setting pin 15 to “low” when the race starts, turning off the green light.
This is the result you should get:

Next lesson!

In this lesson, we learned about loops and conditions; now, our race’s starting line is ready! Next, we’ll start working on the finish line!