Spring Sale: 5% OFF Over $89, Code: Elephant5; 10% OFF Over $149, Code:Elephant10



Arduino Project: Magical Light Cup

Posted by Fiona Su on

thumb

Introduction:
Magical light cup is a product developed by KEYES that can interact with ARDUINO.
The principle is to use PWM to regulate light brightness of the two components.
Mercury switch provides digital signal, triggering PWM to regulate light brightness.
Through the designed program, we can see effect like two cups pouring light to each other.
Hardware Required:

  • Arduino Board *1
  • USB Cable *1
  • Ball Tilt Switch *2
  • Red M5 LED*2
  • 220Ω Resistor *2
  • 10KΩ Resistor *2
  • Breadboard *1
  • Breadboard Jumper Wire*10

Connection for REV4:

thumb

Connection for Arduino Mega 2560 R3:

thumb

Sample Code:

int LedPinA = 5;
int LedPinB = 6;
int ButtonPinA = 7;
int ButtonPinB = 4;
int buttonStateA = 0;
int buttonStateB = 0;
int brightnessA = 0;
int brightnessB= 255;
void setup()
{
Serial.begin(9600);
pinMode(LedPinA, OUTPUT);
pinMode(LedPinB, OUTPUT);
pinMode(ButtonPinA, INPUT);
pinMode(ButtonPinB, INPUT);
}
void loop()
{
buttonStateA = digitalRead(ButtonPinA);
if (buttonStateA == HIGH && brightnessA != 255)
{
brightnessA ++;
}
if (buttonStateA == LOW && brightnessA != 0)
{
brightnessA --;
}
analogWrite(LedPinB, brightnessA);
Serial.print(brightnessA);
Serial.print("   ");
buttonStateB = digitalRead(ButtonPinB);
if (buttonStateB == HIGH && brightnessB != 0)
{
brightnessB --;
}
if (buttonStateB == LOW && brightnessB != 255)
{
brightnessB++;
}
analogWrite(LedPinA, brightnessB); 
Serial.println(brightnessB);
delay(5);
}

Result:
Tilt the circuit to one side, one light turns on, the other one will go out; see the change if tilt to the other side.

thumb 

0 comments

Leave a comment

Please note, comments must be approved before they are published