Advertisement
Guest User

LED Flash

a guest
Oct 6th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. // Program for Smart LED tape. Button debounce (working) and responds to a contact closure on pin8
  2. // At power on, LEDS all on, colour solid. 4 pins output different colours
  3. // so one this one program can be used for any roundel.
  4. // Upon contact closure (5V n/o) momentary event, the following happens to all 4 colour pins:
  5. // LEDS all off / all on fast pulse (x2), with a LED chase effect to finish
  6. // Lights all on when this finishes, so roundel colour remains visible.
  7. //
  8. // 20/08/2020 tested stable, with 110k-Ohm resistor between GND and pin8
  9. // 24/08/2020 tested stable, with 4 extenstions of Cat5 (185M) between 12V PSU & 5V relay and Arduino & LED strip
  10. // 25/08/2020 V3_2: test code removed, leaving only code in use
  11. // 30/09/2020 V5: code replacement with ALL leds on and off now (instead of quick increment)
  12. // Timings adjusted for best responsiveness of flashes
  13. // 02/10/2020 V8: Light_Pulse slowly fades between 50% - 100% brightness in a loop, to add minor animation to the solid ON state
  14. // Added an interval timer, so a number of light pulses happen after an adjustable length of time of solid LEDs on
  15. //
  16. // by S.Arbuckle
  17.  
  18. #include <FastLED.h>
  19.  
  20. #define NUM_LEDS 38 //number of LEDs in the roundel strip
  21. #define NUM_STRIPS 3
  22. #define DATA_PIN_R 3
  23. #define DATA_PIN_G 5
  24. #define DATA_PIN_B 6
  25. #define DATA_PIN_P 9
  26. #define NUM_COLORS 3
  27. #define LED_TYPE WS2812B
  28. #define COLOR_ORDER RGB
  29.  
  30. //define input pins
  31. const int PinIn = 8;
  32. const long interval = 15000; //interval at which to run Light_Pulse
  33. int i = 0;
  34. int j = 0;
  35. int brightness = 1;
  36. int fadeAmount = 2;
  37. int flashes = 1;
  38. int buttonState1 = 0;
  39. int lastButtonState = LOW; // the previous reading from the input pin
  40.  
  41. unsigned long lastDebounceTime = 0; //the last time the output pin was toggled
  42. unsigned long debounceDelay = 200; //the debounce time; increase if the output flickers
  43. unsigned long previousMillis = 0; //store the last time interval timer was updated
  44.  
  45. CRGB redLeds [NUM_LEDS];
  46. CRGB greenLeds [NUM_LEDS];
  47. CRGB blueLeds [NUM_LEDS];
  48. CRGB purpleLeds[NUM_LEDS];
  49.  
  50. void setup()
  51. {
  52.     // tell FastLED about the LED strip configuration
  53.     FastLED.addLeds<LED_TYPE,DATA_PIN_R,COLOR_ORDER>(redLeds, NUM_LEDS);
  54.     FastLED.addLeds<LED_TYPE,DATA_PIN_G,COLOR_ORDER>(greenLeds, NUM_LEDS);
  55.     FastLED.addLeds<LED_TYPE,DATA_PIN_B,COLOR_ORDER>(blueLeds, NUM_LEDS);
  56.     FastLED.addLeds<LED_TYPE,DATA_PIN_P,COLOR_ORDER>(purpleLeds, NUM_LEDS);
  57.  
  58.     pinMode(PinIn,INPUT);
  59.     digitalWrite(PinIn,LOW); // set initial trigger state
  60. }
  61.  
  62. void loop()
  63. {
  64.     // read the state of the switch into a local variable:
  65.     int reading = digitalRead(PinIn);
  66.     unsigned long currentMillis = millis();
  67.  
  68.     if (currentMillis - previousMillis >= interval)
  69.     {
  70.         previousMillis = currentMillis;
  71.         Light_Pulse(); //activate Light_Pulse at interval time
  72.     }
  73.  
  74.     Light_Max(); //switch all LEDs on
  75.  
  76.     if (Check_Button() == true)
  77.     {
  78.         Flash_X();
  79.     }
  80. }
  81.  
  82. bool Check_Button()
  83. {
  84.     if (reading != lastButtonState)
  85.     {
  86.         // reset the debouncing timer
  87.         lastDebounceTime = millis();
  88.     }
  89.  
  90.     if ((millis() - lastDebounceTime) > debounceDelay)
  91.     {
  92.         // whatever the reading is at, it’s been there for longer than the debounce
  93.         // delay, so take it as the actual current state:
  94.         if (reading != buttonState1)
  95.         {
  96.             // if the button state has changed:
  97.             buttonState1 = reading;
  98.             buttonState1 = digitalRead(PinIn); // only toggle if the new button state is HIGH
  99.             if (buttonState1 == HIGH)
  100.             {
  101.                 return true;
  102.             }
  103.         }
  104.     }
  105.     // save the reading. Next time through the loop, it’ll be the lastButtonState:
  106.     lastButtonState = reading;
  107.     return false;
  108. }
  109.  
  110. void Light_Max()
  111. {
  112.     fill_solid(redLeds, NUM_LEDS, CRGB::Red);
  113.     fill_solid(greenLeds, NUM_LEDS, CRGB::Green);
  114.     fill_solid(blueLeds, NUM_LEDS, CRGB::Blue);
  115.     fill_solid(purpleLeds, NUM_LEDS,CRGB::Purple);
  116.     FastLED.show();
  117. }
  118.  
  119. void Light_Pulse()
  120. {
  121.     bool ButtonPressed = false;
  122.     //fade count limits how many times Light_Pulse runs
  123.     for (int fade_count=0; fade_count <= 2; fade_count++)
  124.     {
  125.         brightness = 135; //Set this so its starts at full brightness
  126.         fadeAmount = -1; //Start from light to dark
  127.         for (int fade_loop = 0; fade_loop < 135 * 4; fade_loop++) // 135 * 2 gives it the opportunity to go through 0 to 135 twice
  128.         {
  129.             ButtonPressed = Check_Button();
  130.             if (ButtonPressed)
  131.             {
  132.                 break; //If the button is pressed, we will exit out of this fade_loop
  133.             }
  134.            
  135.             for(i=0; i< NUM_LEDS; i++)
  136.             {
  137.                 redLeds[i] = CRGB::Red; //set colours in the array
  138.                 greenLeds[i] = CRGB::Green;
  139.                 blueLeds[i] = CRGB::Blue;
  140.                 purpleLeds[i] = CRGB::Purple;
  141.  
  142.                 redLeds[i]. fadeLightBy(brightness); //apply the brightness
  143.                 greenLeds[i]. fadeLightBy(brightness);
  144.                 blueLeds[i]. fadeLightBy(brightness);
  145.                 purpleLeds[i].fadeLightBy(brightness);
  146.             }
  147.             FastLED.show();
  148.  
  149.             brightness = brightness + fadeAmount; //raise the brightness value
  150.  
  151.             if (brightness <= 0)
  152.             { //fade up
  153.                 fadeAmount = -fadeAmount;
  154.                 brightness = 0 + fadeAmount;
  155.             }
  156.             if (brightness >= 155)
  157.             { //fade down (not to off)
  158.                 fadeAmount = -fadeAmount;
  159.                 brightness = 155 + fadeAmount; //0=full brightness, 255=minimum brightness
  160.             }
  161.             delay(10); //this setting is the speed of the brighness change
  162.         }
  163.         if (ButtonPressed)
  164.         {
  165.             break;  //If the button was pressed, we exit out of the fade_count loop too
  166.         }
  167.     }
  168.     if (ButtonPressed)
  169.     {
  170.         Flash_X();
  171.     }
  172. }
  173.  
  174. void Flash_X()
  175. {
  176.     for (j=0; j<=flashes; j++)
  177.     {
  178.         FastLED.clear(true); //all LED arrays cleared / off
  179.         delay(225); //delay time of transition for OFF to ON for flash sequence
  180.         fill_solid(redLeds, NUM_LEDS, CRGB::Red);
  181.         fill_solid(greenLeds, NUM_LEDS, CRGB::Green);
  182.         fill_solid(blueLeds, NUM_LEDS, CRGB::Blue);
  183.         fill_solid(purpleLeds, NUM_LEDS,CRGB::Purple);
  184.         FastLED.show(); //all LED arrays set to full brightness
  185.         delay(225); //delay time of transition for ON to OFF for flash sequence
  186.     }
  187.  
  188.     for(i=0; i< NUM_LEDS; i++)
  189.     {
  190.         redLeds[i] = CRGB::Black;
  191.         greenLeds[i] = CRGB::Black;
  192.         blueLeds[i] = CRGB::Black;
  193.         purpleLeds[i] = CRGB::Black;
  194.         FastLED.show(); //all LED arrays set to increment off pixels
  195.         delay(8); //this setting is the speed of pixel movement
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement