Timer Sequence Code Example from Livestream 07.02.2020

Hi All,

If you aren’t already subscribed to our YouTube channel, you should join us, we live stream every Tuesday and Thursday.

Today, I live coded to show some possible use of timers with animations, ending with a simple animation sequencer. Here is the code from the livestream :slight_smile:

/*
 * Timer for animation sequence
 */

Timer seqTimer;

enum Sequences{
  FLASH,        // = 0
  SPARKLE,      // = 1
  RAINBOW,      // = 2
  NUM_SEQUENCES // = 3
};

byte seqIndex = FLASH;
 
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

  if(seqTimer.isExpired()) {
    seqTimer.set(2000);
    seqIndex = (seqIndex + 1) % NUM_SEQUENCES;
  }

  switch(seqIndex) {
    case FLASH: 
      displayFlash();
      break;
    case SPARKLE: 
      displaySparkle();
      break;
    case RAINBOW: 
      displayRainbow();
      break;
    default:
      setColor(RED);
      break;
  }
  
}

void displayFlash() {
  setColor(dim(WHITE, seqTimer.getRemaining()/8));
}

void displaySparkle() {
  FOREACH_FACE(f) {
    setColorOnFace(dim(WHITE, 51 + 51*random(4)), f);
  }
}

void displayRainbow() {
  setColorOnFace(RED, 0);
  setColorOnFace(ORANGE, 1);
  setColorOnFace(YELLOW, 2);
  setColorOnFace(GREEN, 3);
  setColorOnFace(BLUE, 4);
  setColorOnFace(MAGENTA, 5);
}

4 Likes