Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing a function after x time

Tags:

arduino

What can I use to perform a function in the loop if none of the other conditions in the loop and their code have been executed after a certain amount of time? Can it be done with a delay, or is there some other function?

like image 743
Markaj Avatar asked May 27 '12 10:05

Markaj


People also ask

How do you call a function after some time in react?

The setTimeout method allows us to run a function once after the interval of the time. Here we have defined a function to log something in the browser console after 2 seconds. const timerId = setTimeout(() => { console. log('Will be called after 2 seconds'); }, 2000);


1 Answers

I don't think it's possible to implement since the Arduino does not have an internal clock.

EDIT : It is possible to use the millis() function to calculate an amount of time since the start of the board:

unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

void setup(){
}

void loop(){
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;  

     // do something
  }
}
like image 68
ndeverge Avatar answered Sep 18 '22 21:09

ndeverge