I have two sketches, one uses millis() and the other uses micros() to do the same thing - blink two LEDs at different rates (1Hz and 0.5Hz). The on-time for each is the same, the off-times are different.
*____*____*____*____*____
*_________*_________*____
My micros() version LEDs go out of sync.
It's the same as the millis() [w/ all occurrences of millis() to micros(), appended three 0's to constants.]
Is it because micros() is actually based on 2 usecs?
Or is my implementation of rollover-proofing, in fact, flawed?
[Or something else... my if's are inexpedient?]
void loop ()
{
if (((grnFlag == true) && ((micros()-grnHItimer)>=grnOntime)) || ((grnFlag == false) && ((micros()-grnLOtimer)>=grnOfftime)))
{
toggleGLED();
}
if (((redFlag == true) && ((micros()-redHItimer)>=redOntime)) || ((redFlag == false) && ((micros()-redLOtimer)>=redOfftime)))
{
toggleRLED();
}
}
millis() version
const uint8_t grnLED = 9;
uint32_t grnHItimer;
uint32_t grnLOtimer;
boolean grnFlag;
const uint32_t grnOntime = 100;
const uint32_t grnOfftime = 1900;
const uint8_t redLED = 10;
uint32_t redHItimer;
uint32_t redLOtimer;
boolean redFlag;
const uint32_t redOntime = 100;
const uint32_t redOfftime = 900;
void setup ()
{
pinMode(grnLED,OUTPUT);
grnHItimer = millis();
grnLOtimer = millis();
grnFlag = true; // grnFlag AND redFlag must init TRUE
// to start On together
pinMode(redLED,OUTPUT);
redHItimer = millis();
redLOtimer = millis();
redFlag = true; // grnFlag AND redFlag must init TRUE
// to start On together
digitalWrite(redLED,HIGH); // init HIGH or long off_pd at start
digitalWrite(grnLED,HIGH); // init HIGH or long off_pd at start
}
void loop ()
{
if (((grnFlag == true) && ((millis()-grnHItimer)>=grnOntime)) || ((grnFlag == false) && ((millis()-grnLOtimer)>=grnOfftime)))
{
toggleGLED();
}
if (((redFlag == true) && ((millis()-redHItimer)>=redOntime)) || ((redFlag == false) && ((millis()-redLOtimer)>=redOfftime)))
{
toggleRLED();
}
}
void toggleGLED()
{
if (grnFlag == false)
{
digitalWrite(grnLED, HIGH);
grnFlag = true;
}
else
{
digitalWrite(grnLED, LOW);
grnFlag = false;
}
grnHItimer = millis();
grnLOtimer = millis();
}
micros() version
const uint8_t grnLED = 9;
uint32_t grnHItimer;
uint32_t grnLOtimer;
boolean grnFlag;
const uint32_t grnOntime = 100000;
const uint32_t grnOfftime = 1900000;
const uint8_t redLED = 10;
uint32_t redHItimer;
uint32_t redLOtimer;
boolean redFlag;
const uint32_t redOntime = 100000;
const uint32_t redOfftime = 900000;
void setup ()
{
pinMode(grnLED,OUTPUT);
grnHItimer = micros();
grnLOtimer = micros();
grnFlag = true; // grnFlag AND redFlag must init TRUE
// to start On together
pinMode(redLED,OUTPUT);
redHItimer = micros();
redLOtimer = micros();
redFlag = true; // grnFlag AND redFlag must init TRUE
// to start On together
digitalWrite(redLED,HIGH); // init HIGH or long off_pd at start
digitalWrite(grnLED,HIGH); // init HIGH or long off_pd at start
}
void loop ()
{
if (((grnFlag == true) && ((micros()-grnHItimer)>=grnOntime)) || ((grnFlag == false) && ((micros()-grnLOtimer)>=grnOfftime)))
{
toggleGLED();
}
if (((redFlag == true) && ((micros()-redHItimer)>=redOntime)) || ((redFlag == false) && ((micros()-redLOtimer)>=redOfftime)))
{
toggleRLED();
}
}
void toggleGLED()
{
if (grnFlag == false)
{
digitalWrite(grnLED, HIGH);
grnFlag = true;
}
else
{
digitalWrite(grnLED, LOW);
grnFlag = false;
}
grnHItimer = micros();
grnLOtimer = micros();
}
void toggleRLED()
{
if (redFlag == false)
{
digitalWrite(redLED, HIGH);
redFlag = true;
}
else
{
digitalWrite(redLED, LOW);
redFlag = false;
}
redHItimer = micros();
redLOtimer = micros();
}
using_millis_asymtime_3.ino (1.67 KB)
using_micros_asymtime_1.ino (1.74 KB)