AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Loops and light shows

Week 29 · Unit 12: Be an artist · 3 lessons
1

Count the flashes

Computing 4: Unit 12, pp. 168–179
GoalUse the repeat block to run blocks a chosen number of times, and work out how long the whole loop takes.
New words
loop · a group of blocks that the computer runs again and againcount-controlled loop · a loop that repeats a number of times that we chooseround · one trip through the blocks inside a loop
Explanation

Some light shows need the same blocks again and again. We could copy the blocks many times, but that makes a long, untidy program. A loop is better. In micro:bit blocks the repeat (n) times block is a count-controlled loop. We put the blocks to repeat inside it and type how many times. Every trip through the inside is one round. The total time is the time of one round times the number of rounds. Blocks below the loop run only once, after the last round has finished.

Without a loopWith a loop
show, pause, clear, pause, show, pause, clear, pause, show, pause, clear, pause, show, pause, clear, pauserepeat 4 times: show, pause, clear, pause
16 blocks5 blocks
Worked examples
show ledspause (ms)500clear screenpause (ms)500repeat4timeson buttonApressed
A flashing diamond. One round shows the small diamond for 500 ms and then clears the screen for 500 ms. That is 1000 ms = 1 second a round, and four rounds take 4 seconds.
show ledspause (ms)1000show ledspause (ms)1500repeat3timeson buttonApressed
A plus and X show. One round is 1000 + 1500 = 2500 ms, which is 2.5 seconds. The loop has 3 rounds, so the show takes 2.5 × 3 = 7.5 seconds. The X is still on at the end, because nothing clears it.
show ledspause (ms)500show ledspause (ms)500repeat2timesshow iconhappyon buttonApressed
Blocks after the loop. The diamond pair shows twice, which takes 2 seconds. Only then does the happy face appear. It is shown once, not twice.
Class activity

Clap the loop (with family): choose a pattern, such as clap, stamp, clap, clap. Say “repeat 3 times” and do the pattern three times with a grown-up counting the rounds. Then change the number and the pattern. Make a pattern with a part before the loop and a part after it, such as one bow, then the pattern 4 times, then a spin. Can your family tell which part happened once and which part happened again and again?

Practice
1
How many times does the triangle appear, and how long does the whole program run?
show ledspause (ms)2000clear screenrepeat3timeson buttonApressed
2
Write the blocks for this plan: when button A is pressed, flash the big diamond 5 times. It stays on for 0.5 seconds, then the screen is off for 0.5 seconds.
3
What is on the display 3.7 seconds after button A is pressed? What is on the display 4.5 seconds after?
show ledspause (ms)500show ledspause (ms)500repeat4timesclear screenon buttonApressed
4
A child wants the heart to flash 3 times. This program leaves the heart on all the time. Why?
show iconheartpause (ms)500repeat3timeson buttonApressed