Clap to summon a duck with the enviro:bit

What you will learn:

How to use a variable to store a "state" - in this case whether there is a duck or not!

What you will need:

micro:bit, enviro:bit, device to code the micro:bit with (e.g. laptop), USB cable to transfer data

How to do it:

The enviro:bit has a microphone on it, which can detect a clap sound. You will find the "when I hear a clap" block in the envirobit part of the menu. To make a simple version of this code, you could simply say "when I hear a clap"... "show icon of a duck". What we want to do is have the micro:bit remember if there is a duck or not.

1. In the variables menu, create a new variable called "duck". In the "on start" block, add in a block from the variables menu to say that at the start of the program, there is No Duck.

2. Now we need to listen for a clap, and then check whether there is a duck or not. In the logic menu, you will find a block for "if ___ then, else"

3. If there is no duck, show a duck and remember there is One Duck. Else (otherwise), clear all ducks, and remember there is No Duck. Let's tackle the No Duck scenario.

Drag a comparison block from the logic menu into the hexagon that says "true". Use the duck variable inside it.

4. If there is No Duck, we want a duck to be displayed, so add that next.

5. Now we've added a duck, the amount of ducks is not zero, so we need to change the duck variable to say that there is one duck.

6. If the duck isn't zero, that means there is a duck there, so we need to clear the screen to get rid of any ducks, and take 1 away from the duck number.

7. Try clapping to make your duck appear and disappear.

Challenges:

Use the same technique to turn the lights on the enviro:bit on or off.

Use a second micro:bit and the radio blocks to trigger an event - maybe use an enviro:bit on one to detect a clap, and a neopixel ring on the other to turn the lights on and off. Start and stop a motor - the choice is yours!

Take a reading and display the temperature on the micro:bit display when you clap your hands.

Download the code: