Amazing Light Effect Using RGB Light and Glue Stick
An interesting light display using 16 glue sticks and 32 WS2812B RGB LED strips.This lamp was based on a similar lamp called Hot Glue LED Matrix Lamp. the goal was to replace the 12mm Diffused Digital RGB LED used in jbumstead’s design with cheap WS2812B RGB strips and to make the container for the LEDs.
Components List:
- WS2812B LED Strip: https://amzn.to/3j0Tbv5
- Glue Sticks : https://amzn.to/35Z1XoB
- Plastic Box : You will get from local electronics shop
- 10K Pot : https://amzn.to/2TIAGke
- Mini Push Switch : https://amzn.to/34NklkC
- Arduino Nano : https://amzn.to/35Z2rep
- SPST Switch : https://amzn.to/3ef0kqu
Connection Diagram :
Code:
Main Page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
#include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> #endif #include "Button.h" #define PIN_LED 2 #define PIN_SWITCH 3 #define PIN_POT A0 #define LEDS 32 // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN_LED, NEO_GRB + NEO_KHZ800); Button mode = Button(PIN_SWITCH); bool modePressed = false; //Physical LED map for bottom and top LED arrays const uint8_t botLED[] PROGMEM = { 16,9,8,1, 15,10,7,2, 14,11,6,3, 13,12,5,4, }; const uint8_t topLED[] PROGMEM = { 17,24,25,32, 18,23,26,31, 19,22,27,30, 20,21,28,29, }; //Storage for current values int red = 128; int green = 128; int blue = 128; int pattern = 1; // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input // and minimize distance between Arduino and first pixel. Avoid connecting // on a live circuit...if you must, connect GND first. void setup() { Serial.begin(115200); pinMode(PIN_LED, OUTPUT); pinMode(PIN_SWITCH, INPUT); pinMode(PIN_POT, INPUT); //Pixel Strip Serial.println("Setup()"); strip.begin(); strip.show(); // Initialize all pixels to 'off' //Button callbacks //mode.Background(ButtonBackground); //Set ISR for pin change on MODE pin Button::PinChangeSetup(PIN_SWITCH); } void loop() { if (modePressed) { pattern = (pattern % 8) + 1; strip.clear(); } modePressed = false; Serial.print("Mode "); Serial.print(pattern, DEC); Serial.println(); switch (pattern) { case 1: colorWipe(strip.Color(255, 0, 0)); break; // Red case 2: colorWipe(strip.Color(0, 255, 0)); break; // Green case 3: colorWipe(strip.Color(0, 0, 255)); break; // Blue case 4: theaterChase(strip.Color(127, 127, 127)); break; // White case 5: rainbow(); break; case 6: rainbowDifference(); break; case 7: rainbowCycle(); break; case 8: rainbowCycleDifference(); break; case 9: theaterChaseRainbow(); break; } if (!modePressed) { modePressed = mode.Pressed(); } } //Mode button interrupt to break out of loops etc //PCINT1 handles pin changes for pins for A0 to A5 ISR (PCINT2_vect) { modePressed = modePressed | (mode.State() == LOW); } void ButtonBackground(void) { } // Fill the dots one after the other with a color void colorWipe(uint32_t c) { int total = strip.numPixels() / 2; for(uint16_t i=0; i < total && !modePressed; i++) { uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, c); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, c); strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 100, 0)); } for(uint16_t i=total; i > 0 && !modePressed; i--) { uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, 0); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, 0); strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 100, 0)); } } void rainbow() { int total = strip.numPixels() / 2; for(uint16_t j=0; j < 256 && !modePressed; j++) { for(uint16_t i=0; i < total && !modePressed; i++) { uint32_t c = Wheel((i+j) & 255); uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, c); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 40, 0)); } } void rainbowDifference() { int total = strip.numPixels() / 2; for(uint16_t j=0; j < 256 && !modePressed; j++) { for(uint16_t i=0; i < total && !modePressed; i++) { uint32_t c = Wheel((i+j) & 255); uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, c); c = Wheel((i+j+64) & 255); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 40, 0)); } } // Slightly different, this makes the rainbow equally distributed throughout void rainbowCycle() { int total = strip.numPixels() / 2; for(uint16_t j=0; j < 256*5 && !modePressed; j++) { // 5 cycles of all colors on wheel for(uint16_t i=0; i < total && !modePressed; i++) { uint32_t c = Wheel(((i * 256 / total) + j) & 255); uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, c); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 40, 0)); } } // Slightly different, this makes the rainbow equally distributed throughout void rainbowCycleDifference() { int total = strip.numPixels() / 2; for(uint16_t j=0; j < 256*5 && !modePressed; j++) { // 5 cycles of all colors on wheel for(uint16_t i=0; i < total && !modePressed; i++) { uint32_t c = Wheel(((i * 256 / total) + j) & 255); uint8_t botIndex = pgm_read_byte(&botLED[i]) - 1; strip.setPixelColor(botIndex, c); c = Wheel(((i * 256 / total) + j + 64) & 255); uint8_t topIndex = pgm_read_byte(&topLED[i]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 40, 0)); } } //Theatre-style crawling lights. void theaterChase(uint32_t c) { int total = strip.numPixels() / 2; for (int j=0; j < 10 && !modePressed; j++) { //do 10 cycles of chasing for (int q=0; q < 3 && !modePressed; q++) { for (uint16_t i=0; i < total && !modePressed; i=i+3) { uint8_t botIndex = pgm_read_byte(&botLED[i+q]) - 1; //turn every third pixel on strip.setPixelColor(botIndex, c); uint8_t topIndex = pgm_read_byte(&topLED[i+q]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 1, 150)); for (uint16_t i=0; i < total && !modePressed; i=i+3) { uint8_t botIndex = pgm_read_byte(&botLED[i+q]) - 1; ////turn every third pixel off strip.setPixelColor(botIndex, 0); uint8_t topIndex = pgm_read_byte(&topLED[i+q]) - 1; strip.setPixelColor(topIndex, 0); } } } } //Theatre-style crawling lights with rainbow effect void theaterChaseRainbow() { int total = strip.numPixels() / 2; for (int j=0; j < 256 && !modePressed; j++) { // cycle all 256 colors in the wheel for (int q=0; q < 3 && !modePressed; q++) { for (uint16_t i=0; i < total && !modePressed; i=i+3) { uint32_t c = Wheel( (i+j) % 255); uint8_t botIndex = pgm_read_byte(&botLED[i+q]) - 1; //turn every third pixel on strip.setPixelColor(botIndex, c); uint8_t topIndex = pgm_read_byte(&topLED[i+q]) - 1; strip.setPixelColor(topIndex, c); } strip.show(); delay(map(analogRead(PIN_POT), 0, 1024, 1, 150)); for (uint16_t i=0; i < total && !modePressed; i=i+3) { uint8_t botIndex = pgm_read_byte(&botLED[i+q]) - 1; ////turn every third pixel off strip.setPixelColor(botIndex, 0); uint8_t topIndex = pgm_read_byte(&topLED[i+q]) - 1; strip.setPixelColor(topIndex, 0); } } } } // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } |
Button.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Class: Button Author: John Bradnam ([email protected]) Purpose: Arduino library to handle buttons */ #pragma once #include "Arduino.h" #define DEBOUNCE_DELAY 5 //Repeat speed #define REPEAT_START_SPEED 500 #define REPEAT_INCREASE_SPEED 50 #define REPEAT_MAX_SPEED 50 class Button { public: //Simple constructor Button(int Pin); //Background function called when in a wait or repeat loop void Background(void (*pBackgroundFunction)()); //Repeat function called when button is pressed void Repeat(void (*pRepeatFunction)()); //Test whether button is pressed and released //Will call repeat function if one is provided bool Pressed(); //Return button state (HIGH or LOW) - LOW = Pressed int State(); //Pin Change Interrupt Setup //ISR (PCINT0_vect) pin change interrupt for D8 to D13 //ISR (PCINT1_vect) pin change interrupt for A0 to A5 //ISR (PCINT2_vect) pin change interrupt for D0 to D7 static void PinChangeSetup(byte pin); private: int _pin; void (*_repeatCallback)(void); void (*_backgroundCallback)(void); }; |
Button.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include "Button.h" Button::Button(int pin) { _pin = pin; pinMode(_pin, INPUT); } //Set function to invoke in a delay or repeat loop void Button::Background(void (*pBackgroundFunction)()) { _backgroundCallback = pBackgroundFunction; } //Set function to invoke if repeat system required void Button::Repeat(void (*pRepeatFunction)()) { _repeatCallback = pRepeatFunction; } static void Button::PinChangeSetup(byte pin) { *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group } //Tests if a button is pressed and released // returns true if the button was pressed and released // if repeat callback supplied, the callback is called while the key is pressed bool Button::Pressed() { bool pressed = false; if (digitalRead(_pin) == LOW) { unsigned long wait = millis() + DEBOUNCE_DELAY; while (millis() < wait) { if (_backgroundCallback != NULL) { _backgroundCallback(); } } if (digitalRead(_pin) == LOW) { //Set up for repeat loop if (_repeatCallback != NULL) { _repeatCallback(); } unsigned long speed = REPEAT_START_SPEED; unsigned long time = millis() + speed; while (digitalRead(_pin) == LOW) { if (_backgroundCallback != NULL) { _backgroundCallback(); } if (_repeatCallback != NULL && millis() >= time) { _repeatCallback(); unsigned long faster = speed - REPEAT_INCREASE_SPEED; if (faster >= REPEAT_MAX_SPEED) { speed = faster; } time = millis() + speed; } } pressed = true; } } return pressed; } //Return current button state int Button::State() { return digitalRead(_pin); } |