Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. //Reverse Comet Effect Written for Leon van der Meij
  2. //Author: Scott C
  3. //Date: 26th July 2017
  4. //Arduino IDE version 1.8.1
  5. //======================================================================
  6.  
  7. #include "FastLED.h" //Make sure to install the FastLED library into your Arduino IDE
  8.  
  9. //The total number of LEDs being used is 300
  10. #define NUM_LEDS 300
  11.  
  12. // The data pin for the NeoPixel strip is connected to digital Pin 6 on the Arduino
  13. #define DATA_PIN 6
  14.  
  15. // The button will be connected to digital pin 9 on the Arduino. Uses Internal pullup resistor.
  16. #define BUTTON 9
  17.  
  18. //Initialise the LED array, the LED Hue (ledh) array, and the LED Brightness (ledb) array.
  19. CRGB leds[NUM_LEDS];
  20. byte ledh[NUM_LEDS];
  21. byte ledb[NUM_LEDS];
  22.  
  23. const int LEDSpeed = 50; //Speed of the LED animation effect. Make sure not to exceed the maxLEDSpeed value.
  24. int maxLEDSpeed = 50; //Identifies the maximum speed of the LED animation sequence
  25. int LEDposition=299; //Identifies the LED position in the strip that the comet is currently at. The maximum position is NUM_LEDS-1 (eg. 299)
  26. int oldPosition=0; //Holds the previous position of the comet.
  27.  
  28. byte hue = 0; //Stores the Leading LED's hue value (colour)
  29. byte sat = 255; //Stores the Leading LED's saturation value
  30. byte tailHue = 0; //Stores the Comet tail hue value
  31. int tailLength = 8; //determines the length of the tail.
  32. byte hueRange = 20; //Colour variation of the tail (greater values have greater variation
  33. byte intensity = 200; //The default brightness of the leading LED
  34. byte tailbrightness= intensity / 2; //Affects the brightness and length of the tail
  35. int animationDelay = 0; //The greater the animation delay, the slower the LED sequence. Calculated from LEDSpeed and MaxSpeed.
  36.  
  37. unsigned long waitTime = 60000; //The wait time for each comet is currently set for 1 minute (or 60000 milliseconds).
  38. unsigned long endTime; //The time when we no longer have to wait for the next comet
  39.  
  40. int cometNumber = 3; //Used to choose which comet colour to show (***Don't change this variable***)
  41.  
  42.  
  43. //===================================================================================================================================================
  44. // setup() : Is used to initialise the LED strip
  45. //===================================================================================================================================================
  46. void setup() {
  47. delay(2000); //Delay for two seconds to power the LEDS before starting the data signal on the Arduino
  48. FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); //initialise the LED strip
  49. pinMode(BUTTON, INPUT_PULLUP); //Connect button to digital pin 9, and use the internal pullup resistor
  50. selectNextComet(); //Select the next comet colour
  51. }
  52.  
  53.  
  54. //===================================================================================================================================================
  55. // loop() :
  56. //===================================================================================================================================================
  57. void loop(){
  58. showLED(LEDposition, hue, sat, intensity);
  59.  
  60. //Adjust the hue of the tail so that it is a different colour from the leading LED. To reduce variation in tail colour, reduce the hueRange.
  61. if(hue>(254-hueRange)){
  62. tailHue = random((hue-hueRange),hue);
  63. } else {
  64. tailHue = random(hue, (hue+hueRange));
  65. }
  66.  
  67. tailbrightness = random(50, 100); //Randomly select the brightness of the trailing LED (provides sparkling tail)
  68.  
  69.  
  70. leds[LEDposition]=CHSV((tailHue),sat,tailbrightness); //Set the colour, saturation and brightness of the trailing LED
  71. fadeLEDs(tailLength); //Fade the tail so that the tail brightness dwindles down to nothingness.
  72. setDelay(LEDSpeed); //
  73.  
  74. LEDposition--;
  75. if(LEDposition<1){
  76.  
  77. for(int i=0; i<50; i++){
  78. showLED(LEDposition, hue, sat, intensity);
  79. fadeLEDs(tailLength);
  80. setDelay(LEDSpeed);
  81. }
  82. LEDposition=299;
  83. selectNextComet(); //Select the next comet colour
  84. waitForNextComet(); //wait for the next comet (either 60 seconds or button press)
  85. }
  86.  
  87. }
  88.  
  89.  
  90. //===================================================================================================================================================
  91. // showLED() : is used to illuminate the LEDs
  92. //===================================================================================================================================================
  93. void showLED(int pos, byte LEDhue, byte LEDsat, byte LEDbright){
  94. leds[pos] = CHSV(LEDhue,LEDsat,LEDbright);
  95. FastLED.show();
  96. }
  97.  
  98.  
  99. //===================================================================================================================================================
  100. // fadeLEDs(): This function is used to fade the LEDs back to black (OFF)
  101. //===================================================================================================================================================
  102. void fadeLEDs(int fadeVal){
  103. for (int i = 0; i<NUM_LEDS; i++){
  104. leds[i].fadeToBlackBy( fadeVal );
  105. }
  106. }
  107.  
  108.  
  109. //===================================================================================================================================================
  110. // setDelay() : is where the speed of the LED animation sequence is controlled. The speed of the animation is controlled by the LEDSpeed variable.
  111. // and cannot go faster than the maxLEDSpeed variable.
  112. //===================================================================================================================================================
  113. void setDelay(int LSpeed){
  114. animationDelay = maxLEDSpeed - abs(LSpeed);
  115. delay(animationDelay);
  116. }
  117.  
  118.  
  119. //===================================================================================================================================================
  120. //selectNextComet() : This is where we select either the Blue, the Pink or the White comet
  121. //===================================================================================================================================================
  122. void selectNextComet(){
  123. cometNumber++;
  124. if(cometNumber>3){
  125. cometNumber=1;
  126. }
  127.  
  128. switch(cometNumber){
  129. case 1: { //Blue Comet
  130. hue = 160;
  131. sat = 255;
  132. hueRange=20;
  133. break;
  134. }
  135.  
  136. case 2: { //Pink Comet
  137. hue = 224;
  138. sat = 120;
  139. hueRange=10;
  140. break;
  141. }
  142.  
  143. default: { //White Comet
  144. hue = 0;
  145. sat = 0;
  146. hueRange = 0;
  147. break;
  148. }
  149. }
  150. }
  151.  
  152.  
  153. //===================================================================================================================================================
  154. // waitForNextComet() : Is where we either wait for 60 seconds for another comet to come, or initiate another comet with a button press.
  155. // The button will only be "ACTIVE" while we are waiting for the next comet. It has no effect while a comet is currently running
  156. //===================================================================================================================================================
  157. void waitForNextComet(){
  158. endTime = millis() + waitTime;
  159.  
  160. while(millis()<endTime){
  161. if(digitalRead(BUTTON)==LOW){
  162. break;
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement