chemdoc77

Spooky Eyes for FastLED

Oct 24th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. // Spooky Eyes modified for use with FastLED By Chemdoc77 using W. Earl's orgianal code.
  2.  
  3.  
  4. /*****************************************************************************
  5. Based on Random Eyes sketch for WS2801 pixels
  6. W. Earl 10/16/11
  7. For Adafruit Industries
  8.  
  9. Creates randomized pairs of WS2801 led pixels that look like eyes peering
  10. from the darkness.
  11.  
  12. Blinking is implemented as an array of state machines so that multiple pairs
  13. of eyes can be active concurrently, but in different phases of a blink.
  14. *****************************************************************************/
  15. #include "SPI.h"
  16.  
  17. #include <TimerOne.h>
  18. #include "FastLED.h"
  19. #define NUM_LEDS 24
  20. #define Data_Pin 6
  21. #define chipset NEOPIXEL //WS2811
  22. CRGB leds[NUM_LEDS];
  23. #define Brightness 50
  24.  
  25.  
  26. const int maxEyes = 5; // maximum number of concurrently active blinkers
  27.  
  28. // dead-time between lighting of a range of pixels
  29. const int deadTimeMin = 50;
  30. const int deadTimeMax = 500;
  31.  
  32. // interval between blink starts - independent of position
  33. const int intervalMin = 10;
  34. const int intervalMax = 300;
  35.  
  36. const int stepInterval = 10;
  37. long lastStep = 0;
  38.  
  39.  
  40. /*****************************************************************************
  41. Blinker Class
  42.  
  43. Implements a state machine which generates a blink of random duration and color.
  44. The blink uses two adjacent pixels and ramps the intensity up, then down, with
  45. a random repeat now and again.
  46. *****************************************************************************/
  47.  
  48. class blinker
  49. {
  50. public:
  51.  
  52. boolean m_active; // blinker is in use.
  53. int m_deadTime; // don't re-use this pair immediately
  54.  
  55. int m_pos; // position of the 'left' eye. the 'right' eye is m_pos + 1
  56.  
  57. int m_red; // RGB components of the color
  58. int m_green;
  59. int m_blue;
  60.  
  61. int m_increment; // ramp increment - determines blink speed
  62. int m_repeats; // not used
  63. int m_intensity; // current ramp intensity
  64.  
  65. public:
  66. // Constructor - start as inactive
  67. blinker()
  68. {
  69. m_active = false;
  70. }
  71.  
  72. // Initiate a blink at the specified pixel position
  73. // All other blink parameters are randomly generated
  74. void StartBlink(int pos)
  75. {
  76. m_pos = pos;
  77.  
  78. // Pick a random color - skew toward red/orange/yellow part of the spectrum for extra creepyness
  79. m_red = random(150, 255);
  80. m_blue = 0;
  81. m_green = random(100);
  82.  
  83. m_repeats += random(1, 3);
  84.  
  85. // set blink speed and deadtime between blinks
  86. m_increment = random(1, 6);
  87. m_deadTime = random(deadTimeMin, deadTimeMax);
  88.  
  89. // Mark as active and start at intensity zero
  90. m_active = true;
  91. m_intensity = 0;
  92. }
  93.  
  94. // Step the state machine:
  95. void step()
  96. {
  97. if (!m_active)
  98. {
  99. // count down the dead-time when the blink is done
  100. if (m_deadTime > 0)
  101. {
  102. m_deadTime--;
  103. }
  104. return;
  105. }
  106.  
  107. // Increment the intensity
  108. m_intensity += m_increment;
  109. if (m_intensity >= 75) // max out at 75 - then start counting down
  110. {
  111. m_increment = -m_increment;
  112. m_intensity += m_increment;
  113. }
  114. if (m_intensity <= 0)
  115. {
  116. // make sure pixels all are off
  117. //strip.setPixelColor(m_pos, Color(0,0,0));
  118. leds[m_pos]= CRGB::Black;
  119. //strip.setPixelColor(m_pos+1, Color(0,0,0));
  120. leds[m_pos+1]= CRGB::Black;
  121.  
  122. if (--m_repeats <= 0) // Are we done?
  123. {
  124. m_active = false;
  125. }
  126. else // no - start to ramp up again
  127. {
  128. m_increment = random(1, 5);
  129. }
  130. return;
  131. }
  132.  
  133. // Generate the color at the current intensity level
  134. int r = map(m_red, 0, 255, 0, m_intensity);
  135. int g = map(m_green, 0, 255, 0, m_intensity);
  136. int b = map(m_blue, 0, 255, 0, m_intensity);
  137. //uint32_t color = Color(r, g, b);
  138.  
  139. CRGB color = CRGB( r, g, b);
  140.  
  141. // Write to both 'eyes'
  142. leds[m_pos]= color;
  143. // strip.setPixelColor(m_pos +1, color);
  144. leds[m_pos+1]= color;
  145. }
  146. };
  147.  
  148. // An array of blinkers - this is the maximum number of concurrently active blinks
  149. blinker blinkers[maxEyes];
  150.  
  151. // A delay between starting new blinks
  152. int countdown;
  153.  
  154. void setup()
  155. {
  156. Serial.begin(9600);
  157. delay(2000); // power-up safety delay
  158. FastLED.addLeds<chipset, Data_Pin>(leds, NUM_LEDS);
  159. FastLED.setBrightness(Brightness);
  160.  
  161. countdown = 0;
  162. }
  163.  
  164.  
  165. void loop()
  166. {
  167. if (millis() - lastStep > stepInterval)
  168. {
  169. lastStep = millis();
  170. --countdown;
  171. for(int i = 0; i < maxEyes; i++)
  172. {
  173. // Only start a blink if the countdown is expired and there is an available blinker
  174. if ((countdown <= 0) && (blinkers[i].m_active == false))
  175. {
  176. int newPos = random(0,NUM_LEDS/2) * 2;
  177.  
  178. for(int j = 0; j < maxEyes; j++)
  179. {
  180. // avoid active or recently active pixels
  181. if ((blinkers[j].m_deadTime > 0) && (abs(newPos - blinkers[j].m_pos) < 4))
  182. {
  183. Serial.print("-");
  184. Serial.print(newPos);
  185. newPos = -1; // collision - do not start
  186. break;
  187. }
  188. }
  189.  
  190. if (newPos >= 0) // if we have a valid pixel to start with...
  191. {
  192. Serial.print(i);
  193. Serial.print(" Activate - ");
  194. Serial.println(newPos);
  195. blinkers[i].StartBlink(newPos);
  196. countdown = random(intervalMin, intervalMax); // random delay to next start
  197. }
  198. }
  199. // step all the state machines
  200. blinkers[i].step();
  201. }
  202.  
  203. FastLED.show();
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment