Components and supplies
40 colored male-male jumper wires
Arduino Plug and Make Kit
DC5V WS2811 LED Chain
Arduino® UNO R4 WiFi
Apps and platforms
Arduino Cloud
Project description
Code
Interactive Holiday Lights
cpp
This code uses Arduino Cloud
1/* 2This code shows a interactive christmas light using addressable RGBs (WS2811) and the Arduino Modulino Buttons and Distance as input. 3 4Modulino Buttons: 5- Choose between three animations (A,B,C). 6 7Modulino Distance: 8- Change the animation speed when someone approaches 9 10Animations are taken from Adafruit_Neopixel library 11https://github.com/adafruit/Adafruit_NeoPixel/blob/master/examples/RGBWstrandtest/RGBWstrandtest.ino 12 13Initial author: Hannes Siebeneicher (h.siebeneicher@arduino.cc) 14*/ 15 16// Libraries 17#include <Modulino.h> 18#include <Adafruit_NeoPixel.h> 19#include "thingProperties.h" 20#include <Arduino_LED_Matrix.h> 21#include <Arduino_CloudConnectionFeedback.h> 22 23// Which pin on the Arduino is connected to the NeoPixels? 24#define LED_PIN 6 25 26// How many NeoPixels are attached to the Arduino? 27#define LED_COUNT 50 28 29// Distance responsible for triggering the animation 30#define DISTANCE 1000 // Change this value according to your preference 31 32// Declare our NeoPixel strip object with correct color order: 33Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800); 34 35// Matrix Object 36ArduinoLEDMatrix matrix; 37 38// Modulino Objects 39ModulinoButtons buttons; 40ModulinoDistance distance; 41 42unsigned long timePassed = 0; // Tracking passed time for cooldown 43unsigned long cooldown = 5000; // Cooldown in ms for the animation 44 45bool hasPlayed; // Tack if the animation has just played 46uint8_t r, g, b; // Variables to store the color 47 48void setup() { 49 // Initialize serial and wait for port to open: 50 Serial.begin(9600); 51 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 52 delay(1500); 53 54 // Defined in thingProperties.h 55 initProperties(); 56 57 // Initialize everything 58 matrix.begin(); 59 Modulino.begin(); 60 buttons.begin(); 61 distance.begin(); 62 strip.begin(); 63 strip.show(); 64 65 // Connect to Arduino IoT Cloud 66 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 67 waitForArduinoCloudConnection(matrix); 68 setDebugMessageLevel(2); 69 ArduinoCloud.printDebugInfo(); 70 71 triggerAnimation = false; // Make sure the animation is not running. 72 animation = 1; // Set default animation for cloud variable. 73 hasPlayed = false; 74 75 // Start by turning the LEDs off. 76 setAnimation(0); 77} 78 79/* 80This function loops through all RGBs and sets the color. 81@param uint8_t r 82@param uint8_t g 83@param uint8_t b 84*/ 85void setStrip(uint8_t r, uint8_t g, uint8_t b) { 86 for (int i = 0; i < strip.numPixels(); i++) { 87 strip.setPixelColor(i, strip.Color(r, g, b)); 88 } 89 strip.show(); 90} 91 92/* 93 This function sets which temperature startAnimation should play 94 @ param int animation 95*/ 96int setAnimation(int newAnimation) { 97 animation = newAnimation; 98 updateButtonLeds(newAnimation); 99} 100 101/* 102This function updates the ButtonLeds 103*/ 104void updateButtonLeds(int anim) { 105 switch (anim) { 106 case 1: 107 buttons.setLeds(true, false, false); 108 break; 109 case 2: 110 buttons.setLeds(false, true, false); 111 break; 112 case 3: 113 buttons.setLeds(false, false, true); 114 break; 115 } 116} 117 118/* 119 This function starts the cooldown timer so the animation only plays once. 120*/ 121bool startCooldown() { 122 unsigned long currentTime = millis(); 123 if ((currentTime - timePassed) > cooldown) { 124 timePassed = currentTime; 125 hasPlayed = false; 126 triggerAnimation = false; 127 return true; 128 } else { 129 return false; 130 } 131} 132 133 /* 134 This function starts the animation based on which one is set. 135*/ 136 void startAnimation() { 137 138 // Exit early cooldown hasn't reset the flag 139 if (hasPlayed) { 140 return; 141 } 142 143 // manually set the cloud variable 144 triggerAnimation = true; 145 146 // Update the cloud before the animation 147 ArduinoCloud.update(); 148 149 switch (animation) { 150 case 1: 151 rainbowFade2White(3, 3, 1); 152 break; 153 case 2: 154 whiteOverRainbow(75, 5); 155 break; 156 case 3: 157 colorWipe(strip.Color(255, 0, 0) , 50); // Red 158 colorWipe(strip.Color( 0, 255, 0) , 50); // Green 159 colorWipe(strip.Color( 0, 0, 255) , 50); // Blue 160 colorWipe(strip.Color( 0, 0, 0, 255), 50); // True white (not RGB white) 161 break; 162 } 163 164 // Go back to standard color 165 setStrip(r, g, b); 166 hasPlayed = true; // Mark the animation as played. 167 triggerAnimation = false; // turn the switch back to off once the animation is finished 168 ArduinoCloud.update(); // Update the cloud after the animation 169 timePassed = millis(); // Set start time for the cooldown cycle. 170 } 171 172/* 173 Animation 1 174 This function creates a rainbow animation and displays it on the RGB LEDs 175 @ param int wait 176 @ param int rainbowLoops 177 @ param int whiteLoops 178*/ 179void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) { 180 int fadeVal=0, fadeMax=100; 181 182 // Hue of first pixel runs 'rainbowLoops' complete loops through the color 183 // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so 184 // just count from 0 to rainbowLoops*65536, using steps of 256 so we 185 // advance around the wheel at a decent clip. 186 for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536; 187 firstPixelHue += 256) { 188 189 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... 190 191 // Offset pixel hue by an amount to make one full revolution of the 192 // color wheel (range of 65536) along the length of the strip 193 // (strip.numPixels() steps): 194 uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); 195 196 // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or 197 // optionally add saturation and value (brightness) (each 0 to 255). 198 // Here we're using just the three-argument variant, though the 199 // second value (saturation) is a constant 255. 200 strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255, 201 255 * fadeVal / fadeMax))); 202 } 203 204 strip.show(); 205 delay(wait); 206 207 if(firstPixelHue < 65536) { // First loop, 208 if(fadeVal < fadeMax) fadeVal++; // fade in 209 } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop, 210 if(fadeVal > 0) fadeVal--; // fade out 211 } else { 212 fadeVal = fadeMax; // Interim loop, make sure fade is at max 213 } 214 } 215} 216 217/* 218 Animation 2 219 This function creates another rainbow animation and displays it on the RGB LEDs. 220 @ param int whiteSpeed 221 @ param int whiteLength 222*/ 223void whiteOverRainbow(int whiteSpeed, int whiteLength) { 224 if (whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1; 225 226 int head = whiteLength - 1; 227 int tail = 0; 228 int loops = 3; 229 int loopNum = 0; 230 uint32_t lastTime = millis(); 231 uint32_t firstPixelHue = 0; 232 233 while(loopNum < loops) { 234 for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip... 235 if (((i >= tail) && (i <= head)) || // If between head & tail... 236 ((tail > head) && ((i >= tail) || (i <= head)))) { 237 strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white 238 } else { // else set rainbow 239 int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); 240 strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); 241 } 242 } 243 strip.show(); // Update strip with new contents 244 245 firstPixelHue += 40; // Advance just a little along the color wheel 246 247 uint32_t currentTime = millis(); 248 if ((currentTime - lastTime) > whiteSpeed) { // Time to update head/tail? 249 if (++head >= strip.numPixels()) { // Advance head, wrap around 250 head = 0; 251 if (++loopNum >= loops) break; // Completes loop 252 } 253 if (++tail >= strip.numPixels()) { // Advance tail, wrap around 254 tail = 0; 255 } 256 lastTime = currentTime; // Save time of last movement 257 } 258 } 259 260 // Clear the strip to ensure nothing is left on 261 strip.clear(); 262 strip.show(); 263} 264 265/* 266 Animation 3 267 This function creates another rainbow animation and displays it on the RGB LEDs. 268 @ param int speed 269 @ param int trailLength 270*/ 271void colorWipe(uint32_t color, int wait) { 272 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip... 273 strip.setPixelColor(i, color); // Set pixel's color (in RAM) 274 strip.show(); // Update strip to match 275 delay(wait); // Pause for a moment 276 } 277} 278 279void loop() { 280 281 ArduinoCloud.update(); 282 283 // Continiously check distance 284 if (distance.available()) { 285 int measure = distance.get(); 286 if (measure > 0 && measure <= DISTANCE && !hasPlayed) { 287 startAnimation(); 288 } 289 } 290 291 startCooldown(); 292 293 // Check Button input 294 if (buttons.update()) { 295 //Check if the buttons has been pressed 296 if (buttons.isPressed(0)) { 297 setAnimation(1); 298 } else if (buttons.isPressed(1)) { 299 setAnimation(2); 300 } else if (buttons.isPressed(2)) { 301 setAnimation(3); 302 } 303 } 304 delay(10); // a short delay for stability 305} 306 307/* 308 Since TriggerAnimation is READ_WRITE variable, onTriggerAnimationChange() is 309 executed every time a new value is received from IoT Cloud. 310*/ 311void onTriggerAnimationChange() { 312 if (hasPlayed) { 313 triggerAnimation = false; 314 return; 315 } 316 startAnimation(); 317} 318 319/* 320 Since LedColorWheel is READ_WRITE variable, onLedColorWheelChange() is 321 executed every time a new value is received from IoT Cloud. 322*/ 323void onLedColorWheelChange() { 324 325 // Get colors from dashboard widget 326 ledColorWheel.getValue().getRGB(r, g, b); 327 setStrip(r, g, b); // Apply the colors 328 329 // Then check if switch is on/off 330 if (ledColorWheel.getSwitch()) { 331 setStrip(r, g, b); 332 } else { 333 setStrip(0, 0, 0); 334 } 335} 336 337 338/* 339 Since Animation is READ_WRITE variable, onAnimationChange() is 340 executed every time a new value is received from IoT Cloud. 341*/ 342void onAnimationChange() { 343 updateButtonLeds(animation); 344}
Downloadable files
Scematics
2c20d874-cde9-402e-b0b5-3d2eae6065ea.png
Comments
Only logged in users can leave comments