2LED's with diffrent delay

Hi,

I have a question about a program with 2 led’s with a different delay.

The program should work as follow;
If button 1 is pushed led 1 should burn for 5000ms
If button 2 is pushed led 2 should burn for 2000ms

If I program this and the delay for button 1 is active, the program does not look at button 2. How can I make a program where the buttons are monitored also if there is a delay active.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

In the state machine i make a function that i call for a certain time, right?

But the program can still be stuck in the If(millis() >= goTime) function();
Or am I wrong here?

dennisfranky:
In the state machine i make a function that i call for a certain time, right?

But the program can still be stuck in the If(millis() >= goTime) function();
Or am I wrong here?

Depends on how the code is written.

Some time ago i encounter similar problem, and to resolve it i find an example, based on which i created this class. (Unfortunately i can't quote the original author, since i can't find from where i see it.).

But based on this anonymous author, i extend the class a little bit.
You will find the class in the attached zip and here is the example for your case.

You can take a look the code in the class, hopefully it will make sense.
Feel free to use this class, change and update it.

// To use external classes that are not in the arduino library, simply put them in the same folder as your project - ".ino" file
// and include them - wrapped with quotes
#include "IOControl.h"

// The digital pins on which are your LED's
IOControl led_1(5);
IOControl led_2(6);

// Define variables for buttons state
int btn_1 = 0;
int btn_2 = 0;

void setup() {
  // Define the button pins, on which IO are
  pinMode( 3, INPUT );
  pinMode( 4, INPUT );
}

void loop() {
  // Read the state of the buttons
  btn_1 = digitalRead( 3 );
  btn_2 = digitalRead( 4 );
  
  if ( btn_1 == HIGH ) {
    led_1.setOnForTime( 5000 /* in milliseconds */ );
  }
  
  if ( btn_2 == HIGH ) {
    led_2.setOnForTime( 2000 /* in milliseconds */ );
  }
  
  // This will check the status, to turn them off when the time passes
  led_1.check();
  led_2.check();
}

Archive.zip (947 Bytes)

I also found this;

Simply start a second loop. I only don’t know what the impact is on the performance of the Arduino. And is properly going to be a mess when there are more led’s.

dennisfranky:
But the program can still be stuck in the If(millis() >= goTime) function();
Or am I wrong here?

It seems like you have not looked at the demo several things at a time that @AWOL linked to. If you did you would not be trying to use millis() in that way. I suspect @AWOL's own example is similar to mine.

The other reason I suspect you did not read the link is because it actually does exactly what you want - except with 3 LEDs.

...R

dennisfranky:
In the state machine i make a function that i call for a certain time, right?

Wrong not FOR a certain time but AT a certain time.

dennisfranky:
But the program can still be stuck in the If(millis() >= goTime) function();

Well the function() is the only function, and if you are sloppy writing that, then yes you can get stuck there, but as you must never use delays then there is little chance of getting stuck.

dennisfranky:
I also found this;
Scheduler - Arduino Reference

That way madness lies.