c - How can I replace delay() by millis()? -
after while, final result want can't use delay because need different time different strip, need replace delay()
millis()
in code:
#include <fastled.h> #define num_leds1 10 #define num_leds2 6 #define data_pin1 6 #define data_pin2 7 crgb leds1[num_leds1]; crgb leds2[num_leds2]; void setup() { fastled.addleds<neopixel, data_pin1>(leds1, num_leds1); fastled.addleds<neopixel, data_pin2>(leds2, num_leds2); } int dot_delay1[ ] = { 100,200,300,400,500,600,700,800,900,1000 }; int dot_delay2[ ] = { 100,200,300,400,500,600 }; void loop() { for(int dot = 0; dot < num_leds1; dot++) for(int dot = 0; dot < num_leds2; dot++) { leds1[dot] = crgb::blue; leds2[dot] = crgb::blue; fastled.show(); leds1[dot] = crgb::black; leds2[dot] = crgb::black; delay( dot_delay1[ dot ] ); // need put second delay, // can't put more 1 delay. // need refactor code millis() function instead of delay() } }
you can use non-blocking code patterns, executed in special frequencies or times. in following, give short example how can replace delay(1000)
, delay(5000)
without blocking main loop. in addition, can check stackoverflow similar posts (e.g. pause without delay() arduino).
// 1 sec. frequency unsigned long interval=1000; // time need wait unsigned long previousmillis=0; // millis() returns unsigned long. // 5 sec. frequency unsigned long interval1=5000; // time need wait unsigned long previousmillis1=0; // millis() returns unsigned long. void setup() { //... } void loop() { // other cmd's... // replace delay(1000) if ((unsigned long)(millis() - previousmillis) >= interval) { previousmillis = millis(); // every first second // ... } // other cmd's... // replace delay(5000) if ((unsigned long)(millis() - previousmillis1) >= interval1) { previousmillis1 = millis(); // every fifth second // ... } // other cmd's... }
Comments
Post a Comment