Energy Shield

From Microduino Wiki
Jump to: navigation, search

Overview

Anywhere game energy.jpg

Make a energy shield, which can charge automatically when attacked.
Suitable kit: mCookie302


Module Bill

Modules

Module Number Function
mCookie-Core 1 Core board
Battery box 1 Program download and power supply
mCookie-Hub 1 Sensor adapter plate
Color LED 3 Emit light
Line finder 3 Detect objects

Other Equipment

  • USB cable*1
  • Sensor cable*6
  • Lego shell
  • Lego or other fixation structure
Anywhere game energy-module.jpg

Module Setup

Setup

Anywhere game energy-step.jpg
  • 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;

Programming

Required Program Blocks


Programming Thought

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. 。

Mixly-ctrl-game energy.jpg

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.

Mixly-ctrl-game energy-code.jpg



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.

Anywhere game energy.jpg



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.

Mixly-ctrl-game energy-code1.jpg

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.

Mixly-ctrl-game energy-code2.jpg

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.

Mixly-ctrl-game energy-code3.jpg

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.

Mixly-ctrl-game energy-code4.jpg

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.

Mixly-ctrl-game energy-code5.jpg
Mixly-ctrl-game energy-code6.jpg

Technical Specification


Related Cases

FAQ