Arduino Leonardo LED Color Changing

by ulandachen0509 in Circuits > Arduino

128 Views, 0 Favorites, 0 Comments

Arduino Leonardo LED Color Changing

71ClWuwF15L._AC_SL1500_.jpg

The lightbox can be used for many functions, for example, the lights' colors can be adjusted and be used as a lamp. As the potentiometer is being turned, different LED lights with different colors light up.

Materials

arduino-leonardo-microcontroller-headers_3.jpeg
breadboard-self-adhesive-white-prt-12002-3e7.jpeg
IMG_7404.jpeg
led.jpeg
220-ohm-resistor.jpeg
wire.jpeg
IMG_7402.jpeg
IMG_7403.jpeg

  • Arduino Leonardo x1
  • Breadboard x1
  • Potentiometer x1
  • 3mm LED x5 (red, yellow, blue, green, white)
  • 220-ohm Resistor x5
  • Jumper Wires x15 (male-male x10, male-female x5)
  • Box x1
  • Tape x1

Procedure

FPJS2ZLJB2S5QVA.png
IMG_7407.jpeg
IMG_7401.jpeg
IMG_7406.jpeg
IMG_7405.jpeg
IMG_7408.jpeg
  1. Connect the circuit according to the picture above. *Use male-female wires for the LED
  2. Make the box
    1. Cut five small holes on top of the box for the LED
    2. Cut the hole for the potentiometer
    3. Cut the hole for USB power source output
  3. Use the tape to wrap around the connecting point of the LED and the wires to make sure it won't fall off.
  4. Place the LED inside the holes.
  5. Place the box onto the board, cover the wires, and align the potentiometer position with the hole.
  6. Connect Power and Shine!

Code

int potPin = 2;   //sets potentiometer pin at analog 2
int potVal = 0;   //holds the value read in from the potentiometer
int led1 = 11;    //pin number 11 has led1
int led2 = 10;    //pin number 10 has led2
int led3 = 9;     //pin number 9 has led3
int led4 = 6;     //pin number 6 has led4
int led5 = 5;     //pin number 5 has led5
byte full= 255;   //the highest brightness is 255
byte low = 50;   //the low brightness is 50
byte dim = 5;    //the dim is set to 5
byte off = LOW;   //and off has no voltage

void setup(){
  Serial.begin(9600);
  pinMode(potPin, INPUT); //potentiometer is an input
  pinMode(led1, OUTPUT);  //led1 is an output
  pinMode(led2, OUTPUT);  //led2 is an output  
  pinMode(led3, OUTPUT);  //led3 is an output
  pinMode(led4, OUTPUT);  //led4 is an outut
  pinMode(led5, OUTPUT);  //led5 is an outut
}//end setup()

void loop(){
  //read in analog value from potentiometer and stores into potVal
  potVal = analogRead(potPin);
  //prints the value in the serial monitor
  Serial.print(potVal);
  //displays the brightness of each led based on the position of the potentiometer
  if(potVal>=0&&potVal<205)
  {
    digitalWrite(led1,full);
    digitalWrite(led2,low);
    digitalWrite(led3,dim);
    digitalWrite(led4,off);
    digitalWrite(led5,off);
  }
  if(potVal>=205&&potVal<410)
  {
    digitalWrite(led1,low);
    digitalWrite(led2,full);
    digitalWrite(led3,low);
    digitalWrite(led4,dim);
    digitalWrite(led5,off); 
  }
  if(potVal>=410&&potVal<615)
  {
    digitalWrite(led1,dim);
    digitalWrite(led2,low);
    digitalWrite(led3,full);
    digitalWrite(led4,low);
    digitalWrite(led5,dim);
  }
  if(potVal>=615&&potVal<820)
  {
    digitalWrite(led1,off);
    digitalWrite(led2,dim);
    digitalWrite(led3,low);
    digitalWrite(led4,full);
    digitalWrite(led5,low);
  }
  if(potVal>=820&&potVal<1023)
  {
    digitalWrite(led1,off);
    digitalWrite(led2,off);
    digitalWrite(led3,dim);
    digitalWrite(led4,low);
    digitalWrite(led5,full);
  }
  //waits 100 ms
  delay(500);
}//end loop

Final Product Video

Arduino期末專題

This is my final product.