Advertisement
Guest User

GreenMask.ino

a guest
Mar 18th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3.  
  4. FASTLED_USING_NAMESPACE
  5.  
  6. // Credits -Mark Kriegsman, December 2014
  7. // https://armaizadenwala.com/blog/how-to-create-a-led-rave-mask-using-arduino/
  8.  
  9.  
  10. //LED
  11. #define DATA_PIN A5
  12. //#define CLK_PIN 4
  13. #define LED_TYPE WS2812B
  14. #define COLOR_ORDER GRB
  15. #define NUM_LEDS 65
  16. CRGB leds[NUM_LEDS];
  17.  
  18. #include "constants.h"
  19.  
  20. #define FRAMES_PER_SECOND 120
  21.  
  22. // Push button for Animation Cycle
  23. #define buttonAnimationPin A1
  24. uint8_t buttonAnimationState = 0; // current state of the button
  25. uint8_t lastButtonAnimationState = 0; // previous state of the button
  26.  
  27. // Push button for Palette Cycle
  28. #define buttonPalettePin A2
  29. uint8_t buttonPaletteState = 0; // current state of the button
  30. uint8_t lastButtonPaletteState = 0; // previous state of the button
  31.  
  32.  
  33. //Potentiometer for Brightness
  34. #define potBrightnessPin A0 // analog pin used to connect the potentiometer
  35.  
  36.  
  37. CRGBPalette16 currentPalette;
  38. TBlendType currentBlending;
  39.  
  40.  
  41.  
  42. // ----------------------------------------------------------------------------------------- SETUP EXECUTABLE ----------------------
  43. void setup() {
  44. delay(1000); // 1 second delay for recovery
  45.  
  46. FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // tell FastLED about the LED strip configuration
  47.  
  48. pinMode(buttonAnimationPin, INPUT); // initialize the pushbuttons pin as an input for animation cycle
  49. pinMode(buttonPalettePin, INPUT); // initialize the pushbuttons pin as an input for palette cycle
  50.  
  51. Serial.begin(9600); // Allow Serial Monitor for debugging
  52. }
  53. // ------------------------------------------------------------------------------------------------ END SETUP ------------------------
  54.  
  55.  
  56. // List of Patterns to cycle through. Each is defined as a separate function below.
  57. typedef void (*SimplePatternList[])();
  58. // Decleration of all Animation Functions
  59. SimplePatternList gPatterns = { customBigVPattern, customOneTwoPattern, customTrianglesPattern, customCirclesPattern, customOutwardsWingsPattern, customOutwardsColumnPattern, customScrollingRowPattern, customScrollingColumnPattern, bpm, rainbow, rainbowWithGlitter, sinelon, confetti, juggle };
  60. #define ARRAY_SIZE(gPatterns) (sizeof(gPatterns) / sizeof((gPatterns)[0]))
  61. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  62.  
  63.  
  64. // List of Palettes to cycle through. All defined below.
  65. extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
  66. extern uint8_t gGradientPaletteCount;
  67. uint8_t gCurrentPaletteNumber = 0; // Current number of available palettes the 'playlist' of color palettes
  68.  
  69. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  70.  
  71.  
  72.  
  73. // ---------------------------------------------------------------------------------------------MAIN EXECUTABLE ----------------------
  74. void loop()
  75. {
  76. checkAnimationButton(); // Check animation push button for state change
  77.  
  78. gPatterns[gCurrentPatternNumber](); // Call gPatterns function and refer to position [GCurrentPatternNumber] to execute the pattern animation function in that position (e.g. 0 is rainbow)
  79.  
  80. checkPaletteButton();
  81. currentPalette = gGradientPalettes[ gCurrentPaletteNumber ];
  82.  
  83. potentialBrightness(); // Check potentiometer for brighness change
  84. //FastLED.setBrightness(255);
  85. FastLED.show(); // send the 'leds' array out to the actual LED strip
  86.  
  87. FastLED.delay(1000 / FRAMES_PER_SECOND); // insert a delay to keep the framerate modest
  88.  
  89.  
  90. EVERY_N_SECONDS( 5 ) {
  91. gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
  92. //TargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
  93. }
  94.  
  95. EVERY_N_MILLISECONDS( 10 ) { // Change Hue base color every X milliseconds
  96. gHue++; // slowly cycle the "base color" through the rainbow
  97. }
  98.  
  99. }
  100.  
  101. // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MAIN END XXXXXXXXXXXXXXXXXX
  102.  
  103.  
  104. // --------------------------------------------------------------------- INPUT CONTROLS FUNCTIONS START ----------------------------
  105.  
  106. // Animation cycle based on Push Button
  107. void checkAnimationButton() {
  108. // read the pushbutton input pin:
  109. buttonAnimationState = digitalRead(buttonAnimationPin);
  110. // compare the buttonState to its previous state
  111. if (buttonAnimationState != lastButtonAnimationState) {
  112. // if the state has changed, increment the counter
  113. if (buttonAnimationState == HIGH) {
  114. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  115. //Serial.print("Pattern: ");
  116. //Serial.println(gCurrentPatternNumber);
  117. }
  118. // Delay a little bit to avoid bouncing
  119. delay(10);
  120. }
  121. // save the current state as the last state, for next time through the loop
  122. lastButtonAnimationState = buttonAnimationState;
  123. }
  124.  
  125. // Palette cycle based on Push Button
  126. void checkPaletteButton() {
  127. // read the pushbutton input pin:
  128. buttonPaletteState = digitalRead(buttonPalettePin);
  129. // compare the buttonState to its previous state
  130. if (buttonPaletteState != lastButtonPaletteState) {
  131. // if the state has changed, increment the counter
  132. if (buttonPaletteState == HIGH) {
  133. gCurrentPaletteNumber = (gCurrentPaletteNumber + 1) % gGradientPaletteCount;
  134. //Serial.print("Palette: ");
  135. //Serial.println(gCurrentPaletteNumber);
  136. }
  137. // Delay a little bit to avoid bouncing
  138. delay(10);
  139. }
  140. // save the current state as the last state, for next time through the loop
  141. lastButtonPaletteState = buttonPaletteState;
  142. }
  143.  
  144. // Brightness Check using Potentiometer
  145. void potentialBrightness() {
  146. FastLED.setBrightness(map(analogRead(potBrightnessPin), 0, 1050, 20, 100)); // scale the numbers from the potential value (20 - 100)
  147. }
  148.  
  149. // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX INPUT CONTROLS FUNCTIONS END XXXXXXXXXXXXXXXXXXXXXX
  150.  
  151.  
  152. // ----------------------------------------------------------------------- ANIMATION FUNCTIONS START ----------------------------
  153.  
  154.  
  155. // Pattern Animation - Preset: Rainbow
  156. void rainbow()
  157. {
  158. // FastLED's built-in rainbow generator
  159. fill_rainbow( leds, NUM_LEDS, gHue, 5);
  160. }
  161.  
  162.  
  163. // Pattern Animation - Preset: Rainbow w/ Glitter
  164. void rainbowWithGlitter()
  165. {
  166. // built-in FastLED rainbow, plus some random sparkly glitter
  167. rainbow();
  168. addGlitter(80);
  169. }
  170.  
  171.  
  172. // Pattern Animation Add-on - Preset: Glitter
  173. void addGlitter( fract8 chanceOfGlitter)
  174. {
  175. if ( random8() < chanceOfGlitter) {
  176. leds[ random16(NUM_LEDS) ] += CRGB::White;
  177. }
  178. }
  179.  
  180.  
  181. // Pattern Animation - Preset: Confetti
  182. void confetti()
  183. {
  184. // random colored speckles that blink in and fade smoothly
  185. fadeToBlackBy( leds, NUM_LEDS, 10);
  186. int pos = random16(NUM_LEDS);
  187. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  188. }
  189.  
  190.  
  191. // Pattern Animation - Preset: Sinelon
  192. void sinelon()
  193. {
  194. // a colored dot sweeping back and forth, with fading trails
  195. fadeToBlackBy( leds, NUM_LEDS, 20);
  196. int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
  197. leds[pos] += CHSV( gHue, 255, 192);
  198. }
  199.  
  200. // Pattern Animation - Preset: Three Step
  201. void threeStep()
  202. {
  203. uint8_t colorIndex = gHue;
  204. for( int i = 0; i < NUM_LEDS; i++) {
  205. leds[i] = ColorFromPalette( currentPalette, colorIndex);
  206. colorIndex += 3;
  207. }
  208. }
  209.  
  210. // Pattern Animation - Preset: BPM
  211. void bpm()
  212. {
  213. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  214. uint8_t BeatsPerMinute = 300;
  215. //CRGBPalette16 palette = PartyColors_p;
  216. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  217. for ( int i = 0; i < NUM_LEDS; i++) { //9948
  218. leds[i] = ColorFromPalette(currentPalette, gHue + (i * 2), beat - gHue + (i * 10));
  219. }
  220. }
  221.  
  222.  
  223. // Pattern Animation - Preset: Juggle
  224. void juggle() {
  225. // eight colored dots, weaving in and out of sync with each other
  226. fadeToBlackBy( leds, NUM_LEDS, 20);
  227. byte dothue = 0;
  228. for ( int i = 0; i < 8; i++) {
  229. leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
  230. dothue += 32;
  231. }
  232. }
  233.  
  234.  
  235. // ----------------------- Custom animation calls --------------------
  236.  
  237.  
  238. // Pattern Animation - Custom Scrolling Line
  239. void customScrollingRowPattern() {
  240.  
  241. customPattern(scrollingRowPattern, scrollingColors, false, 2, 16);
  242. }
  243.  
  244.  
  245. // Pattern Animation - Custom Scrolling Column
  246. void customScrollingColumnPattern() {
  247.  
  248. customPattern(scrollingColumnPattern, scrollingColors, false, 2, 16);
  249. }
  250.  
  251.  
  252. // Pattern Animation - Custom Outwards Columns
  253. void customOutwardsColumnPattern() {
  254.  
  255. customPattern(outwardsColumnPattern, scrollingColors, false, 2, 16);
  256. }
  257.  
  258.  
  259. // Pattern Animation - Custom Outwards Wings
  260. void customOutwardsWingsPattern() {
  261.  
  262. customPattern(outwardsWingsPattern, scrollingColors, true, 4, 16);
  263. }
  264.  
  265.  
  266. // Pattern Animation - Custom Circles
  267. void customCirclesPattern() {
  268.  
  269. customPattern(circlesPattern, scrollingColors, false, 2, 16);
  270. }
  271.  
  272.  
  273. // Pattern Animation - Custom Triangles
  274. void customTrianglesPattern() {
  275.  
  276. customPattern(trianglesPattern, scrollingColors, false, 2, 16);
  277. }
  278.  
  279.  
  280. void customOneTwoPattern() {
  281.  
  282. customPattern(oneTwoPattern, scrollingColors, false, 2, 16);
  283. }
  284.  
  285.  
  286. // Pattern Animation - Custom Big V
  287. void customBigVPattern() {
  288.  
  289. customPattern(bigVPattern, scrollingColors, false, 3, 265);
  290. }
  291.  
  292.  
  293. // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ANIMATION FUNCTIONS END XXXXXXXXXXXXXXXXXXXXX
  294.  
  295. // --------------------------------------------------------------- customPattern () START -----------------------------------
  296.  
  297. //Line Flipping for customPattern - Function for converting order of LEDs every other row
  298. uint_least8_t FlipIndex(uint_least8_t nIndex, uint_least8_t nMidpoint)
  299. {
  300. return nMidpoint * 2 - nIndex;
  301. }
  302.  
  303.  
  304. // Line Flipping Check for customPattern - Function for checking if current row needs to be converted or not (runs FlipIndex function)
  305. uint_least8_t ConvertIndex(uint_least8_t i)
  306. {
  307. if (15 <= i && i <= 28) { // Check if currently on 2nd line
  308. return FlipIndex(i, 21) + 1;
  309. }
  310. if (42 <= i && i <= 53) { // Check if currently on 4th line
  311. return FlipIndex(i, 47) + 1;
  312. }
  313. if (65 <= i && i <= 74) { // Check if currently on 6th line
  314. return FlipIndex(i, 69) + 1;
  315. }
  316. return i; // if on 1st, 3rd, 5th or 7th line, don't flip position
  317. }
  318.  
  319. void customPattern(uint_least8_t pattern[NUM_LEDS], uint_least8_t patternColors[], bool reverse, float speed, uint_least8_t max) {
  320. // Function that creates the pattern animation using these variables:
  321. // pattern - the selected layout of the animation
  322. // patternColors - sequence of colors from palette to indexes
  323. // reverse - choose direction through true / false
  324. // speed - adjust the run speed of a loop, bigger number is slower
  325. // max - select the max number of colors from the color palette (jumps of 16 on gradient scale 0 - 255, total of 16 steps)
  326.  
  327.  
  328. currentBlending = LINEARBLEND;
  329.  
  330.  
  331. for (uint_least8_t x = 0; x < max; x++) { // Run through all LED Indexs, until you hit the MAX allowed index number
  332. potentialBrightness(); // Adjust brightness based on check
  333. for (uint_least8_t z = 0; z < (4 * speed); z++) { // Speed adjustment
  334. for (uint_least8_t i = 0; i < NUM_LEDS; i++) { // Run through all the LEDs, until you hit the last LED
  335. if ((buttonPaletteState != lastButtonPaletteState) || (buttonAnimationState != lastButtonAnimationState)){
  336. break; // Check for buttoon press and break early
  337. }
  338. uint_least8_t convertedI = ConvertIndex(i); // Run through line flipping function, flip if needed to swap LED position
  339. uint8_t color = (pattern[i] + x) % max; // Color is the index of the current LED, will be used in the next
  340. // function to map to the color bridge patternColors[], so LEDs with
  341. // the same index on the pattern get colored the same
  342.  
  343. leds[convertedI] = ColorFromPalette(currentPalette, patternColors[color]*16, 255, currentBlending);
  344. // color the LED using the currently selected pattern, with the
  345. // selected gradient value based on (color), assuming gradient is in
  346. // equal jumps of 16.
  347. // CHANGE TO THE ABOVE: Instead of jumps of 16, run through only the
  348. // declared colors on the gradient no matter the size, but skip the
  349. //ranges between them
  350. } // i Loop end, run through all LEDs
  351. FastLED.delay(1); // Delay by X milliseconds
  352. } // z Loop end, speed adjustment
  353. } // x loop end, run over pattern indexes
  354. } // customPattern() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement