Make a energy shield, which can charge automatically when attacked.
Suitable kit: mCookie302
- USB cable*1
- Sensor cable*6
- Lego shell
- Lego or other fixation structure
- Assemble modules and sensors:
- Stick the red core module, battery base, and green extension module together;
- Connect the three color LED to pin 6/7 of Hub;
- Connect the line finders to pin A0/A1, A2/A3 and A6/A7 of Hub;
- Insert one end of USB cable into the battery base, and connect the other end to computer;
Required Program Blocks
When something approaches it, the energy shield will emit energy light, and the shorter the distance is, the stronger the light is.
Observe the below schematic diagram, the visible brightness range is 0-255, and the distance value range is 0-1023; the distance range is approximately 3 timers more than the brightness range. The relationship between the change of the brightness and that of the distance is: the longest distance (1023), the lowest brightness (0); the shortest distance (0), the highest brightness (255).
So we can find the red brightness of the color LEDs is reversely proportional to the distance value of the corresponding line finder, and we can get the format: red brightness=255-(distance÷4). In program, just put the following program blocks in the area of setting brightness. 。
Start to Program
Display the force value according to the distance
The red brightness of each color LED is related to the distance value of the corresponding line finder.
Test the result
After upload the program, move close to the three line finders respectively to find if the color of the color LED has changed.
Q: During related games, the energy shields have health value, to a certain value, the game will be over, can it be changed into the energy shield with heath value?
A: You should set a variable to record the energy value, which can reduce 1 for every time being attacked, and can be displayed in the serial port.
Declare a variable power to record the energy value of the shield, and the initial value is 100. Convert the distance value detected by the line finder to the brightness of the color LED.
When the distance is less than 100, for every 100ms, the value of power reduces 1. When the distance value of line finder A0 is less than 100（attack comes）, the energy value reduces 1, and delay 100ms.
When the distance value is less than 100, for each 100ms, the value of power reduces 1, and when the distance value of line finder A2 is less than 100 (attack comes), the energy value reduces 1, and delay 100ms.
When the distance value is less than 100, for each 100ms, the value of power will reduce 1. When the distance value of line finder A6 is less than 100 (attack comes), the energy value reduce 1, and delay 100ms.
Print the energy value out, display the real-time "energy value%" in serial monitor.
When the power is exhausted, if the power value is less than 1 (power is run out), repeat executing: display "DEAD" on the computer screen, and turn all LEDs off.