Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. // Pattern types supported:
  4. enum pattern { NONE, COLOR_WIPE, STROBE};
  5.  
  6. // Patern directions supported:
  7. enum direction { FORWARD, REVERSE };
  8.  
  9.  
  10. // NeoPattern Class - derived from the Adafruit_NeoPixel class
  11. class NeoPatterns : public Adafruit_NeoPixel
  12. {
  13. public:
  14.  
  15. // Member Variables:
  16. pattern ActivePattern; // which pattern is running
  17. direction Direction; // direction to run the pattern
  18. unsigned long Interval; // milliseconds between updates
  19. unsigned long lastUpdate; // last update of position
  20.  
  21. uint32_t Color1, Color2; // What colors are in use
  22. uint16_t TotalSteps; // total number of steps in the pattern
  23. uint16_t Index; // current step within the pattern
  24. void (*OnComplete)(); // Callback on completion of pattern
  25.  
  26. void Update()
  27. {
  28. if((millis() - lastUpdate) > Interval) // time to update
  29. {
  30. lastUpdate = millis();
  31. switch(ActivePattern)
  32. {
  33. case COLOR_WIPE:
  34. ColorWipeUpdate();
  35. break;
  36. default:
  37. break;
  38. }
  39. }
  40. }
  41. // Constructor - calls base-class constructor to initialize strip
  42. NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
  43. :Adafruit_NeoPixel(pixels, pin, type)
  44. {
  45. OnComplete = callback;
  46. }
  47. // Increment the Index and reset at the end
  48. void Increment()
  49. {
  50. if (Direction == FORWARD)
  51. {
  52. Index++;
  53. if (Index >= TotalSteps)
  54. {
  55. Index = 0;
  56. if (OnComplete != NULL)
  57. {
  58. OnComplete(); // call the comlpetion callback
  59. }
  60. }
  61. }
  62. else // Direction == REVERSE
  63. {
  64. --Index;
  65. if (Index <= 0)
  66. {
  67. Index = TotalSteps-1;
  68. if (OnComplete != NULL)
  69. {
  70. OnComplete(); // call the comlpetion callback
  71. }
  72. }
  73. }
  74. }
  75.  
  76. // Reverse pattern direction
  77. void Reverse()
  78. {
  79. if (Direction == FORWARD)
  80. {
  81. Direction = REVERSE;
  82. Index = TotalSteps-1;
  83. }
  84. else
  85. {
  86. Direction = FORWARD;
  87. Index = 0;
  88. }
  89. }
  90.  
  91.  
  92. // Initialize for a ColorWipe
  93. void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
  94. {
  95. ActivePattern = COLOR_WIPE;
  96. Interval = interval;
  97. TotalSteps = numPixels();
  98. Color1 = color;
  99. Index = 0;
  100. Direction = dir;
  101. }
  102.  
  103. // Update the Color Wipe Pattern
  104. void ColorWipeUpdate()
  105. {
  106. setPixelColor(Index, Color1);
  107. show();
  108. Increment();
  109. }
  110.  
  111.  
  112. void ColorSet(uint32_t color)
  113. {
  114. for (int i = 0; i < numPixels(); i++)
  115. {
  116. setPixelColor(i, color);
  117. }
  118. show();
  119. }
  120.  
  121. // Returns the Red component of a 32-bit color
  122. uint8_t Red(uint32_t color)
  123. {
  124. return (color >> 16) & 0xFF;
  125. }
  126.  
  127. // Returns the Green component of a 32-bit color
  128. uint8_t Green(uint32_t color)
  129. {
  130. return (color >> 8) & 0xFF;
  131. }
  132.  
  133. // Returns the Blue component of a 32-bit color
  134. uint8_t Blue(uint32_t color)
  135. {
  136. return color & 0xFF;
  137. }
  138.  
  139. // Input a value 0 to 255 to get a color value.
  140. // The colours are a transition r - g - b - back to r.
  141. uint32_t Wheel(byte WheelPos)
  142. {
  143. WheelPos = 255 - WheelPos;
  144. if(WheelPos < 85)
  145. {
  146. return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  147. }
  148. else if(WheelPos < 170)
  149. {
  150. WheelPos -= 85;
  151. return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  152. }
  153. else
  154. {
  155. WheelPos -= 170;
  156. return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  157. }
  158. }
  159. };
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. void strip1Complete();
  174. void strip2Complete();
  175. // Define some NeoPatterns for the two strips
  176. // as well as some completion routines
  177. NeoPatterns strip1(300, 6, NEO_GRBW + NEO_KHZ800, &strip1Complete);
  178. NeoPatterns strip2(150, 5, NEO_GRB + NEO_KHZ800, &strip1Complete);
  179.  
  180. // Initialize everything and prepare to start
  181. void setup()
  182. {
  183.  
  184. // Initialize all the pixelStrips
  185. strip1.begin();
  186. strip2.begin();
  187. }
  188.  
  189.  
  190. unsigned long next = 1;
  191. boolean phase1_done = false;
  192. boolean phase2_done = false;
  193. unsigned long msec = 5; // next trigger time
  194.  
  195. // Main loop
  196. void loop()
  197. {
  198. // Update the strips.
  199. strip1.Update();
  200. strip2.Update();
  201.  
  202. if ((millis() - next) >= !phase1_done)
  203. {
  204. strip1.ActivePattern = COLOR_WIPE;
  205. strip2.ActivePattern = COLOR_WIPE;
  206. strip2.TotalSteps = strip2.numPixels();
  207.  
  208. // lock out this phase
  209. phase1_done = true;
  210. phase2_done = true;
  211. msec = 99; // next phase
  212. }
  213. }
  214.  
  215. //------------------------------------------------------------
  216. //Completion Routines - get called on completion of a pattern
  217. //------------------------------------------------------------
  218.  
  219. // Strip1 Completion Callback
  220. void strip1Complete()
  221. {
  222. strip1.Color1 = strip1.Wheel(random(255));
  223. strip1.Interval = 20;
  224. }
  225.  
  226. // Strip 2 Completion Callback
  227. void Strip2Complete()
  228. {
  229. strip2.Color1 = strip2.Wheel(random(255));
  230. strip2.Interval = 20;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement