7. case 05:Flashlight¶
7.1. Our Goal¶
Use watch kit to create a wearable flashlight.
1 x Watch kit
7.3. Hardware Connection¶
Plug the LED ring into power:bit just like the picture below.
Advancedin the code drawer of MakeCode to see more options.
To program for the LED ring, we have to add a package. Find
Add Packagein the bottom of code drawer and click it. This will pop up a dialogue box. Search for “neopixel” and click to download this package.
Note: If you get a hint that some packages will be deleted due to the problem of incompatibility, you can either follow the prompts, or create a new project in the project menu.
When start our program, create a variable
flagand set its value to 0.
When button A is pressed, set the value of the variable
When button B is pressed, set the value of the variable
When button “A+B” is pressed, set the value of the variable
Create a forever loop to continuously judge if statements.
flagequals to 1，call function
light(turn on 3 LEDs); when
flagequals to 1 and button A is pressed, call function
light_02(All LEDs turned on).
flagequals to 2，call function
police(simulate alarm light).
flagequals to 0，call function
true offto turn off all LEDs.
lightallows 8 LEDs connected to P2 port and turns on the LED number 4~6 with white light.
light_02will turn on all LEDs.
policeallows number 0~4 LEDs become blue and number 4~8 LEDs become red. Each section delays for 0.1 second.
true offwill turn off all LEDs.
Press button A, all LEDs on LED ring are turned on.Press button B, LEDs are flashing.Press button A+B, all LEDs are turned off.
Q: Why the LED light looks yellow while in the code it is white?A: Due to the insufficient battery power, the current can’t support to drive the LED ring.