Help with my fallback routine / Timer issues


After some successful projects, I'm struggeling with this simple code:

The first trigger impulse activates flash1. If there is a second trigger impuls, then flash2 will be triggered. So far, so good.

I'd like to implement a fallback routine, that switches back to flash1 again if there is no second trigger impulse within e. g. 5 seconds.

I've tried the TimerOne library, but in my code the fallback timer runs free so there is no defined timing.

I'd have a timer, that starts after the first trigger and is waiting for the next trigger within 5 seconds. If there is no second trigger within 5 sec. then flash1 should become active again.

Any ideas?


#include "TimerOne.h" 
#include "pitches.h"

int flash1 = 2; // Front Flash
int flash2 = 3; // Mask Flash
int button = 5; // Trigger
int trig;  // Trigger State
int count; // Flash switching
int fallbacktime=5;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(flash1, OUTPUT);
  pinMode(flash2, OUTPUT);
  pinMode(trig, INPUT);
  digitalWrite(flash1, LOW);
  digitalWrite(flash2, LOW);

// fallback tryout

void fb() {
      if (count == 1) {
        count = 0;   
        tone(8, 2000, 70);

// the loop 

void loop() {

  trig = digitalRead(button); 

  // waiting for the very first flash trigger signal
  if (trig == HIGH && count == 0) {
    digitalWrite(flash1, HIGH);
    delay (20);
    count = 1; // switch to the second flash
    digitalWrite(flash1, LOW);
    delay (100);

  // if there is a second flash trigger within 5 seconds, trigger the second flash
  if (trig == HIGH && count == 1) {
    digitalWrite(flash2, HIGH);
    delay (20);
    digitalWrite(flash2, LOW);
    delay (100);
    count = 0; // switch back to the first flash

When flash 1 happens save the current value of millis(). At the end of loop() test whether 5 seconds have elapsed since the first flash

if (millis() - flash1Time >= 5000)

and set count back to 0 if it has.

The delay()s are a nuisance and it would be better to use millis() timing throughout but try the simple test for 5 seconds first to get the idea.

Of course! Thank you for kicking me in the right direction ;-)