Introduction
There are two same Magic Cup modules in this kit, and each adds a separate LED based on the mercury switch. You may learn the application of one module and then try to apply two modules together to make one dim when at the same time the other brightens.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 2 * Magic cup module
– Several jumper wires
Experimental Principle
Connect pin S and pin L of one module to digital pin 7 and pin 5 of the SunFounder Uno board; pin S and pin L of the other module to D4 and pin 6 of the Uno.
When you tilt one mercury switch, the LED attached will become dimmer gradually, while the other LED will slowly brighten. The effect is just like the telepathy.
Experimental Procedures
Step 1: Connect pin S and pin L of one module to D7 and pin 5 of the SunFounder Uno; pin S and pin L of the other module to D4 and pin 6 of Uno.
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, tilt the breadboard and you will see the LED on one module gets dimmer while that on the other LED becomes brighter.
Code
int LedPinA = 5; int LedPinB = 6; int ButtonPinA = 7; int ButtonPinB = 4; int buttonStateA = 0; int buttonStateB = 0; int brightness = 0;void setup() { pinMode(LedPinA, OUTPUT); pinMode(LedPinB, OUTPUT); pinMode(ButtonPinA, INPUT); pinMode(ButtonPinB, INPUT); }void loop() { buttonStateA = digitalRead(ButtonPinA); if (buttonStateA == HIGH && brightness != 255) { brightness ++; }buttonStateB = digitalRead(ButtonPinB); if (buttonStateB == HIGH && brightness != 0) { brightness –; } analogWrite(LedPinA, brightness); analogWrite(LedPinB, 255 – brightness); delay(5); } |