Outline
Let's make a force sword, which will light up gradually when we hold the hilt.
Suitable kits: mCookie302, IBC
Module Bill
Module
Other Equipment
- USB cable*1
- Sensor wire*3
- Sensor shell*1
- Lego or other fixation structure
Module Setup
Setup
- Assemble the modules and sensors:
- Stick the red core module, battery base and green extension module together;
- Connect the 2 color LEDs to pin 6/7 of Hub respectively;
- Connect the touch sensor to pin 4/5 of Hub.
- Insert one end of USB cable into the battery base, and connect the other to computer.
Programming
Required Program Blocks
Programming Thought
ColorLED prepare, wait for the touch sensor to be pressed, and make the "every time" program block light up gradually.
Start To Program
ColorLED prepare, wait for the touch sensor to be pressed.
Use "every time" to make the two LEDs light up gradually.
Define the variable i to the value of the red light of the color LED, and it increases from 0 to 255, by 1 each time; the red light values of LED 1, 2 vary with the change of i.
Delay 4s, to make the LED light up gradually, and the change is gentle, 4×255 times≈1s. After pressing the touch sensor, the force sword will arrive the brightest in around 1s.
Test the result
After upload, press the touch sensor, and the red LED will become brighter gradually, and arrive the brightest in around 1s. The force sword is completed.
Q: I find when the touch button is pressed, it can repeat the process of being on continuously; if I want the light to go off gradually, what should I do?
A: When the touch button is released, make the light go off gradually.
Release the touch sensor, the two color LEDs bbecome dark gradually.
Define variable i to the value of the red light of the color LED, and decrease from 255 to 0 gradually, by 1 each time and go off gradually. The force sword will completely be off in around 1s.
Technical Specification
- The product number of color LED: MSDL11
- APT library is required to drive.
- For details about color LED, please refer to Sensor-Color LED.
- For details about touch sensor, please refer to Sensor-Touch Button.
Related Cases
FAQ
|