Guest User

Demo1100 with button

a guest
Jul 6th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.45 KB | None | 0 0
  1. //***************************************************************
  2. // This is Mark Kriegsman's FastLED DemoReel100 example with
  3. // a modificaiton to use a button for changing patterns. The
  4. // timer used for picking a new pattern has been commented out
  5. // and there is a button check inside the main loop now.
  6. //
  7. // Search for "button" to find the various button related
  8. // code additions.
  9. //
  10. // You can view the serial monitor to see a message when the
  11. // button is pressed.
  12. //
  13. // Marc Miller, March 2017
  14. // Updated Jan 2020 - for JC_button library updates
  15. //***************************************************************
  16.  
  17. #include <FastLED.h>
  18. #define DATA_PIN 5
  19. #define CLK_PIN 13
  20. #define LED_TYPE WS2811
  21. #define COLOR_ORDER GRB
  22. #define NUM_LEDS 40
  23. CRGB leds[NUM_LEDS];
  24. #define BRIGHTNESS 100
  25. #define FRAMES_PER_SECOND 120
  26.  
  27.  
  28.  
  29. //---------------------------------------------------------------
  30. // This uses JChristensen's Button Library from:
  31. // https://github.com/JChristensen/JC_Button
  32. #include "JC_Button.h" // Include Button library
  33. const uint8_t buttonPin = 2; // Set digital pin used with debounced pushbutton
  34. Button myButton(buttonPin, true, true, 50); // Declare the button
  35.  
  36.  
  37. //---------------------------------------------------------------
  38. void setup() {
  39. Serial.begin(115200); // Allows serial monitor output (check baud rate)
  40. delay(1500); // short delay for recovery
  41. //FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  42. FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  43. FastLED.setBrightness(BRIGHTNESS);
  44. FastLED.clear();
  45.  
  46. myButton.begin(); // initialize the button object
  47.  
  48. Serial.println("Setup done.\n");
  49. }
  50.  
  51.  
  52. // List of patterns to cycle through. Each is defined as a separate function below.
  53. typedef void (*SimplePatternList[])();
  54. //SimplePatternList gPatterns = { applause, fillAndCC, blinkyblink1, blinkyblink2, spewFour, spew, confetti_GB, rainbow, confetti, sinelon, juggle };
  55. SimplePatternList gPatterns = { twoDots, fillAndCC, blinkyblink2, spewFour, spew, confetti_GB, rainbow, confetti, sinelon, juggle };
  56.  
  57. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  58. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  59.  
  60.  
  61. //---------------------------------------------------------------
  62. void loop()
  63. {
  64. // BUTTON STUFF
  65. // Not using this timer to change patterns any more. Instead check the button.
  66. // EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  67. readbutton(); // check for button press
  68.  
  69. // Call the current pattern function once, updating the 'leds' array
  70. gPatterns[gCurrentPatternNumber]();
  71.  
  72. // send the 'leds' array out to the actual LED strip
  73. FastLED.show();
  74. // insert a delay to keep the framerate modest
  75. FastLED.delay(1000/FRAMES_PER_SECOND);
  76.  
  77. // do some periodic updates
  78. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  79.  
  80. }//end_main_loop
  81.  
  82. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  83.  
  84.  
  85. //---------------------------------------------------------------
  86. void nextPattern()
  87. {
  88. // add one to the current pattern number, and wrap around at the end
  89. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  90. }
  91.  
  92. //////////////////////////
  93. void rainbow()
  94. {
  95. // FastLED's built-in rainbow generator
  96. fill_rainbow( leds, NUM_LEDS, gHue, 12);
  97. }
  98.  
  99. //////////////////////////
  100. void confetti()
  101. {
  102. // random colored speckles that blink in and fade smoothly
  103. fadeToBlackBy( leds, NUM_LEDS, 20);
  104. int pos = random16(NUM_LEDS);
  105. leds[pos] += CHSV( gHue + random8(64), random8(128,200), random8(48,255));
  106. }
  107.  
  108.  
  109. //////////////////////////
  110. void confetti_GB()
  111. {
  112. // random colored speckles, Green and Blue hues only
  113. // Green = 96, Blue = 160
  114. uint8_t p = 25; // What percentage of the time to make speckles. [Range 0-100]
  115.  
  116. fadeToBlackBy( leds, NUM_LEDS, 10);
  117. if (random8(100) < p) {
  118. int pos = random16(NUM_LEDS);
  119. uint8_t hue = random8(2); // randomly chooses a 0 or 1
  120. if (hue == 0) {
  121. hue = random8(92,111); // pick a hue somewhere in the green zone
  122. } else {
  123. hue = random8(156,165); // pick a hue somewhere in the blue zone
  124. }
  125. leds[pos] += CHSV( hue, random8(200,240), 255);
  126. }
  127. }//end confetti_GB
  128.  
  129.  
  130. //////////////////////////
  131. void sinelon()
  132. {
  133. // a colored dot sweeping back and forth, with fading trails
  134. fadeToBlackBy( leds, NUM_LEDS, 12);
  135. int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  136. leds[pos] += CHSV( gHue, 255, 192);
  137. }
  138.  
  139. //////////////////////////
  140. void juggle() {
  141. // four colored dots, weaving in and out of sync with each other
  142. fadeToBlackBy( leds, NUM_LEDS, 20);
  143. byte dothue = 0;
  144. for( int i = 0; i < 4; i++) {
  145. leds[beatsin16( i+5, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  146. dothue += 32;
  147. }
  148. }
  149.  
  150.  
  151.  
  152. //////////////////////////
  153. void spew() {
  154. const uint16_t spewSpeed = 100; // rate of advance
  155. static boolean spewing = 0; // pixels are On(1) or Off(0)
  156. static uint8_t count = 1; // how many to light (or not light)
  157. static uint8_t temp = count;
  158. static uint8_t hue = random8();
  159. EVERY_N_MILLISECONDS(spewSpeed){
  160. if (count == 0) {
  161. spewing = !spewing;
  162. if (spewing == 1) { count = random8(2,5); } // random number for On pixels
  163. else { count = random8(1,8); } // random number for Off pixels
  164. temp = count;
  165. //gHue = gHue - 30;
  166. hue = random8();
  167. }
  168. for (uint8_t i = NUM_LEDS-1; i > 0; i--) {
  169. leds[i] = leds[i-1]; // shift data down the line by one pixel
  170. }
  171. if (spewing == 1) { // new pixels are On
  172. if (temp == count) {
  173. leds[0] = CHSV(hue-5, 215, 255); // for first dot
  174. //leds[0] = CHSV(gHue-5, 215, 255); // for first dot
  175. } else {
  176. leds[0] = CHSV(hue, 255, 255/(1+((temp-count)*2)) ); // for following dots
  177. //leds[0] = CHSV(gHue, 255, 255/(1+((temp-count)*2)) ); // for following dots
  178. }
  179. } else { // new pixels are Off
  180. leds[0] = CHSV(0,0,0); // set pixel 0 to black
  181. }
  182. count = count - 1; // reduce count by one.
  183. }//end every_n
  184. }//end spew
  185.  
  186. //////////////////////////
  187. void spewFour() {
  188. //Similar to the abouve "spew", but split up into four sections,
  189. //specifically designed for a 8x4 matrix with Z-layout.
  190. const uint16_t spewSpeed = 100; // rate of advance
  191. static uint8_t spewing[4] = {0,0,0,0}; // pixels are On(1) or Off(0)
  192. static uint8_t count[4] = {1,1,1,1}; // how many to light (or not light)
  193. static uint8_t temp[4] = {count[0],count[1],count[2],count[3]};
  194. static uint8_t hue[4] = {random8(), random8(), random8(), random8()};
  195. EVERY_N_MILLISECONDS(spewSpeed){
  196. for (uint8_t j=0; j<4; j++) {
  197. if (count[j] == 0) {
  198. if (spewing[j] == 0) {
  199. spewing[j] = 1;
  200. } else {
  201. spewing[j] = 0;
  202. }
  203. if (spewing[j] == 1) { count[j] = random8(2,5); } // random number for On pixels
  204. else { count[j] = random8(1,8); } // random number for Off pixels
  205. temp[j] = count[j];
  206. EVERY_N_SECONDS(2){ // hue going across is constant for awhile
  207. hue[j] = random8();
  208. }
  209. }
  210. for (uint8_t i = 7; i > 0; i--) {
  211. leds[(j*8)+i] = leds[(j*8)+i-1]; // shift data down the line by one pixel
  212. }
  213. if (spewing[j] == 1) { // new pixels are On
  214. if (temp[j] == count[j]) {
  215. leds[((j+1)*8)-8] = CHSV(hue[j]-5, 215, 255); // for first dot
  216. } else {
  217. leds[((j+1)*8)-8] = CHSV(hue[j], 255, 255/(1+((temp[j]-count[j])*2)) ); // for following dots
  218. }
  219. } else { // new pixels are Off
  220. leds[((j+1)*8)-8] = CHSV(0,0,0); // set pixel 0 to black
  221. }
  222. count[j] = count[j] - 1; // reduce count by one.
  223. }//end for loop
  224. }//end every_n
  225. }//end spewFour
  226.  
  227.  
  228. //////////////////////////
  229. void blinkyblink1() {
  230. static boolean dataIncoming = LOW;
  231. static boolean blinkGate1 = LOW;
  232. static boolean blinkGate2 = HIGH;
  233. static int8_t count = -1;
  234.  
  235. EVERY_N_MILLISECONDS_I( timingObj, 250 ) {
  236. count++;
  237. if (count == 6) { count = 0; }
  238. blinkGate2 = count;
  239. dataIncoming = !dataIncoming;
  240. blinkGate1 = !blinkGate1;
  241. //Serial.print("c: "); Serial.print(count); Serial.print("\t");
  242. //Serial.print(dataIncoming); Serial.print(" "); Serial.print(blinkGate1);
  243. //Serial.print("\t"); Serial.print(dataIncoming * blinkGate1 * 255 * blinkGate2);
  244. //Serial.print("\tb: "); Serial.print(blinkGate2); Serial.println(" ");
  245. FastLED.clear();
  246. leds[0] = CHSV(gHue,0,dataIncoming*blinkGate1*255 * blinkGate2);
  247. if (count == 2 || count == 3) {
  248. timingObj.setPeriod( 50 );
  249. } else if (count == 4) {
  250. timingObj.setPeriod( 405 );
  251. } else {
  252. timingObj.setPeriod( 165 );
  253. }
  254. }
  255. }//end_blinkyblink1
  256.  
  257.  
  258. //////////////////////////
  259. void blinkyblink2() {
  260. static boolean dataIncoming = LOW;
  261. static boolean blinkGate1 = LOW;
  262. static boolean blinkGate2 = HIGH;
  263. static int8_t count = -1;
  264. static int8_t P;
  265.  
  266. EVERY_N_MILLISECONDS_I( timingObj, 250 ) {
  267. count++;
  268. if (count == 8) {
  269. count = 0;
  270. P = random8(NUM_LEDS);
  271. }
  272. blinkGate2 = count;
  273. dataIncoming = !dataIncoming;
  274. blinkGate1 = !blinkGate1;
  275. //Serial.print("c: "); Serial.print(count); Serial.print("\t");
  276. //Serial.print(dataIncoming); Serial.print(" "); Serial.print(blinkGate1);
  277. //Serial.print("\t"); Serial.print(dataIncoming * blinkGate1 * 255 * blinkGate2);
  278. //Serial.print("\tb: "); Serial.print(blinkGate2); Serial.println(" ");
  279. FastLED.clear();
  280. leds[P] = CHSV(gHue,255,dataIncoming*blinkGate1*255 * blinkGate2);
  281. if (count == 6) {
  282. timingObj.setPeriod( 250 );
  283. } else if (count == 7) {
  284. timingObj.setPeriod( 500 );
  285. } else {
  286. timingObj.setPeriod( 25 );
  287. }
  288. }
  289. }//end_blinkyblink2
  290.  
  291.  
  292. //////////////////////////
  293. void fillAndCC() {
  294. static int16_t pos = 0; // position along strip
  295. static int8_t delta = 3; // delta (can be negative, and/or odd numbers)
  296. static uint8_t hue = 0; // hue to display
  297. EVERY_N_MILLISECONDS(50) {
  298. leds[pos] = CHSV(hue,255,255);
  299. pos = (pos + delta + NUM_LEDS) % NUM_LEDS;
  300. if (delta >= 0 && pos == 0) { //going forward
  301. hue = hue + random8(42,128);
  302. }
  303. if (delta < 0 && pos == NUM_LEDS-1) { //going backward
  304. hue = hue + random8(42,128);
  305. }
  306. }
  307. }//fillAndCC
  308.  
  309.  
  310.  
  311. //////////////////////////
  312. void applause() //originally by Mark K.
  313. {
  314. //Number of loops is increased based on number of pixels.
  315. //Fade ammount drops as pixels increase.
  316. static uint8_t appAmmount = (NUM_LEDS/32);
  317. for (uint8_t i=0; i<appAmmount; i++) {
  318. static uint16_t lastPixel = 0;
  319. fadeToBlackBy( leds, NUM_LEDS, 32/appAmmount ); //was 32
  320. leds[lastPixel] = CHSV(random8(HUE_BLUE,HUE_PURPLE),255,255);
  321. lastPixel = random16(NUM_LEDS);
  322. leds[lastPixel] = CRGB::White;
  323. }
  324. }
  325.  
  326.  
  327. //////////////////////////
  328. void twoDots() {
  329. static uint8_t pos; //used to keep track of position
  330. EVERY_N_MILLISECONDS(70) {
  331. fadeToBlackBy( leds, NUM_LEDS, 200); //fade all the pixels some
  332. leds[pos] = CHSV(gHue, random8(170,230), 255);
  333. leds[(pos+5) % NUM_LEDS] = CHSV(gHue+64, random8(170,230), 255);
  334. pos = pos + 1; //advance position
  335.  
  336. //This following check is very important. Do not go past the last pixel!
  337. if (pos == NUM_LEDS) { pos = 0; } //reset to beginning
  338. //Trying to write data to non-existent pixels causes bad things.
  339. }
  340. }//end_twoDots
  341.  
  342.  
  343.  
  344. //---------------------------------------------------------------
  345.  
  346. //---------Function to read the button and do something----------
  347. // This section could be changed to use a potentiometer,
  348. // encoder, or something else to trigger a pattern change.
  349. // Depending on what was used, the nextPattern() function
  350. // might need to be updated as well to have things work
  351. // the way you intend.
  352.  
  353. void readbutton() {
  354. myButton.read();
  355. if(myButton.wasPressed()) {
  356. Serial.println("Button pressed! Next pattern... ");
  357. nextPattern(); // Change to the next pattern
  358.  
  359. //Flash pixel zero white as a visual that button was pressed.
  360. leds[0] = CHSV(0,0,255); //Set first pixel color white
  361. FastLED.show(); //Update display
  362. delay(100); //Short pause so we can see leds[0] flash on
  363. leds[0] = CRGB::Black; //Set first pixel off
  364. FastLED.show();
  365. delay(100);
  366. }
  367. }//end_readbutton
Advertisement
Add Comment
Please, Sign In to add comment