8. case 05:Flashlight#

8.1. Our Goal#


  • Use watch kit to create a wearable flashlight.

8.2. Material#


  • 1 x Watch kit

8.3. Hardware Connection#


Plug the LED ring into power:bit just like the picture below.

../../_images/xLUYTkT.jpg

8.4. Software#


Microsoft Makecode

8.5. Programming#


Step 1#

  • Click Advanced in the code drawer of MakeCode to see more options.

../../_images/LjMR5IU3.png

  • To program for the LED ring, we have to add a package. Find Add Package in the bottom of code drawer and click it. This will pop up a dialogue box. Search for “neopixel” and click to download this package.

../../_images/0u6UbMV.png

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.

Step 2#

  • When start our program, create a variable flag and set its value to 0.

  • When button A is pressed, set the value of the variable flag to 1.

  • When button B is pressed, set the value of the variable flag to 2.

  • When button “A+B” is pressed, set the value of the variable flag to 0.

../../_images/n6EOHiO.png

Step 3#

  • Create a forever loop to continuously judge if statements.

  • When flag equals to 1,call function light(turn on 3 LEDs); when flag equals to 1 and button A is pressed, call function light_02(All LEDs turned on).

  • When flag equals to 2,call function police(simulate alarm light).

  • When flag equals to 0,call function true off to turn off all LEDs.

../../_images/shL403s.png

Step 4#

  • Function light allows 8 LEDs connected to P2 port and turns on the LED number 4~6 with white light.

../../_images/fUgwYDa.png

  • Function light_02 will turn on all LEDs.

../../_images/v0v7crG.png

  • Function police allows number 0~4 LEDs become blue and number 4~8 LEDs become red. Each section delays for 0.1 second.

../../_images/8Wujurq.png

  • Function true off will turn off all LEDs.

../../_images/MTjecwh.png

Program#

8.6. Result#


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.

../../_images/Uiksjgk.gif

8.7. Think#


8.8. FAQ#


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.

8.9. Relative Readings#