Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Syntax >> delay vs millis
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1288601584

Message started by mga on 11/01/10 at 08:53:04

Title: delay vs millis
Post by mga on 11/01/10 at 08:53:04
Hi

Can any:

[code c++]int n = 100;
void loop() {
doSomething();
delay(n);
}
[/code]
be modeled using:

[code c++]int n = 100;
unsigned long mycounter = 0;
void loop() {
if (millis()-mycounter>n) {
mycounter = millis();
doSomething();
}
}[/code]

and expect the same results?

Title: Re: delay vs millis
Post by barragan on 11/01/10 at 17:53:49
not really, the second program doesn't block the cpu. The delay() command blocks the cpu for the number or milliseconds specified, on the other hand the second program just checks if "n" milliseconds have passed, if true it executes the commands inside the if, it not it continues executing other statements within the program.

[code c++]
int n = 100;
unsigned long mycounter = 0;

void loop() {
  if (millis()-mycounter>n) {
    mycounter = millis();
    doSomething();
  }
  // other things continue executing beyond this point
 
}
[/code]


Title: Re: delay vs millis
Post by mga on 11/01/10 at 20:10:55
OK. So this is what I want (not blocking the CPU). I assume it is advisable NOT to use delay() unless there is a very special reason to do so.

Title: Re: delay vs millis
Post by barragan on 11/01/10 at 22:24:50
that's right delay() will stop the cpu.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.