Block coding on Arduino: your first blink project, step by step
If you are new to block coding, blinking an LED is the best first project. Let's build the LED blink project in BlueGrays step by step with blocks. Circuit: the LED's long leg goes to pin 9 of the Arduino Uno through a 220 Ω resistor, the short leg goes to GND. Blocks: 1. The outer block is "setup (runs once) / loop forever". The setup part runs once when the board starts, the loop part keeps running for as long as the board is on. 2. Put "set pin 9 as OUTPUT" in the setup part. 3. In the loop part, stack these in order: "write HIGH to pin 9", "wait 100 ms", "write LOW to pin 9", "wait 100 ms". These four blocks blink the LED 5 times a second. Change both waits to 1000 and it blinks once a second. Changing a number and watching what happens is the fastest way to understand what each block does. The Arduino C++ version of the same blocks is below. Each block turns into a single line: "set pin" becomes pinMode, "write" becomes digitalWrite and "wait" becomes delay.
1void setup() {
2 pinMode(9, OUTPUT); // set pin 9 as OUTPUT
3}
4
5void loop() {
6 digitalWrite(9, HIGH); // write HIGH to pin 9
7 delay(100); // wait 100 ms
8 digitalWrite(9, LOW); // write LOW to pin 9
9 delay(100); // wait 100 ms
10}
1 comments
Log in to comment
Try it yourself: make the LED blink 3 times fast, then once slowly. Hint: put a "repeat 3 times" block inside the loop part, with the fast blink inside it and the slow one below it. The blocks for this project are ready in the app: https://bluegrays.com/p/1 Turn on "Show as blocks" in the code tab and the code is replaced by the blocks. The "Understand the blocks" tab then walks through the program step by step, showing the blocks for each step. Which wait time did you pick? Post it here.