We have completed all the needed preparations, and it’s time to do some programming!
First, we will program Arduino to open the gate and start the race.
This video shows the final result of what you will achieve in this lesson:
In our case, the servo motor connects to the orange gate. So when the servo moves, the gate will open, and the cars will release onto the track.
Let’s go back to mBlock and start building our first program.
Step 1: Basic Arduino block
Each time we write code for Arduino, we start with a basic Arduino block: “when Arduino starts up.”
Add this block to our program as shown in the video:
You have now added a basic Arduino block! Every Arduino program requires this block. Now, let’s make the servo motor move.
From the Pin menu, add the servo block as shown in the video below. (Make sure you attach the new block to the existing one!)
Let’s upload the code to Arduino and make sure it works. We will explore how the servo block works later.
Check that the USB cable connects Arduino to your computer.
How will you know if Arduino is connected?
If you can see the Upload button (shown in the picture below), you will know it is connected.
If you do not see the Upload button, click the Connect button instead (like we did in the last lesson).
Once connected, click on the Upload button as shown in this video:
If you run into any issues, try restarting your computer:
1) Save the file (File->Save to your computer)
2) Restart your computer
3) Open mBlock
4) Open your file (File -> Open from your computer)
5) Connect Arduino and try uploading again
Why does this help? When using your computer, extra data builds up and may cause problems. Restarting clears the extra data, which gives you a faster and more reliable session on your computer.
If restarting does not work, try downloading the Arduino driver, install it, then restart your computer again.
If installing the driver does not work, please try using mBlock on another computer if possible.
If all the above does not fork please contact us for assistance at email@example.com
When the code finishes uploading successfully, the servo motor will move one time (as seen in the video below).
If you missed it, don’t worry! Just continue to the next step.
The servo block has two parameters that you can edit: “angle” (as we just tried) and “pin.”
Arduino has many “teeth” (called pins) used to connect different electronics components. For example, the servo motor is an electronic component, and it uses these pins to connect to your Arduino.
To program an electronic component, we need to tell Arduino which pin connects to the component.
Our servo motor connects to pin 9, which is already the default pin on the servo block. So, there is no need to change it.
How is the servo connected to pin 9?
It may be challenging to see, but between your Arduino and all the other components is a black circuit board with tiny lines called “traces.” The circuit board holds the Arduino and the traces connect to the pins.
Below is a large image of the black circuit board. Look for the lines that run across the board to different places (not the white lines). These connect the Arduino pins to your components:
Right now, we can make our servo move. But every time we want to move the servo, we need to change the angle value and upload a new code. Not good enough!
We will improve the code so Arduino will perform the following actions every time it turns on:
1) Close gate (180-degrees).
2) Wait 3 seconds
3) Start the race – Open the gate (0-degrees)
Watch this video to see what we want to achieve:
Let’s start by closing the gate. We already know how to do it!
Update your code to use 180 degrees as the angle:
Now we want to wait 3 seconds, then close the gate.
Under the control menu, find a block called “wait 1 second”. Add this block to our code, then change the wait time to 3 seconds. Watch this video to see how:
Now, this is what our code is now telling Arduino to do:
“When you turn on, close the gate and wait 3 seconds.”
Next, we will tell Arduino to open the gate after waiting is complete.
To do that, we will need to add another servo block. But this time, set the angle to 0 degrees.
Add a second servo block with this setting as shown in the video below, then upload the code:
Congratulations! We have now written a basic start-sequence code. When Arduino powers on, it will close the gate, wait 3 seconds, then open the gate.
However, we have a small problem. The code only runs one time when Arduino powers on. When we are racing, we will need a way to start the race without re-uploading the code before each race.
Can you use the on/off switch to solve this problem?
Because the computer is connected to Arduino with the USB cable, the on/off switch will not turn off the Arduino. The computer will continue to power it through the USB cable even when the switch is turned off.
The solution is the small restart button located on top of Arduino. Push this button to restart the Arduino and execute the code again.
Push the restart button, as shown in this video:
Although our code is simple, at this stage, we can already play a racing game!
Every time you want to start a new race, push the restart button to trigger the start sequence!
After you downloaded the file open it in mBlock using File -> “Open from your computer“.
This step is optional and only needed if you want to move away the track from your computer.
You can continue with the workshop without installing the batteries as long as you keep the Arduino and your computer connected with the USB cable.
Once the code is uploaded, the USB cable is only supplying power to Arduino. Our latest code will still be saved on Arduino even if we disconnect the cable.
So, if you want to move the race track away from your computer, you can continue playing using only battery power (3 X AA batteries – not included in the kit).
To install the batteries loosen both screws (included in the orange upper ramp), as shown in the video below.
After disassembling the ramp, open the battery box lid and insert the batteries, then close the lid:
Now, let’s re-assemble the ramp.
Important: Look from the side and make sure the screws enter the holes in the black triangles.
Do not screw the screws in all the way or apply force. You can stop once the screws are inside the triangles.
New batteries should give you many hours of racing (as long as you turn off the system after playing!) When replacing the batteries in the future, follow the same process as we just did.
After installing the batteries you can move the race track away from your computer and use battery power instead like shown in the video below:
Our race track works! It is still a basic racing game because there are many components we are not using yet. In the next lesson, we will upgrade our code and add some action to the game!