Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program for Smart LED tape. Button debounce (working) and responds to a contact closure on pin8
- // At power on, LEDS all on, colour solid. 4 pins output different colours
- // so one this one program can be used for any roundel.
- // Upon contact closure (5V n/o) momentary event, the following happens to all 4 colour pins:
- // LEDS all off / all on fast pulse (x2), with a LED chase effect to finish
- // Lights all on when this finishes, so roundel colour remains visible.
- //
- // 20/08/2020 tested stable, with 110k-Ohm resistor between GND and pin8
- // 24/08/2020 tested stable, with 4 extenstions of Cat5 (185M) between 12V PSU & 5V relay and Arduino & LED strip
- // 25/08/2020 V3_2: test code removed, leaving only code in use
- // 30/09/2020 V5: code replacement with ALL leds on and off now (instead of quick increment)
- // Timings adjusted for best responsiveness of flashes
- // 02/10/2020 V8: Light_Pulse slowly fades between 50% - 100% brightness in a loop, to add minor animation to the solid ON state
- // Added an interval timer, so a number of light pulses happen after an adjustable length of time of solid LEDs on
- //
- // by S.Arbuckle
- #include <FastLED.h>
- #define NUM_LEDS 38 //number of LEDs in the roundel strip
- #define NUM_STRIPS 3
- #define DATA_PIN_R 3
- #define DATA_PIN_G 5
- #define DATA_PIN_B 6
- #define DATA_PIN_P 9
- #define NUM_COLORS 3
- #define LED_TYPE WS2812B
- #define COLOR_ORDER RGB
- //define input pins
- const int PinIn = 8;
- const long interval = 15000; //interval at which to run Light_Pulse
- int i = 0;
- int j = 0;
- int brightness = 1;
- int fadeAmount = 2;
- int flashes = 1;
- int buttonState1 = 0;
- int lastButtonState = LOW; // the previous reading from the input pin
- unsigned long lastDebounceTime = 0; //the last time the output pin was toggled
- unsigned long debounceDelay = 200; //the debounce time; increase if the output flickers
- unsigned long previousMillis = 0; //store the last time interval timer was updated
- CRGB redLeds [NUM_LEDS];
- CRGB greenLeds [NUM_LEDS];
- CRGB blueLeds [NUM_LEDS];
- CRGB purpleLeds[NUM_LEDS];
- void setup()
- {
- // tell FastLED about the LED strip configuration
- FastLED.addLeds<LED_TYPE,DATA_PIN_R,COLOR_ORDER>(redLeds, NUM_LEDS);
- FastLED.addLeds<LED_TYPE,DATA_PIN_G,COLOR_ORDER>(greenLeds, NUM_LEDS);
- FastLED.addLeds<LED_TYPE,DATA_PIN_B,COLOR_ORDER>(blueLeds, NUM_LEDS);
- FastLED.addLeds<LED_TYPE,DATA_PIN_P,COLOR_ORDER>(purpleLeds, NUM_LEDS);
- pinMode(PinIn,INPUT);
- digitalWrite(PinIn,LOW); // set initial trigger state
- }
- void loop()
- {
- // read the state of the switch into a local variable:
- int reading = digitalRead(PinIn);
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval)
- {
- previousMillis = currentMillis;
- Light_Pulse(); //activate Light_Pulse at interval time
- }
- Light_Max(); //switch all LEDs on
- if (Check_Button() == true)
- {
- Flash_X();
- }
- }
- bool Check_Button()
- {
- if (reading != lastButtonState)
- {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay)
- {
- // whatever the reading is at, it’s been there for longer than the debounce
- // delay, so take it as the actual current state:
- if (reading != buttonState1)
- {
- // if the button state has changed:
- buttonState1 = reading;
- buttonState1 = digitalRead(PinIn); // only toggle if the new button state is HIGH
- if (buttonState1 == HIGH)
- {
- return true;
- }
- }
- }
- // save the reading. Next time through the loop, it’ll be the lastButtonState:
- lastButtonState = reading;
- return false;
- }
- void Light_Max()
- {
- fill_solid(redLeds, NUM_LEDS, CRGB::Red);
- fill_solid(greenLeds, NUM_LEDS, CRGB::Green);
- fill_solid(blueLeds, NUM_LEDS, CRGB::Blue);
- fill_solid(purpleLeds, NUM_LEDS,CRGB::Purple);
- FastLED.show();
- }
- void Light_Pulse()
- {
- bool ButtonPressed = false;
- //fade count limits how many times Light_Pulse runs
- for (int fade_count=0; fade_count <= 2; fade_count++)
- {
- brightness = 135; //Set this so its starts at full brightness
- fadeAmount = -1; //Start from light to dark
- for (int fade_loop = 0; fade_loop < 135 * 4; fade_loop++) // 135 * 2 gives it the opportunity to go through 0 to 135 twice
- {
- ButtonPressed = Check_Button();
- if (ButtonPressed)
- {
- break; //If the button is pressed, we will exit out of this fade_loop
- }
- for(i=0; i< NUM_LEDS; i++)
- {
- redLeds[i] = CRGB::Red; //set colours in the array
- greenLeds[i] = CRGB::Green;
- blueLeds[i] = CRGB::Blue;
- purpleLeds[i] = CRGB::Purple;
- redLeds[i]. fadeLightBy(brightness); //apply the brightness
- greenLeds[i]. fadeLightBy(brightness);
- blueLeds[i]. fadeLightBy(brightness);
- purpleLeds[i].fadeLightBy(brightness);
- }
- FastLED.show();
- brightness = brightness + fadeAmount; //raise the brightness value
- if (brightness <= 0)
- { //fade up
- fadeAmount = -fadeAmount;
- brightness = 0 + fadeAmount;
- }
- if (brightness >= 155)
- { //fade down (not to off)
- fadeAmount = -fadeAmount;
- brightness = 155 + fadeAmount; //0=full brightness, 255=minimum brightness
- }
- delay(10); //this setting is the speed of the brighness change
- }
- if (ButtonPressed)
- {
- break; //If the button was pressed, we exit out of the fade_count loop too
- }
- }
- if (ButtonPressed)
- {
- Flash_X();
- }
- }
- void Flash_X()
- {
- for (j=0; j<=flashes; j++)
- {
- FastLED.clear(true); //all LED arrays cleared / off
- delay(225); //delay time of transition for OFF to ON for flash sequence
- fill_solid(redLeds, NUM_LEDS, CRGB::Red);
- fill_solid(greenLeds, NUM_LEDS, CRGB::Green);
- fill_solid(blueLeds, NUM_LEDS, CRGB::Blue);
- fill_solid(purpleLeds, NUM_LEDS,CRGB::Purple);
- FastLED.show(); //all LED arrays set to full brightness
- delay(225); //delay time of transition for ON to OFF for flash sequence
- }
- for(i=0; i< NUM_LEDS; i++)
- {
- redLeds[i] = CRGB::Black;
- greenLeds[i] = CRGB::Black;
- blueLeds[i] = CRGB::Black;
- purpleLeds[i] = CRGB::Black;
- FastLED.show(); //all LED arrays set to increment off pixels
- delay(8); //this setting is the speed of pixel movement
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement