World of Electronics

Use Your RGB Led Strip to Create a SMART Christmas Light Tree! Full Tutorial Code.

For this tutorial, you’ll need:

  1. 1 x Arduino UNO
  2. 3 x Resistor 330E
  3. 3 x BDW93C
  4. Jumper Wires
  5. 1x RGB Strip LED
  6. 1 x Adaptor 12V 5A

Watch this full video:

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

Smart Christmas Tree Example Code:

 const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
 unsigned int sample;
 

 int color_levels[]= {0, 50, 100, 150, 200, 250}; // length = 5
 

 #define red 9
 #define green 10
 #define blue 11
 

 double volts1=0;
 

 int rCL =0; //random red color level
 int gCL =0; //random red color level
 int bCL =0; //random red color level
  
 void setup() 
 {
    Serial.begin(9600);
 

    pinMode(red, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(blue, OUTPUT);
 

    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
    digitalWrite(blue, LOW);             
 }
  
  
 void loop() 
 {
    unsigned long startMillis= millis();  // Start of sample window
    unsigned int peakToPeak = 0;   // peak-to-peak level
  
    unsigned int signalMax = 0;
    unsigned int signalMin = 1024;
  
    // collect data for 50 mS
    while (millis() - startMillis < sampleWindow)
    {
       sample = analogRead(A0);
       if (sample < 1024)  // toss out spurious readings
       {
          if (sample > signalMax)
          {
             signalMax = sample;  // save just the max levels
          }
          else if (sample < signalMin)
          {
             signalMin = sample;  // save just the min levels
          }
       }
    }
    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
    double volts = (peakToPeak * 5.0) / 1024;  // convert to volts
  
    Serial.println(volts);
 

    if (volts<0.4)
    {
       digitalWrite(red, 0);
       digitalWrite(green, 0);
       digitalWrite(blue, 0);
    }
    else
    {
     double a = volts1 +0.1;
     double b = volts1 -0.1;
 

 

 

    if((volts>a) || (volts<b))
    {
 

     rCL = random(0,6);
     gCL = random(0,6);
     bCL = random(0,6);
 

     digitalWrite(red, color_levels[rCL]);
     digitalWrite(green, color_levels[gCL]);
     digitalWrite(blue, color_levels[bCL]);
 

    }
    }
 

   delay(100);
 

 

    volts1 = volts;
    
    
 }