Stap 2, Blink

Before learning how to walk, learn how to blink a LED!

It is tradition in the world of robot programming to start by making a LED blink on and off, so this is what we will start with.

Before we start the programming, we need to make sure the mBlock environment is set up correctly.

1
First connect the Simple Walker to a USB port of you computer.

charging

2
Start mBlock (download).
3
Make sure mBlock is running in Arduino mode (Edit -> Arduino mode).

les1_01

When set up correctly, the environment should look like this:

les1_01

4
Now we need to select the right board type. For the Simple Walker this is Arduino Leonardo. Go to Boards -> Arduino Leonardo.

les1_01

5
Now we need to select the right serial port. Under Windows this can sometimes be a bit of trial and error. Go to Connect -> Serial Port. If there is only one com port, select that one. If there are multiple options, just select a random one. You will find out soon enough if it is the right one.

Windows:
les1_02

Mac:
les1_02

6
Now it is time to start coding! mBlock is a visual programming language. You can drag and drop blocks that define primitive operations that the robot can perform. By combining the blocks in the right way, you can get the robot to do pretty much anything.

Let’s start by assembling the blocks in such a way to make a LED blink on and off.

First click on the section Robots from the scripts menu. Find the block called Arduino Program and drag into the grey script area. This block is the starting point of any script for the Simple Walker.
les1_03
les1_03

7
Go to the Control section and find the forever block. Attach it below the Arduino Program block. This block will tell the Simple Walker to repeat something forever. We’ll define what that “something” is, in the next steps.
les1_04
les1_04
8
Go back to the section Robots and drag the block set digital pin into the forever loop. Change the pin number from 9 to 13. This block will tell the LED to turn on.

les1_05

9
From Control, drag the wait block below the set digital pin block. This will tell the Simple Walker to do nothing for 1 second.

les1_06

10
Repeat step 3 and 4: Get another set digital pin and wait block. But this time set the value of the set digital pin to LOW. This will tell the LED to turn off.

les1_07

11
Upload the program. Click on the upload button. It might take a minute for the Simple Walker to load the program. During the programming the LED on the Simple Walker might blink erratically. This is normal. Once it is done you should see the LED blink on and off regularly.

If the LED does not blink or you got some error during the upload it might be the case that you didn’t select the right COM port. Go to Connect -> Serial Port, select a different COM port and try to upload again.

les1_08

Wat did you just do?

The blocks that you have dragged into one script represent simple operations. The forever block tells the robot that some behavior should be repeated indefinitely. What will be repeated? Those blocks that you put inside of the forever block. In this case you tell the Simple Walker to turn the LED connected to pin 13 on, then wait for a second, and then turn it off again. After this wait another second. This process is repeated forever.

Puzzles:

See if you can figure out some variations on this program yourself:

Puzzle 1. Faster

Change the sketch in such a way that the LED will blink twice as fast.

Puzzle 2. Twist

Change the sketch in such a way that the LED will stay on for only 0.1 seconds and off for 1 second.

Puzzle 3. SOS

What if the Simple Walker needs help? It can not talk, but it can use the LED to communicate. Create a sketch that will send the SOS signal using the LED. The Morse code for SOS is short-short-short-long-long-long-short-short-short.