Skip to content


How to do a mood lamp using Arduino

This was my first Arduino project so I decided to use only LEDs :)

Being Christmas I wanted to do something nice to look at night so I looked on my electronic parts box and found 3 different color LEDs : red, green and blue. I soldered their cathodes and then took 10 inches of common network cable and soldered its wires on LEDs anodes and their common cathode. This is how I build the “RGB LED”. It was a pretty ok solution though the colors aren’t perfect.

dscf3295

Below is the code for it (it has a lot of comments).
Basically the only settings you need to do are :
1) set the output ports on the “int leds[] = {11,10,9};” line
2) set the delay for the LEDs on the “int delaySec = 100;” line

  1. //setup our used ports (where we have the LEDs), put them in the right order!!! Which is RGB!
  2. //            R  G  B
  3. int leds[] = {11,10,9};
  4.  
  5. //the values for our LEDs, in RGB order
  6. //              R  G  B
  7. int values[] ={255,0,0};
  8. //we need this just to reset it to the current state
  9. int defValues[] ={255,0,0};
  10.  
  11. //this is our counter
  12. int cnt = -1;
  13.  
  14. //set here the delay (in milliseconds)
  15. int delaySec = 100;
  16.  
  17. //setup function, runs only once when the board is initialized
  18. void setup(){
  19.   //set our ports for output (so we have power to light up the LEDs)
  20.   for (int i=0;i<3;i++){
  21.     pinMode(leds[i], OUTPUT);
  22.   }
  23.   //call our test function
  24.   test();
  25. }
  26.  
  27.  
  28. //function to test if all our LEDs are ok
  29. void test(){
  30.   //keep them a second on and then turn them off
  31.   for (int i=0;i<3;i++){
  32.     digitalWrite(leds[i], HIGH);
  33.     delay(1000);
  34.     digitalWrite(leds[i], LOW);
  35.   }
  36.   //test them fading too
  37.   for (int i=0;i<3;i++){
  38.     for (int j=0;j<256;j++){
  39.         analogWrite(leds[i], j);
  40.         delay(10);
  41.     }
  42.     digitalWrite(leds[i], LOW);
  43.   }
  44.   delay(3000);
  45. }
  46.  
  47.  
  48. //this function is looping for the whole execution
  49. //the login go from Red->Green, Green->Blue, Blue->Red
  50. void loop(){
  51.  
  52.   //increase our counter
  53.   cnt++;
  54.  
  55.   //take decisions
  56.   if (cnt < 254){
  57.     values[0]--;
  58.     values[1]++;
  59.     values[2] = 0;
  60.   }
  61.        
  62.   if ((cnt > 253) && (cnt < 509)){
  63.     values[0] = 0;
  64.     values[1]--;
  65.     values[2]++;
  66.   }
  67.  
  68.   if ((cnt > 508) && (cnt < 763)){
  69.     values[0]++;
  70.     values[1]=0;
  71.     values[2]--;
  72.   }
  73.  
  74.   if (cnt > 762){
  75.     values[0] = 255;
  76.     values[1] = 0;
  77.     values[2] = 0;
  78.     cnt = -1;
  79.   }
  80.  
  81.   cnt++;
  82.  
  83.   //change colors
  84.   for (int i=0;i<3;i++){
  85.     analogWrite(leds[i], values[i]);
  86.     delay(delaySec);
  87.   }
  88.  
  89. }

... and a small demo (on super fast mode) of the lamp running the above code ...

Hope you enjoy this 15 minutes project :)

Posted in Arduino.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Ori says

    Thanks for sharing your code and explanation!



Some HTML is OK

or, reply to this post via trackback.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes