User Tools

Site Tools


using_delays

Using Delays

The delay() function call can cause problems when you want to add additional functionality to your code, such as external controls or multiple strings or functions.

Bad delay code:

void loop() {

  // do something
  delay(1000);
}

Hand written delay code:

unsigned long timer;                        // the timer
unsigned long interval = 1000;              // the repeat interval

void setup() {
  timer = millis();                         // start timer
}

void loop() {
  if ((millis()-timer) > interval) {        // timed out
    timer += interval;                      // reset timer by moving it along to the next interval 
    // do something
  }
}

Using the FastLED scheduler:

void loop() {
  EVERY_N_MILLIS(10) {
    afunction();
  }
  FastLED.show();
}

void afunction() {
  // do something
}

You could use timer based library function such as beatsin8():

void loop() {
  uint8_t myhue = beatsin8(10,0,255)
  fill_solid(leds,NUM_LEDS, CHSV(myhue, 255,255));
  FastLED.show();
}
using_delays.txt · Last modified: 2018/11/10 14:31 by atuline