Mighty Microbit Basic
latest
  • Lesson 1 beautiful image lesson
  • Lesson 2 lucky 7 blocks lesson
  • Lesson 3 answering machine blocks lesson
  • Lesson 4 game of chance blocks lesson
  • Lesson 5 smiley blocks lesson
  • Lesson 6 magic logo lesson
  • Lesson 7 snowflake fall blocks lesson
  • Lesson 8 screen wipe blocks lesson
  • Lesson 9 flashing heart blocks lesson
  • Lesson 10 blink blocks lesson
    • Topic
    • Quick links
      • blink blocks activity
        • ~avatar avatar
        • ~
        • ~avatar boothing
        • ~
      • blink blocks quiz
      • blink blocks quiz answers
      • blink blocks challenges
    • Prior learning / place of lesson in scheme of work
    • Documentation
    • Objectives
  • Lesson 11 night light lesson
  • Lesson 12 game counter lesson
  • Lesson 13 happy birthday blocks lesson
Mighty Microbit Basic
  • Docs »
  • Lesson 10 blink blocks lesson »
  • blink blocks activity
  • Edit on GitHub

blink blocks activity¶

Turn an LED on and off with forever

~avatar avatar¶

basic.forever(() => {
    led.plot(2, 2)
    basic.pause(500)
    led.unplot(2, 2)
    basic.pause(500)
})

Let’s build a blinking light!

~¶

Have you ever tried to blink a flashlight at night? The concept is fairly simply: turn on the light, wait for a little, turn off the light, wait again, and repeat. That’s exactly what we need to code to get a blinking LED.

Let’s start by adding a line of code that turns on the LED at position 2, 2.

led.plot(2, 2)

Run your script to make sure it’s correct. Then, let’s add code to pause 500 milliseconds and turn off the LED.

led.plot(2, 2)
basic.pause(500)
led.unplot(2,2)

We’ve got the LED blinking once. Let’s add another pause and turn on the LED again.

led.plot(2, 2)
basic.pause(500)
led.unplot(2, 2)
basic.pause(500)
led.plot(2, 2)

The current code works but it only blinks once! We are going to use a forever loop and move the code inside it to repeat it forever. We’ve dropped the second plot line since we don’t need it in the loop.

basic.forever(() => {
    led.plot(2, 2)
    basic.pause(500)
    led.unplot(2, 2)
    basic.pause(500)
})

~avatar boothing¶

Excellent, you’re ready to continue with the challenges!

~¶

Next Previous

© Copyright 2018, sean Revision faf697ce.

Built with Sphinx using a theme provided by Read the Docs.