Enchanted bottle

Description

When the cap of a bottle is screwed on, the bottle is not enchanted. But, when the lid of the bottle is unscrewed, the contents inside the bottle light up and change different colors!

Materials

  • 3 LEDs (Red, Green, Blue)
  • 3 220k ohm resistors
  • 1 potentiometer
  • 1 diffuser
  • 1 arduino uno
  • 1 breadboard

Code

/*

 * Code Adapted From:

 http://www.arduino.cc/en/Tutorial/AnalogInput

 */

int sensorPin = A0; // select the input pin for the potentiometer
int greenLedPin = 11; // select the pin for the LED
int blueLedPin = 10;
int redLedPin = 9;
int sensorValue = 0; // variable to store the value coming from the sensor

int potPin = A1; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int i = 1; //to drive the movement through the lights
int valueGreen = 125;
int valueRed = 75;
int valueBlue =125;

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(greenLedPin, OUTPUT);
 pinMode(redLedPin, OUTPUT);
 pinMode(blueLedPin, OUTPUT);

 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
// pinMode(led, OUTPUT);
}

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 Serial.println(sensorValue);
 // turn the ledPin on
 if (sensorValue > 375) {
 analogWrite(greenLedPin, 0);
 analogWrite(redLedPin, 0);
 analogWrite(blueLedPin, 0);
 }
 else {
 analogWrite(greenLedPin, valueGreen);
 analogWrite(redLedPin, valueRed);
 analogWrite(blueLedPin, valueBlue);
 valueGreen += i;
 if (valueGreen == 255) {
 i = -i;
 }
 if (valueGreen == 125) {
 i = -i;
 }
 delay(20);
 }
}


img_7382

Leave a Reply