Go Down

Topic: Help with my fallback routine / Timer issues (Read 17 times) previous topic - next topic

lurchy

Greetings!

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?

Code: [Select]
//
//


#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);
  count=0;
  trig=0;
  digitalWrite(flash1, LOW);
  digitalWrite(flash2, LOW);
     
    Timer1.attachInterrupt(fb);
    Timer1.initialize(fallbacktime*1000000);
 
}


// 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
  }         
}




UKHeliBob

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
Code: [Select]
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy