0

I have tried to find simple library for triggering timer after some time. The idea is to retry network connection (RF24 network library).

I have following code now

while(!mesh.write(message, headerType, memSize)) {

}

This code will try to send packet until success

But I need to give up after for instance 10 seconds

I wanna to have something like this

bool timerTriggered = false;
Timer myTimer = new Timer(10000);
myTimer.setCallback(countedDown);

void countedDown() {
   timerTriggered = true;
}

And start timer before network request

myTimer.start();

while(!mesh.write(message, headerType, memSize) || !timerTriggered) {

}

Please suggest the best way to implement this.

Thanks.

EDIT

I have found problem as far as delay(); uses timer internally it conflicts with libraries or simple timer.

The solution is to use Alarm.delay(400); instead.

Maybe someone knows better solution ?

2 Answers 2

1

Something like this would work as a non-blocking, general purpose timer:

unsigned long startTimestamp;
unsigned long timestamp;
const unsigned long timer_length = 10000; //10 seconds

startTimestamp = millis();
timestamp = startTimestamp;

while ( (timestamp - startTimestamp) < timer_length)
{
    // keep doing thing

    timestamp = millis();
}

// stop doing thing now
1

To expand on the other answer and combine it better with your situation:

Just check the current time in your while loop:

unsigned long startTime = millis();
while((!mesh.write(message, headerType, memSize)) && ()millis() - startTime) < 10000)) {
}

That is, while it's not transmitted properly, and the difference between now and when you started transmitting is less that 10 seconds, keep trying.

There is no need for any special libraries or asynchronous timers - it can all be done by examining millis().

Your Answer

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

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