I may be totally wrong here but I need some help.

I need to execute a function after a timeout without using delay or interrupting the program flow.

#include <SimpleTimer.h>
SimpleTimer timer;
void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:
timer.setTimeout(1000,callme);
}

void callme(){
Serial.println(“The function is called.”);
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please spend some time reviewing the example sketches and documentation for the SimpleTimer library. After that if you still can’t figure it out post a link to where you downloaded the SimpleTimer library from. Use the chain links icon on the toolbar to make the link clickable.

I need to execute a function after a timeout without using delay or interrupting the program flow.

So what is the problem with the code you posted ?
What does it do ?
What should it do ?

I am not familiar with the library that you are using but do you perhaps need to start the timer in some way having set the timeout ?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

The demo Several Things at a Time illustrates the use of millis() to manage timing without any need for a library.

...R

All I want to know is how can we call a function after some specified time. The code I am using does not seem to work.

Is your loop() function missing something?
Another call to some method of the SimpleTimer class?
Didn't the library come with examples?

All I want to know is how can we call a function after some specified time.

Relative to what?

How would YOU, if you were playing an Arduino, do what you want the Arduino to do?

What I would do is note when the event of interest (boot-up, or a switch press, or 3:00 PM next Tuesday) occurs.

Periodically, I'd see if enough time had passed to warrant calling the function. If so, I'd call it.

Noting when an event occurs means calling millis(). Periodically means "On every pass through loop...".

I need to execute a function after a timeout without using delay or interrupting the program flow.

If you roll your own code rather than using a library it is easier to see what is going on.

unsigned long period = 1000;
const byte ledPin = 13;
unsigned long currentMillis;
unsigned long startMillis;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - startMillis >= period)
  {
    changeLed();    //time's up.  Call the function
    startMillis = currentMillis;  //save value ready for next period
  }
  //other code to be executed each time through loop() goes here
}

void changeLed()
{
  digitalWrite(ledPin, !digitalRead(ledPin));
}