Stormix
Stormix

Reputation: 113

Repeat blink sketch for a specified time

I need help with an Arduino sketch , I want to repeat the blink sketch for a specified amount of time (3minutes for example) , then Stop .
As we know , the loop() keeps runing forever which is not what I want . Any ideas how I can achieve this , blinking an LED for X minutes and Stopping ?

Upvotes: 0

Views: 356

Answers (1)

flott
flott

Reputation: 231

You should probably make use of some timer library. A simple (maybe naive) way to achieve what you want to do is to make use of a boolean that is set to 0 when 3 minutes has passed or simply digitalWrite the led to low when the timer has passed.

Check this link: http://playground.arduino.cc/Code/Timer

I suggest that you use int after(long duration, callback).

Below is a (very) simple example of how you probably could do:

#include "Timer.h"
Timer t;
LED = 1;

void setup() {
   int afterTime = t.after(180000, cancelLED);
}

void loop() {
   t.update();
   if(LED) {
      //The "write HIGH" statement in your sketch here.
   }
   else {
      //Write the led to LOW
   }
}

void cancelLED() {
   LED = 0;
}

I haven't used the library myself, I just checked the the docs and wrote an example to give you some ideas. Don't expect it to work right away.

Upvotes: 1

Related Questions