SimpleTimer, Timer1 and Timer libaries not working

I am working on a project that I am supplying with voltage an LED through a digital pin (26) and I want the LED to be switched off after 4 seconds.

Code 1st Example

#include <SimpleTimer.h>

SimpleTimer timer;

digitalWrite(26, HIGH);

timerId = timer.setTimeout(4000, switchOff);

void switchOff() {
  digitalWrite(26, LOW);
}

Code 2nd Example

#include <SimpleTimer.h>

SimpleTimer timer;

digitalWrite(26, HIGH);

timer.setTimeout(4000, switchOff);

void switchOff() {
  digitalWrite(26, LOW);
}

None of the two examples that I have mentioned for the SimpleTimer library work. The problem is that the LED stays just on (
digitalWrite(26, HIGH);). Can someone who has made the libraries work, provide me with some guidance?

You say in the title that the "Timer1" library and the "Timer" library don't work either. Do you have examples that you tried for them as well?

Meantime, I'm checking out the "SimpleTimer" library now.

So I guess this is for an ESP8266. You could have mentioned that. You should always mention which board and which version of the IDE you’re using, especially if it’s not just an UNO, Mega2560 etc…

Anyway, I don’t have that core installed, so when “SimpleTimer” includes , it’s not found.
I can’t test your “SimpleTimer” code, so I can’t help with that problem. I’ll leave this to others.

Your presented sketches don't make any sense to me. Perhaps OldSteve is right, that you want to target a very special board?

An ordinary Arduino sketch must have setup() and loop() functions, something like this

void setup() {
  pinMode(26, OUTPUT);
  digitalWrite(26, HIGH);
  timer.setTimeout(4000, switchOff);
}

void loop() {
}

OldSteve:
So I guess this is for an ESP8266.

he said for digital pin 26.....

DrDiettrich:
Your presented sketches don't make any sense to me. Perhaps OldSteve is right, that you want to target a very special board?

An ordinary Arduino sketch must have setup() and loop() functions, something like this

void setup() {

pinMode(26, OUTPUT);
 digitalWrite(26, HIGH);
 timer.setTimeout(4000, switchOff);
}

void loop() {
}

I assumed it was just another case of the OP showing bits and pieces of his real sketch instead of all of it. (Otherwise it couldn't compile to perform as he says.)
In the case of the "SimpleTimer" library, it includes a file that's only contained in the ESP8266 core, as far as I know.
Either way, I think that even code for an ESP8266 would have a 'setup()' and 'loop()', or at the very least, a 'main()' function. Isn't 'main()' required in C/C++?

dave-in-nj:
he said for digital pin 26…

Yes, I noticed, but even a Mega2560 has a digital pin 26.

As I said, when it’s an exotic board in particular, the OP should say so.

Edit: Oh, I see what you mean. An ESP8266 doesn’t have a pin 26. I didn’t know that. (Never used one.)

In that case, the code couldn’t compile, since “SimpleTimer” requires the include. Now I’m totally lost, since he implied that the code compiles:-

The problem is that the LED stays just on…

I got my info regarding being a part of the ESP8266 core from here:-

Unless he installed it separately, but that’s more info he should have added.

OldSteve:
Yes, I noticed, but even a Mega2560 has a digital pin 26.

As I said, when it's an exotic board in particular, the OP should say so.

AGREED!

I was going to post some comments about if these libraries are so difficult, why note just write the code ?
and he was not looking for a different library. he was specific

Can someone who has made the libraries work, provide me with some guidance?]

dave-in-nj:
I was going to post some comments about if these libraries are so difficult, why note just write the code ?

For what he wants, simply writing a quick 'millis()'-based delay would be quicker and easier than testing 3 different libraries to get one.

he was specific

Can someone who has made the libraries work, provide me with some guidance?

True, but I planned to quickly get it working for me first, until I tripped over the hurdles. :slight_smile:

I am using Arduino Mega2560 and I am programming it using Arduino’s IDE version 1.6.10. I have installed the libraries that I have tried via the .zip method.

The code is

#include <SimpleTimer.h>

SimpleTimer timer;

void setup() {
pinMode(26, OUTPUT);
digitalWrite(26, HIGH);

timerId = timer.setTimeout(4000, switchOff);
}

void switchOff() {
  digitalWrite(26, LOW);
}

void loop() {
}

Got the answer from stackexchange.

I needed to use

void loop() {
  timer.run();
}

I discovered this morning that there are two versions of “SimpleTimer” - one includes and the other doesn’t. That’s why I had problems.

A link to the library or libraries you’re using is also a good idea in future.

Vasileios:
Got the answer from stackexchange.

I needed to use

void loop() {

timer.run();
}

Can you tell us what board you are using?

ChrisTenone:
Can you tell us what board you are using?

Vasileios:
I am using Arduino Mega2560

Chris, as I mentioned there are two “SimpleTimer” libraries. Apparently, I had the one for an ESP8266. That’s why it included . (It didn’t mention the ESP8266 on the download page.)

OldSteve:
Chris, as I mentioned there are two “SimpleTimer” libraries. Apparently, I had the one for an ESP8266. That’s why it included . (It didn’t mention the ESP8266 on the download page.)

Whoops, missed that. Duh, it was in the post right above! :o