Components and supplies
Resistor 330 ohm
Through Hole Resistor, 200 ohm
5V 2.5A Switching Power Supply
Jumper wires (generic)
Breadboard (generic)
Tactile Switch, Top Actuated
5 mm LED: Green
RGB Diffused Common Cathode
Through Hole Resistor, 40 kohm
Arduino Nano R3
Project description
Code
Arduino code
arduino
Includes 3 animations - you can switch through them with the button (interrupts + debouncing).
1// Simple Arduino Christmas Tree by bzqp, 2021: https://www.youtube.com/c/bzqp1 2volatile 3 int flag=0; // flag to keep track of the current animation 4int old_flag=0; // 5 old flag value to break the animation correctly 6unsigned long interrupt_time=0; 7 // for switch debouncing 8unsigned long last_interrupt_time=0;// for switch debouncing 9int 10 r=20; // initial red value 11int g=4; // initial green value 12int b=0; // initial 13 blue value 14int speed=6; // speed of the swipe animation 15 16void setup() { 17 18 pinMode(2, INPUT); //interrupt pin 19 pinMode(A4, OUTPUT); //5V power for the 20 switch 21 digitalWrite(A4, HIGH); //power the awitch 22 attachInterrupt(digitalPinToInterrupt(2),flagchange,RISING); 23 //interrupt attached to the button 24 pinMode(3, OUTPUT); //green 25 pinMode(5, 26 OUTPUT); //blue 27 pinMode(6, OUTPUT); //red 28 pinMode(9, OUTPUT); //green 29 30 pinMode(10, OUTPUT); //blue 31 pinMode(11, OUTPUT); //red 32} 33 34void loop() 35 { 36 // Night mode animation: static, dims the RGB lights to match the green LEDs 37 38 if(flag==0){ 39 delay(100); 40 r=20; 41 g=4; 42 b=0; 43 44 analogWrite(6, 45 r); 46 analogWrite(3, g); 47 analogWrite(5, b); 48 49 analogWrite(11, r); 50 51 analogWrite(9, g); 52 analogWrite(10, b); 53 } 54 // Swipe animation 55 - swipes through different colors. Lots of while loops and some break functions 56 to detect the interrupts 57 if(flag==1){ 58 old_flag=int(flag); 59 r=80; 60 61 g=16; 62 b=0; 63 64 65 while(r>0){ 66 if(flag!=old_flag){ 67 break; 68 69 } 70 r--; 71 analogWrite(6, r); 72 analogWrite(3, g); 73 analogWrite(5, 74 b); 75 76 analogWrite(11, r); 77 analogWrite(9, g); 78 analogWrite(10, 79 b); 80 delay(speed); 81 82 } 83 while(g<255){ 84 if(flag!=old_flag){ 85 86 break; 87 } 88 g++; 89 analogWrite(6, r); 90 analogWrite(3, g); 91 92 analogWrite(5, b); 93 94 analogWrite(11, r); 95 analogWrite(9, g); 96 97 analogWrite(10, b); 98 delay(speed); 99 } 100 while(b<255){ 101 if(flag!=old_flag){ 102 103 break; 104 } 105 b++; 106 analogWrite(6, r); 107 analogWrite(3, g); 108 109 analogWrite(5, b); 110 111 analogWrite(11, r); 112 analogWrite(9, g); 113 114 analogWrite(10, b); 115 delay(speed); 116 } 117 while(g>0){ 118 if(flag!=old_flag){ 119 120 break; 121 } 122 g--; 123 analogWrite(6, r); 124 analogWrite(3, g); 125 126 analogWrite(5, b); 127 128 analogWrite(11, r); 129 analogWrite(9, g); 130 131 analogWrite(10, b); 132 delay(speed); 133 } 134 while(r<255){ 135 if(flag!=old_flag){ 136 137 break; 138 } 139 r++; 140 analogWrite(6, r); 141 analogWrite(3, g); 142 143 analogWrite(5, b); 144 145 analogWrite(11, r); 146 analogWrite(9, g); 147 148 analogWrite(10, b); 149 delay(speed); 150 } 151 while(b>0){ 152 if(flag!=old_flag){ 153 154 break; 155 } 156 b--; 157 analogWrite(6, r); 158 analogWrite(3, g); 159 160 analogWrite(5, b); 161 162 analogWrite(11, r); 163 analogWrite(9, g); 164 165 analogWrite(10, b); 166 delay(speed); 167 } 168 delay(speed*50); 169 while(g<255){ 170 171 if(flag!=old_flag){ 172 break; 173 } 174 g++; 175 analogWrite(6, 176 r); 177 analogWrite(3, g); 178 analogWrite(5, b); 179 180 analogWrite(11, r); 181 182 analogWrite(9, g); 183 analogWrite(10, b); 184 delay(speed); 185 } 186 while(r>0){ 187 188 if(flag!=old_flag){ 189 break; 190 } 191 r--; 192 analogWrite(6, 193 r); 194 analogWrite(3, g); 195 analogWrite(5, b); 196 197 analogWrite(11, r); 198 199 analogWrite(9, g); 200 analogWrite(10, b); 201 delay(speed); 202 } 203 while(g>16){ 204 205 g--; 206 analogWrite(6, r); 207 analogWrite(3, g); 208 analogWrite(5, b); 209 210 211 analogWrite(11, r); 212 analogWrite(9, g); 213 analogWrite(10, b); 214 215 delay(speed); 216 } 217 while(r<80){ 218 if(flag!=old_flag){ 219 220 break; 221 } 222 r++; 223 analogWrite(6, r); 224 analogWrite(3, g); 225 226 analogWrite(5, b); 227 228 analogWrite(11, r); 229 analogWrite(9, g); 230 231 analogWrite(10, b); 232 delay(speed); 233 } 234 r=80; 235 g=16; 236 b=0; 237 238 239 analogWrite(6, r); 240 analogWrite(3, g); 241 analogWrite(5, b); 242 243 analogWrite(11, 244 r); 245 analogWrite(9, g); 246 analogWrite(10, b); 247 for(int i=0;i<speed*2;i++){ 248 249 if(flag!=old_flag){ 250 break; 251 } 252 delay(500); 253 } 254 255 256 } 257 // Fire flicker animation. My personal favourite 258 if (flag==2){ 259 260 261 analogWrite(6, random(50,255)); 262 delay(random(0,40)); 263 analogWrite(3, 264 random(0,8)); 265 delay(random(0,40)); 266 analogWrite(5, random(0,3)); 267 delay(random(0,40)); 268 269 270 analogWrite(11, random(50,255)); 271 delay(random(0,40)); 272 analogWrite(9, 273 random(0,8)); 274 delay(random(0,40)); 275 analogWrite(10, random(0,3)); 276 delay(random(0,40)); 277 278 279 } 280} 281// interrupt to detect the switch press 282void flagchange(){ 283 284 last_interrupt_time=interrupt_time; 285 interrupt_time=millis(); 286 // interrupt 287 debouncer - stabilizes the button input 288 if(interrupt_time-last_interrupt_time>200){ 289 290 flag++; // flag stores the current animation state 291 if(flag>2){ 292 flag=0; 293 294 } 295 } 296} 297
Arduino code
arduino
Includes 3 animations - you can switch through them with the button (interrupts + debouncing).
1// Simple Arduino Christmas Tree by bzqp, 2021: https://www.youtube.com/c/bzqp1 2volatile int flag=0; // flag to keep track of the current animation 3int old_flag=0; // old flag value to break the animation correctly 4unsigned long interrupt_time=0; // for switch debouncing 5unsigned long last_interrupt_time=0;// for switch debouncing 6int r=20; // initial red value 7int g=4; // initial green value 8int b=0; // initial blue value 9int speed=6; // speed of the swipe animation 10 11void setup() { 12 pinMode(2, INPUT); //interrupt pin 13 pinMode(A4, OUTPUT); //5V power for the switch 14 digitalWrite(A4, HIGH); //power the awitch 15 attachInterrupt(digitalPinToInterrupt(2),flagchange,RISING); //interrupt attached to the button 16 pinMode(3, OUTPUT); //green 17 pinMode(5, OUTPUT); //blue 18 pinMode(6, OUTPUT); //red 19 pinMode(9, OUTPUT); //green 20 pinMode(10, OUTPUT); //blue 21 pinMode(11, OUTPUT); //red 22} 23 24void loop() { 25 // Night mode animation: static, dims the RGB lights to match the green LEDs 26 if(flag==0){ 27 delay(100); 28 r=20; 29 g=4; 30 b=0; 31 32 analogWrite(6, r); 33 analogWrite(3, g); 34 analogWrite(5, b); 35 36 analogWrite(11, r); 37 analogWrite(9, g); 38 analogWrite(10, b); 39 } 40 // Swipe animation - swipes through different colors. Lots of while loops and some break functions to detect the interrupts 41 if(flag==1){ 42 old_flag=int(flag); 43 r=80; 44 g=16; 45 b=0; 46 47 48 while(r>0){ 49 if(flag!=old_flag){ 50 break; 51 } 52 r--; 53 analogWrite(6, r); 54 analogWrite(3, g); 55 analogWrite(5, b); 56 57 analogWrite(11, r); 58 analogWrite(9, g); 59 analogWrite(10, b); 60 delay(speed); 61 62 } 63 while(g<255){ 64 if(flag!=old_flag){ 65 break; 66 } 67 g++; 68 analogWrite(6, r); 69 analogWrite(3, g); 70 analogWrite(5, b); 71 72 analogWrite(11, r); 73 analogWrite(9, g); 74 analogWrite(10, b); 75 delay(speed); 76 } 77 while(b<255){ 78 if(flag!=old_flag){ 79 break; 80 } 81 b++; 82 analogWrite(6, r); 83 analogWrite(3, g); 84 analogWrite(5, b); 85 86 analogWrite(11, r); 87 analogWrite(9, g); 88 analogWrite(10, b); 89 delay(speed); 90 } 91 while(g>0){ 92 if(flag!=old_flag){ 93 break; 94 } 95 g--; 96 analogWrite(6, r); 97 analogWrite(3, g); 98 analogWrite(5, b); 99 100 analogWrite(11, r); 101 analogWrite(9, g); 102 analogWrite(10, b); 103 delay(speed); 104 } 105 while(r<255){ 106 if(flag!=old_flag){ 107 break; 108 } 109 r++; 110 analogWrite(6, r); 111 analogWrite(3, g); 112 analogWrite(5, b); 113 114 analogWrite(11, r); 115 analogWrite(9, g); 116 analogWrite(10, b); 117 delay(speed); 118 } 119 while(b>0){ 120 if(flag!=old_flag){ 121 break; 122 } 123 b--; 124 analogWrite(6, r); 125 analogWrite(3, g); 126 analogWrite(5, b); 127 128 analogWrite(11, r); 129 analogWrite(9, g); 130 analogWrite(10, b); 131 delay(speed); 132 } 133 delay(speed*50); 134 while(g<255){ 135 if(flag!=old_flag){ 136 break; 137 } 138 g++; 139 analogWrite(6, r); 140 analogWrite(3, g); 141 analogWrite(5, b); 142 143 analogWrite(11, r); 144 analogWrite(9, g); 145 analogWrite(10, b); 146 delay(speed); 147 } 148 while(r>0){ 149 if(flag!=old_flag){ 150 break; 151 } 152 r--; 153 analogWrite(6, r); 154 analogWrite(3, g); 155 analogWrite(5, b); 156 157 analogWrite(11, r); 158 analogWrite(9, g); 159 analogWrite(10, b); 160 delay(speed); 161 } 162 while(g>16){ 163 g--; 164 analogWrite(6, r); 165 analogWrite(3, g); 166 analogWrite(5, b); 167 168 analogWrite(11, r); 169 analogWrite(9, g); 170 analogWrite(10, b); 171 delay(speed); 172 } 173 while(r<80){ 174 if(flag!=old_flag){ 175 break; 176 } 177 r++; 178 analogWrite(6, r); 179 analogWrite(3, g); 180 analogWrite(5, b); 181 182 analogWrite(11, r); 183 analogWrite(9, g); 184 analogWrite(10, b); 185 delay(speed); 186 } 187 r=80; 188 g=16; 189 b=0; 190 191 analogWrite(6, r); 192 analogWrite(3, g); 193 analogWrite(5, b); 194 195 analogWrite(11, r); 196 analogWrite(9, g); 197 analogWrite(10, b); 198 for(int i=0;i<speed*2;i++){ 199 if(flag!=old_flag){ 200 break; 201 } 202 delay(500); 203 } 204 205 } 206 // Fire flicker animation. My personal favourite 207 if (flag==2){ 208 209 analogWrite(6, random(50,255)); 210 delay(random(0,40)); 211 analogWrite(3, random(0,8)); 212 delay(random(0,40)); 213 analogWrite(5, random(0,3)); 214 delay(random(0,40)); 215 216 analogWrite(11, random(50,255)); 217 delay(random(0,40)); 218 analogWrite(9, random(0,8)); 219 delay(random(0,40)); 220 analogWrite(10, random(0,3)); 221 delay(random(0,40)); 222 223 } 224} 225// interrupt to detect the switch press 226void flagchange(){ 227 last_interrupt_time=interrupt_time; 228 interrupt_time=millis(); 229 // interrupt debouncer - stabilizes the button input 230 if(interrupt_time-last_interrupt_time>200){ 231 flag++; // flag stores the current animation state 232 if(flag>2){ 233 flag=0; 234 } 235 } 236} 237
Downloadable files
Wiring diagram
Wiring diagram
Wiring diagram
Comments
Only logged in users can leave comments
bzqp
0 Followers
•0 Projects
Table of contents
Intro
1
0