Is there a substitute for delay()


#1

I am working on programming the IR remote, and I use delays all the time. For example, to make it go for half a second, I would do something like this:`

motor1.run(255);
motor2.run(255);
delay(500);`

But the delay() command uses 100% of the processor, so it is oblivious to IR commands during the time the delay is activated. If I did a command and then decided to cancel it, I could not do that because it is unresponsive. Is there any way I can make it wait without using the delay() command so it will still respond during the command?


#2

Hi Niall,

Yes there is. Check out This tutorial about how to use the millis() function instead.

Have fun, Stefan


#3

Agreed. The delay() method is a blocking call. You’ll need to use the millis() call and a static variable to track whether or not to start/stop executing your code.


#4

I tried that, and it only works for values greater that 500 milliseconds. Here is my code, is there a problem?

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > 500) {
previousMillis = currentMillis;
motor1.run(255);
motor2.run(-255);
}


#5