Guest User

Title: DemoReel100 with Button and a little more

a guest
Aug 11th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1.  
  2. /*
  3.  
  4. Title: DemoReel100 with Button and a little more
  5.  
  6. By: Mark Kriegsman
  7.  
  8. Button added by: Andrew Tuline
  9.  
  10. Pot-controlled Cylon pattern added by: Kevin Althans
  11.  
  12.  
  13. - FastLED library (get 3.1) from https://github.com/FastLED/FastLED
  14. - JChristensen's Button Library from https://github.com/JChristensen/Button
  15. */
  16.  
  17. #include "FastLED.h"
  18. #include "Button.h" // Button library. Includes press, long press, double press detection.
  19.  
  20.  
  21.  
  22. // FastLED "100-lines-of-code" demo reel, showing just a few
  23. // of the kinds of animation patterns you can quickly and easily
  24. // compose using FastLED.
  25. //
  26. // This example also shows one easy way to define multiple
  27. // animations patterns and have them automatically rotate.
  28. //
  29. // -Mark Kriegsman, December 2014
  30. //
  31. // I'm trying to adapt this code to include some other loop-based patterns...
  32. // having a little trouble getting that extra loop to break out on button.
  33.  
  34.  
  35. #if FASTLED_VERSION < 3001000
  36. #error "Requires FastLED 3.1 or later; check github for latest code."
  37. #endif
  38.  
  39. // Pushbutton pin definition
  40. const int buttonPin = 12; // Digital pin used for debounced pushbutton
  41.  
  42. // int buttonState = 0;
  43. // int lastButtonState = 0;
  44. Button myBtn(buttonPin, true, true, 50); // Declare the button
  45.  
  46.  
  47.  
  48. #define DATA_PIN 6
  49. #define CLK_PIN 13
  50. #define LED_TYPE WS2812
  51. #define COLOR_ORDER GRB
  52. #define NUM_LEDS 50
  53. CRGB leds[NUM_LEDS];
  54.  
  55. #define FRAMES_PER_SECOND 120
  56.  
  57. uint8_t max_bright = 64;
  58.  
  59. void setup() {
  60. Serial.begin(57600);
  61. delay(3000); // 3 second delay for recovery
  62.  
  63. FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  64. //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  65.  
  66. // set master brightness control
  67. FastLED.setBrightness(max_bright);
  68. }
  69.  
  70.  
  71. // List of patterns to cycle through. Each is defined as a separate function below.
  72. typedef void (*SimplePatternList[])();
  73. SimplePatternList gPatterns = {rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm, cylon};
  74.  
  75. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  76. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  77.  
  78. void loop()
  79. {
  80. // Call the current pattern function once, updating the 'leds' array
  81. gPatterns[gCurrentPatternNumber]();
  82.  
  83. // send the 'leds' array out to the actual LED strip
  84. FastLED.show();
  85. // insert a delay to keep the framerate modest
  86. FastLED.delay(1000/FRAMES_PER_SECOND);
  87.  
  88. // do some periodic updates
  89. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  90. // EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  91.  
  92. readbutton(); // Button press increases the ledMode up to last contiguous mode and then starts over at 0.
  93.  
  94. }
  95.  
  96. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  97.  
  98. void nextPattern()
  99. {
  100. // add one to the current pattern number, and wrap around at the end
  101. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  102. }
  103.  
  104. void rainbow()
  105. {
  106. // FastLED's built-in rainbow generator
  107. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  108. }
  109.  
  110. void rainbowWithGlitter()
  111. {
  112. // built-in FastLED rainbow, plus some random sparkly glitter
  113. rainbow();
  114. addGlitter(80);
  115. }
  116.  
  117. void addGlitter( fract8 chanceOfGlitter)
  118. {
  119. if( random8() < chanceOfGlitter) {
  120. leds[ random16(NUM_LEDS) ] += CRGB::White;
  121. }
  122. }
  123.  
  124. void confetti()
  125. {
  126. // random colored speckles that blink in and fade smoothly
  127. fadeToBlackBy( leds, NUM_LEDS, 10);
  128. int pos = random16(NUM_LEDS);
  129. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  130. }
  131.  
  132. void sinelon()
  133. {
  134. // a colored dot sweeping back and forth, with fading trails
  135. fadeToBlackBy( leds, NUM_LEDS, 20);
  136. int pos = beatsin16(13,0,NUM_LEDS);
  137. leds[pos] += CHSV( gHue, 255, 192);
  138. }
  139.  
  140. void bpm()
  141. {
  142. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  143. uint8_t BeatsPerMinute = 62;
  144. CRGBPalette16 palette = PartyColors_p;
  145. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  146. for( int i = 0; i < NUM_LEDS; i++) { //9948
  147. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  148. }
  149. }
  150.  
  151. void juggle() {
  152. // eight colored dots, weaving in and out of sync with each other
  153. fadeToBlackBy( leds, NUM_LEDS, 20);
  154. byte dothue = 0;
  155. for( int i = 0; i < 8; i++) {
  156. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  157. dothue += 32;
  158. }
  159. }
  160.  
  161. void cylon() {
  162. const int DelayPot = A0; //the Delay potentiometer pin
  163. const int BrightnessPot = A1; //the brightness of "V" pot pin
  164. const int HuePot = A2; //a hue changing value
  165. int DelayPotRead = 0; //the value from the Delay potentiometer
  166. int Delay = 0; //the Delay pot's value converted to a delay value
  167. int BrightnessPotRead = 0; // Brightness value from pot
  168. int BrightnessValue = 0; //start off at 0% briteness
  169. int DelayMin = 0; //DelayMinimum value of the delay
  170. int DelayMax = 30; //DelayMax value of delay
  171. int HuePotRead = 0; //Set Hue to 0
  172.  
  173. static uint8_t hue = 0;
  174. Serial.println("loop");
  175. // First slide the led in one direction
  176. for(int i = 0; i < NUM_LEDS; i++) {
  177.  
  178. DelayPotRead = analogRead(DelayPot); //get reading from Delay pot and convert to Delay
  179. Delay = map(DelayPotRead,0,1023,DelayMin,DelayMax); //map pot reading to a value between DelayMin and DelayMax
  180. BrightnessPotRead = analogRead(BrightnessPot); //get reading from brightness pot and convert to BrightnessValue
  181. BrightnessValue = map(BrightnessPotRead,0,1023,255,64); //map pot reading to a V or brightness value from 64 to 255, reversed
  182. HuePotRead = analogRead(HuePot);
  183.  
  184. Serial.print(" Delay=");
  185. Serial.println(Delay);
  186. Serial.print(" Brightness=");
  187. Serial.println(BrightnessValue);
  188. Serial.print(" hue=");
  189. Serial.println(hue);
  190.  
  191. if (HuePotRead < 50)
  192. {
  193. hue++;
  194. }
  195. else
  196. {
  197. hue = map(HuePotRead,0,1023,255,0);
  198. }
  199. leds[i] = CHSV(hue, 255, BrightnessValue); // Set the i'th led to whatever color
  200.  
  201. FastLED.show(); // Show the leds
  202. for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250);} // Fade out the LEDs
  203. delay(Delay); // Wait a little bit before we loop around and do it again
  204. }
  205.  
  206.  
  207. // Now go in the other direction.
  208. for(int i = (NUM_LEDS)-1; i >= 0; i--) {
  209.  
  210. DelayPotRead = analogRead(DelayPot); //get reading from Delay pot and convert to Delay
  211. Delay = map(DelayPotRead,0,1023,DelayMin,DelayMax);
  212. BrightnessPotRead = analogRead(BrightnessPot); //get reading from brightness pot and convert to BrightnessValue
  213. BrightnessValue = map(BrightnessPotRead,0,1023,255,64); //map pot reading to a V or brightness value from 64 to 255, reversed
  214. HuePotRead = analogRead(HuePot);
  215.  
  216. Serial.print(" Delay=");
  217. Serial.println(Delay);
  218. Serial.print(" Brightness=");
  219. Serial.println(BrightnessValue);
  220. Serial.print(" hue=");
  221. Serial.println(hue);
  222.  
  223.  
  224. if (HuePotRead < 50) // create a deadband for pot to trigger “rainbow” mode
  225. {
  226. hue++; // this is rainbow mode
  227. }
  228. else
  229. {
  230. hue = map(HuePotRead,0,1023,255,0); // map pot values to 8bit space
  231. }
  232. leds[i] = CHSV(hue, 255, BrightnessValue); // Set the i'th led to red
  233.  
  234. FastLED.show(); // Show the leds
  235. for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250);} // Fade out the LEDs
  236. delay(Delay); // Wait a little bit before we loop around and do it again
  237. }
  238. }
  239.  
  240.  
  241. void readbutton() { // Read the button and increase the mode
  242. myBtn.read();
  243. if(myBtn.wasReleased()) {
  244. nextPattern();
  245. }
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment