Make a small desk lamp, and learn to control the on and off of it with switch.
Suitable kits: mCookie102, mCookie202, mCookie302, IBC
- USB cable*1
- Sensor cable*1
- Sensor shell*1
- Lego or other fixation structure
- Assemble the modules and sensors:
- Stick the red core module, USB upload module and green extension module together;
- Connect the red LED to pin 6/7 of Hub;
- Connect the crash sensor to pin 4/5 of Hub;
- Insert one end of USB cable into USB to upload the modules, and connect the other to computer;
Required Program Block
Press the crash sensor, the LED lights on; press again, the LED goes off.
1. Press the crash sensor, the LED lights on.
Set the pin of "crash" program block to 4, and the state to press; set the pin of "digital write" program block to 6, and the state to high; that is controlling the LED to light up after pressing the crash sensor.
2. Press again, the LED goes off
Set the pin of the "crash" program block to 4, and the state to press; set the pin of "digital write" to 6, and the state to low; that is controlling the LED to go off after the crash being pressed.
3.Test the result:
Look at if it is as expected: press the switch, the light is on; press again, the light goes off. Repeat as this.
Q: Why is the switch flexible sometimes, and out of control now and then?
A: This is because the thinking speed of the core module is very fast, the moment you press the crash, the program judging switch stays down all the time, and the program has been executed millions of times. At this point, LED has been on and off for many times, which is invisible,that is why you think it is out of control.
Q: I get it. If I set the program to wait for the crash to be pressed, and then wait for it to be released, is this OK?
A: You can have a try!
Press the crash down, and then release, to turn the LED on. Repeat it to make turn the LED off. Modify the parameters of program block "crash" and "digital write".
Q: The switch is still out of control?
A: Because the "crash" belongs to the mechanical keys, at the moment of being pressed or released, the internal metal chip will shake many times, the program will detect the crash has been pressed and released many times, and the program will be executed continuously. Actually, in this program, the LED has been turned om many times which can't be seen.
Q: How to make the switch stable?
A: In fact, only a "delay" can resolve this problem. At the time of pressing the switch, delay 100ms（make the program pause for 100ms）, to wait for the internal shock of the crash subside, and eliminate the jitter of the crash.
Every time the switch being pressed and released, add a delay program block to wait for the internal of the crash calm down, in order to make the switch able to control the LED.
1. Every time the switch being pressed and released, add a delay, to wait for the internal shock of the crash to calm down. Press, release, then the LED lights up.
2. Every time the switch being pressed and released, add a delay, to wait for the internal shock of the crash to calm down. Press, release, then the LED goes off.
Test the result:
Press, release, then the LED lights up; press, release, then the LED goes off. Repeat that, turning it on and off continuously. So that we can use the crash to control the light stably.
Set up a desk lamp on your own, put the switch and LED at the right place, and see whose lamp is the most beautiful one.
- The product number of crash sensor: MSDS11
- Digital signal input sensor
- For details, please refer to Sensor-Crash.