Moving LEDs

Open up the makecode.microbit.org environment. Microbits have the LED instructions shown at right:If you haven't done so already, right-click on the plot x 0 y 0 block, and select Help. You can get help like this for any microbit block. Last time we saw the locations of the LEDs, as labelled at far-right, where x,y = 0,0 is at the upper-left. The index value of x grows from 0 to 4 moving from left-to-right, and the index value of y grows from 0 to 4 moving from top to bottom.

Light up the upper-left LED out of the 5x5 grid of LEDs. Then turn it off and turn on the one to the right of it. You will need to insert a Basic pause() block as part of it, to slow it down enough to see it. The code below left does this manually for the first few LEDs. How could you instead declare variables for x and y and use those variables with a loop to do the same thing? Once you have this implemented, the LEDs should light up one at a time moving across the top row from left to right, and then it should stop, as shown in the middle below. Next modify your code so that once it reaches the far right edge, it "wraps around" back to the left-hand side and continues moving, in a loop, continuing until you stop the program, shown at right below. (Hint: It will look more realistic if you put in a pause after the row is blank, before it starts up again on the left.)

One LED at a time done "manually"

Across top row once, from left to right.

Looping across top row

Further steps:


  1. Start the led at a random x,y location by getting a random value for x using the Math drawer's pick random 0 to 4, doing the same thing for y, plotting the LED at that random x,y starting point. From that starting point again have the LED "move" to the right and wrap around, continuing until you stop the program.

  2. Do one of the following:

    1. On each iteration have the LED move not only to the right, but also either up or down, now wrapping around in both the vertical and horizontal directions whenever it hits one of those edges.

    2. Start with two different LEDs, starting in two different random locations. Have one loop horizontally and one loop vertically, wrapping around the edge of the LED panel.

    3. Light up the LEDs in a clockwise "spiral" pattern, starting at the outside and moving in. Once they are all lit, continue in the same clockwise direction, but this time from the middle outwards, now turning off the LEDs.