0

I have an Arduino sketch where I'm sending commands over serial, and as a result turning pins from low to high. What I'm looking to do is have those pins turn high for 5 seconds and then turn back to low. I was looking for a time or scheduling library for Arduino that allows multiple timers at once and lets you pass a parameter (at least I think that's what I need).

In other words, I would like to implement a "doAfter" method and pass the function it is calling a pin number.

2 Answers 2

1

Have you tried the delay() function? Try putting this in the loop() function for pin 3.

digitalWrite(3, HIGH);
delay(1000); // time in milliseconds as parameter
digitalWrite(3, LOW);
0

You can use the Timer library to achieve this:

#include "Timer.h"
[...]
digitalWrite(3, HIGH);
int afterEvent = t.after(5000, doAfter);
[...]
void doAfter() {
  digitalWrite(3, LOW);
}

As you can see, you cannot pass a parameter to the timer, just a callback. I suggest that you implement either a bunch of doAfter functions, from doAfter3 to doAfter9` for instance, and call the right one in the callback.

There are probably other timer libraries, but this one is simple and complete enough for your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.