Lesson 5: Clap Lights Program
To Know - Create a Clap Lights Program
To Be Able To - Develop a Clap Lights Program
Step 1: Make It
Turn your micro:bit into a light that you can turn on and off by clapping or making any loud sound.
The program uses a variable called lightsOn to keep track of the light's status: whether it's switched on or off. We're using it as a special kind of variable, a Boolean variable. Boolean variables can only have two values: true (on) or false (off).
When the microphone sensor detects a loud sound, the code toggles the value of lightsOn by setting it to be not lightsOn.
This means that when you clap, if lightsOn is false (and the lights are off), it becomes true and the program lights the LEDs.
If lightsOn was true (and the lights were on), it becomes false and the code switches the LEDs off by clearing the screen.
Brainstorm in pairs, using the description above to breakdown what the program needs to do.
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
___________________________________________
____________________________________________
Step 2: Code It
Use the reference list in micro:bit classroom for guidance.
You will need to toggle a boolean variable (True/False) for this program.
This can be done using the format shown below:
variablename = not variablename
Example for this program:
lightsOn = not lightsOn
Step 3: Improve It
Adjust Microphone Sensitivity
In Python, to change the threshold for loud sounds use microphone.set_threshold(SoundEvent.LOUD, 128) - changing the number 128 to the value you want between 0 and 255.
Make Lights Dance
Make the lights also play a tune when they turn on.