Advertisement
atuline

Delay vs millis vs EVERY_N_MILLIS

Jan 4th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. /* Using millis or EVERY_N_MILLIS instead of delay statement in double for loops.
  2.  *  
  3.  *  By: Andrew Tuline
  4.  *  
  5.  *  Date: Jan, 2020
  6.  *  
  7.  *  If I can ditch a delay statement, I sure will.
  8.  *  
  9.  */
  10.  
  11.  
  12. #include <FastLED.h>
  13.  
  14. void setup(){
  15.   Serial.begin(115200);
  16. }
  17.  
  18.  
  19. void loop(){
  20.  
  21. //  colorWipe(100);                         // Modified original with delays
  22. //  testaWipe(100);                         // Using EVERY_N_MILLIS inside function
  23. //  EVERY_N_MILLIS(100) { testWipe(); }     // Using EVERY_N_MILLIS outside function
  24.   milliWipe(100);
  25. }
  26.  
  27.  
  28. void milliWipe(uint8_t times) {             // This one doesn't delay at all, but repeats the same thing until the counter increases. No harm, no foul.
  29.   static long counte = 0;
  30.   counte = millis() / times;
  31.   counte = counte % 56;
  32.   uint8_t k = counte % 7;
  33.   uint8_t j = counte / 8;
  34.   Serial.print(counte); Serial.print(" "); Serial.print(j); Serial.print(" "); Serial.println(k);
  35. }
  36.  
  37.  
  38. void testWipe() {
  39.   static uint8_t counte = 0;
  40.   counte +=1;
  41.   counte = counte % 56;
  42.   uint8_t k = counte % 7;
  43.   uint8_t j = counte / 8;
  44.   Serial.print(counte); Serial.print(" "); Serial.print(j); Serial.print(" "); Serial.println(k);
  45. } // testWipe()
  46.  
  47.  
  48.  
  49. void testaWipe(uint8_t times) {
  50.   EVERY_N_MILLIS(times) {
  51.     static uint8_t counte = 0;
  52.     counte +=1;
  53.     counte = counte % 56;
  54.     uint8_t k = counte % 7;
  55.     uint8_t j = counte / 8;
  56.     Serial.print(counte); Serial.print(" "); Serial.print(j); Serial.print(" "); Serial.println(k);
  57.   }
  58. } // testaWipe()
  59.  
  60.  
  61.  
  62. void colorWipe(uint8_t wait) {
  63.   for(uint16_t j=0; j<8; j++) {
  64.     for(uint16_t k=0; k<7; k++) {
  65.       Serial.print(j*7+k); Serial.print(" "); Serial.print(j); Serial.print(" "); Serial.println(k);
  66.       FastLED.delay(wait);
  67.     }
  68.   }
  69. } // colorWipe()
  70.  
  71.  
  72. /*
  73. void cd77_8X8_spiral_colorWipe(uint8_t ghue, uint8_t wait) {
  74.   for(uint16_t j=0; j<bigboxarray_Size; j++) {
  75.     uint16_t x=boxtotalarray[j];
  76.     for(uint16_t k=0; k<x; k++) {
  77.       leds[bigboxarray[j][k]]= CHSV(ghue, 255,255);
  78.       FastLED.delay(wait);
  79.     }
  80.   }
  81. }
  82.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement