The Blinking LED is the most basic Arduino lab and usually the first one people learn.

This lab will make one LED blink on and off.  The hardware hookup of this lab is exactly the same as the Arduino Parts Set Up.

Below is the code you will use. 

Before you start using mBlock for Arduino, you have to set up mBlock for Arduino mode:

1.  Go to Extensions and select Arduino, but make sure mBot is unselected (click on it to unselect it if it has a checkmark on it).

2.  Go to Boards and select the Arduino Uno.


Step 1.

First you have to go to Arduino mode which is in Edit --> Arduino Mode.  This will get rid of your stage and an Arduino coding screen will pop up on the right.

 

Step 2.

Go to the Robots category and drag out the first script called Arduino Program.  Everything under this script will appear as code in the Arduino code area to the right.

Step 3.

Go to the Control category and pull out forever.  Anything in the forever loop will appear in the Void loop() area.

**note: you cannot put a forever loop inside a forever loop in Arduino mode or it will crash!

Step 4.

Go to the Robots category and pull out "set digital pin 9 output as HIGH".

Change the 9 to a 13.

Step 5.

Go to the Control category and pull out the top script called "wait 1 secs".  Place this under "set digital pin".

Step 6.

Right-click anywhere on the colored area of "set digital pin".  Select duplicate, and put the clone underneath "wait 1 secs" so that it is within the forever loop.

Step 7.

Change the HIGH in the clone to LOW.


Code Analysis:

You can see directly how the mBlock code translates into the Arduino IDE code.

Forever = void loop

set digital pin 13 output as HIGH = digitalWrite (13,1)  **here 1 is equal to HIGH which is full on

wait 1 secs = delay (1000*1)    **the 1000*1 just makes it equal to 1000, but it represents how many milliseconds to wait as 1 second = 1000 milliseconds)

Compare this code to the code in this Arduino Lesson: Arduino Blinking LED


 

Extra Challenge:

Add 3 more LEDs, and the matching code to make them light up one at a time and then go off one at a time.

Use a variable to control the wait time so you only have to change the wait time in ONE spot, rather than under each wait command.

The code is below: