"""simple""" millis() problem

Hello,

I’m dealing with a problem that forbids me to go further in my project.

Here I used the blink LED example to make my question easaly understandable :

The LED is blinking as long as a button isn’t pressed.

When the button is pressed, the LED stops blinking.

BUT If the blinking has been running for more (or equal) than 5 seconds, and even if the button isn’t pressed, I need the blinking to stop.

( the true purpose for this : open a door until a magnetic switch says it is opened. If the door is opening ( = motor running ) for more than 5 secs, stop the motor even if the switch hasn’t changed, to prevent any damage)

Thank’s for your help ! :slight_smile:

int led = 13;
int button = 2;

void setup() {                
 
  pinMode(led, OUTPUT); 
  pinMode(button,INPUT); 
  millis(); 
}


void loop() {
do{

ouverture();  

}while(millis()< 5000);

}

void ouverture(){

  if(digitalRead(button)==LOW){

  digitalWrite(led, HIGH);  
  delay(1000);              
  digitalWrite(led, LOW); 
  delay(1000);   
  } 

  if(digitalRead(button) == HIGH){

    digitalWrite(led, LOW);
  }
}

Useful.

  millis();

If you don't give a shit what it has to say, why bother calling the function?

Using delay() and millis() in the same sketch is silly.

PaulS: If you don't give a shit what it has to say, why bother calling the function?

Haha, how friendly. Love it.

Would you mind telling me how to set this up the proper way?

This is my first programming experience so " silly '' to you isn't that " silly " to me.

Thanks for your kindness, again.

What are you expecting to use millis() for? Typically it is used to detect when an event happens and to get the current time.

It does little good to collect the time that an event occurs, of you don't record it. So, you need a place to record when the event of interest occurs, and you need to record the time when the event of interest occurs.

So, what is the event of interest? That's where I get lost. If you wanted to start the LED blinking when the switch becomes pressed, and stop it when the switch becomes released OR when 5 seconds had elapsed, even if the switch is still pressed, then the event of interest is the switch becoming pressed.

Your contrived example, of a blinking LED, has nothing to do with the real purpose you gloss over. In the real case, the event of interest is the start of the door opening (presumably in response to the "open the door, Hal" switch becoming pressed.

Next, you need to periodically check that an event of interest is in progress, and determine it that has been happening for too long.

For periodically, substitute "on every pass through loop()".

You know that an event of interest is, or is not, in progress, because the start time is, or is not, 0.

You can determine how long the event has been underway by subtracting then (when the event started) from now (as determined by millis()). It is easy to compare the difference to a threshold, and to do the right think, if needed.

So, is there some part of this that you need help with?

Maybe check out https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay to see an example of how to use millis()?

evanmars: Maybe check out https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay to see an example of how to use millis()?

It's better to use the version that's shipped with the IDE. The online tutorial has a rollover issue.

Have a look at planning and implementing a program and at several things at a time. They both use millis() to manage timing

...R