[No.8] Master the Power of Repeating! Light Up LEDs One by One

Programming and Making

Let's sharpen your programming skills so you can build all kinds of features with SPACEBLOCK, the tiny computer for making things. This time, you'll use the LED panel and one of the most basic programming skills: "repeating"!

Elementary School ~ Beginner 35 min

What You Need & Getting Ready

Left: SPACEBLOCK. Top right: LED panel. Bottom right: USB cable

Connect the LED Panel to SPACEBLOCK

Connect the LED panel to SPACEBLOCK. Plug it straight into the sideways pin socket, or connect it with jumper wires. This time, you'll practice by running programs with SPACEBLOCK still connected to the computer, so you don't need the battery box.
Left: LED panel plugged in directly. Right: LED panel connected with jumper wires

If you use jumper wires, look at the picture below for the details.

When you connect the LED panel with jumper wires, check where each wire goes

Show a Warning Sign

Let's turn the bright LED panel into a sign that warns people about danger. First, show a "!" mark for 1 second. The program is shown in the next picture.
A program that shows a "!" mark for 1 second (1000 milliseconds) when the program starts

Now you have a program where the "!" mark lights up and turns off after 1 second. We want it to stand out more, so let's make the mark blink.

How Do You Make the Mark Blink?

Blinking means "on → off → on → off → on → off → on → off → on → off → on → off → on → off →" and so on. The pattern "on → off →" repeats again and again.

To repeat the same pattern of instructions, use the [Repeat] block in the "Loops" category. The [Repeat] block keeps running the instruction blocks you put inside it, over and over.

The [Repeat] block repeats the program you put inside it

Attach a [Repeat] block to [Start Program], and build the program inside it as shown in the next picture.

Show the mark on the LED panel, wait 1 second, turn off the LEDs, wait 1 second, and go back to the start

Let's watch it in the video.

Repeating is super handy when you do the same pattern many times, like blinking. The key to an efficient program is finding the smallest pattern you want to repeat.

How Do You Light Up the LEDs One at a Time?

Next, let's light up the LEDs on the panel one at a time, in order. The LED panel has 8×8, or 64, LEDs, and you can choose each one separately to turn it on or off.

To light up an LED at a certain position, use the [8 x 8 RGB LED (A10): light x (1) y (1) in (color)] block in the "LED" category. You set the position with an x coordinate and a y coordinate.

You set an LED's position with its x and y coordinates

Light Up the 8 LEDs in a Row, One by One

First, let's light up just the top row of the LED panel, from left to right. If you build the program by setting each position one at a time with the [8 x 8 RGB LED (A10): light x (1) y (1) in (color)] block, it looks like the next picture.
A program that sets each LED one at a time and lights one up every second

You can keep attaching instruction blocks like this, but as the number grows, building the program gets really hard.

If you look closely at the program, the same block is attached again and again, and only the x coordinate in the [8 x 8 RGB LED (A10): light x (1) y (1) in (color)] block is different. The x coordinate goes up by 1 each time, from 1 to 8. When something repeats while a value changes by a rule like this, you can build the program efficiently by using a "variable".

Work Smarter with Variables and Repeating!

A "variable" is a container that can hold a value, and you can give it any name you like. You can change the value stored in a variable while the program is running.

First, make a new variable by following the steps in the next picture. Name it "X."

❶ Click the [Add Variable...] button in the "Variables" category ❷ Type any name you like, choose "Number" as the type, and click [Create] ❸ Your variable blocks appear

Next, put "1" into the variable "X" you just made as its starting value. As in the next picture, attach a [Number set (X) to ( )] block to the [Start Program] block and set it to 1.

Put a number block into the [Number set (X) to ( )] block and set it to 1

Now, use repeating to build a program that lights up just the top row of the LED panel from left to right. As in the next picture, build the program inside a [repeat (8) times] block.

A program that lights up the LEDs in row 1 from left to right

<How the Program Works>

❶ Use the variable "X" as the x coordinate in the [8 x 8 RGB LED (A10): light x (1) y (1) in (color)] block. Whatever value is in "X" when the program runs becomes the x coordinate.

❷ Put the value of "X" plus 1 into the variable "X." Each time it repeats, the value in "X" goes up by 1.

Use the next picture to imagine how the value in "X" changes.

It's the same block, but the value of "X" changes each time it repeats, so it points to a different x coordinate every time

Run the program, and the 8 LEDs in row 1 light up one by one from the left.

The LEDs light up one at a time from the left

Watch it in the video.

Light Up All the LEDs in Order

Now let's keep going down to the next rows and light up all the LEDs in order. Going across, the x coordinate went up by 1. Going down, the y coordinate goes up by 1. Use the next picture to imagine the order the LEDs light up in, and their x and y coordinates.
When the LEDs light up in order, the x and y coordinates change by a rule

Let's upgrade the program you just made that lights up the top row from left to right. Add a new variable "Y" and build the program as in the next picture. Put another [repeat (8) times] block inside the [repeat (8) times] block.

A program that lights up every LED one at a time, in order

<How the Program Works>

❶ This part lights up 8 LEDs in a row, one by one. The x coordinate is the variable "X," and the y coordinate is the new variable "Y." Each time it repeats, "X" goes up by 1.

❷ This part moves the next LED to the left end of the row below. Each time it repeats, "X" is reset to 1 and "Y" goes up by 1.

Run the program. The LEDs light up starting from the top left. When all 8 in a row are lit up to the right end, the next row lights up from its left end. It finishes at the right end of row 8.

All 64 LEDs light up in order

Watch it in the video.

QR code for project
Repeating is useful not only for doing exactly the same instructions again and again, like blinking, but also for efficiently repeating instructions while changing values with variables. Remember it as one of the basic programming skills!