bzqp
Published © CC BY-NC

Simple Christmas Tree

Still don't have a Christmas Tree? Don't panic! Grab an Arduino, some jumper wires and let's do it!!

BeginnerFull instructions provided53
Simple Christmas Tree

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Jumper wires (generic)
Jumper wires (generic)
Best to use green cables for power and brown for ground.
×26
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×4
5 mm LED: Green
5 mm LED: Green
×18
Breadboard (generic)
Breadboard (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×6
Through Hole Resistor, 200 ohm
Through Hole Resistor, 200 ohm
×10
Through Hole Resistor, 40 kohm
Through Hole Resistor, 40 kohm
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×1

Story

Read more

Schematics

Wiring diagram

Wiring diagram

Code

Arduino code

Arduino
Includes 3 animations - you can switch through them with the button (interrupts + debouncing).
// Simple Arduino Christmas Tree by bzqp, 2021: https://www.youtube.com/c/bzqp1
volatile int flag=0; // flag to keep track of the current animation
int old_flag=0; // old flag value to break the animation correctly
unsigned long interrupt_time=0; // for switch debouncing
unsigned long last_interrupt_time=0;// for switch debouncing
int r=20; // initial red value
int g=4; // initial green value
int b=0; // initial blue value
int speed=6; // speed of the swipe animation

void setup() {
  pinMode(2, INPUT); //interrupt pin
  pinMode(A4, OUTPUT); //5V power for the switch
  digitalWrite(A4, HIGH); //power the awitch
  attachInterrupt(digitalPinToInterrupt(2),flagchange,RISING); //interrupt attached to the button
  pinMode(3, OUTPUT); //green
  pinMode(5, OUTPUT); //blue
  pinMode(6, OUTPUT); //red
  pinMode(9, OUTPUT); //green
  pinMode(10, OUTPUT); //blue
  pinMode(11, OUTPUT); //red
}

void loop() {
  // Night mode animation: static, dims the RGB lights to match the green LEDs
  if(flag==0){
  delay(100);
  r=20;
  g=4;
  b=0;
  
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  }     
  // Swipe animation - swipes through different colors. Lots of while loops and some break functions to detect the interrupts
  if(flag==1){
  old_flag=int(flag);
  r=80;
  g=16;
  b=0;
 
  
  while(r>0){
    if(flag!=old_flag){
      break;
    }
    r--;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);

  }
    while(g<255){
          if(flag!=old_flag){
      break;
    }
    g++;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
    while(b<255){
          if(flag!=old_flag){
      break;
    }
    b++;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
    while(g>0){
          if(flag!=old_flag){
      break;
    }
    g--;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
    while(r<255){
          if(flag!=old_flag){
      break;
    }
    r++;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
    while(b>0){
          if(flag!=old_flag){
      break;
    }
    b--;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
  delay(speed*50);
    while(g<255){
          if(flag!=old_flag){
      break;
    }
    g++;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
      while(r>0){
            if(flag!=old_flag){
      break;
    }
    r--;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }  
    while(g>16){
    g--;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }
    while(r<80){
          if(flag!=old_flag){
      break;
    }
    r++;
  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  delay(speed);
  }  
  r=80;
  g=16;
  b=0;

  analogWrite(6, r);
  analogWrite(3, g);
  analogWrite(5, b);
  
  analogWrite(11, r);
  analogWrite(9, g);
  analogWrite(10, b);
  for(int i=0;i<speed*2;i++){
        if(flag!=old_flag){
      break;
    }
  delay(500);
  }
  
  }
  // Fire flicker animation. My personal favourite
  if (flag==2){

  analogWrite(6, random(50,255));
  delay(random(0,40));
  analogWrite(3, random(0,8));
  delay(random(0,40));
  analogWrite(5, random(0,3));
  delay(random(0,40));
  
  analogWrite(11, random(50,255));
  delay(random(0,40));
  analogWrite(9, random(0,8));
  delay(random(0,40));
  analogWrite(10, random(0,3));
  delay(random(0,40));
        
  }
}
// interrupt to detect the switch press
void flagchange(){
  last_interrupt_time=interrupt_time;
  interrupt_time=millis();
  // interrupt debouncer - stabilizes the button input
    if(interrupt_time-last_interrupt_time>200){
  flag++; // flag stores the current animation state
  if(flag>2){
    flag=0;
  }
  }
}

Credits

bzqp

bzqp

4 projects • 7 followers

Comments

Add projectSign up / Login