I expected the following 2 code snippets to have similar behaviour.
I want to use the 2nd snippet so that I can have greater control over the timing resolution.
The loops are writing to an I2C IO Expander. I want to be able to turn one pin on for 8.xx ms and another for 9.xx ms. Currently I can only getting timing resolutions of 1ms (1000 microseconds) for an unknown reason.
//With delayMicroseconds(1000):
//Note: Only have timing resolution of 1ms. This works.
void loop()
{
int offTime = 350;
float maxOnTime = 35;
int toWrite = 0;
float currentTime = 0;
while(currentTime < maxOnTime)
{
toWrite = 0xff;
if (currentTime > 8)
{
toWrite -= LLED;
}
if (currentTime > 9)
{
toWrite -= RLED;
}
writeI2C(I2C_ADDRESS, toWrite);
delayMicroseconds(1000);
currentTime += 1;
}
writeI2C(I2C_ADDRESS, 0x00);
delay(offTime);
}
//With delayMicroseconds(10):
//Note: Should have timing resolution of 0.01ms. This doesn’t work. The timing is very off for some reason. Is there some problem with currentTime += 0.01?
void loop()
{
int offTime = 350;
float maxOnTime = 35;
int toWrite = 0;
float currentTime = 0;
while(currentTime < maxOnTime)
{
toWrite = 0xff;
if (currentTime > 8.15)
{
toWrite -= LLED;
}
if (currentTime > 9.25)
{
toWrite -= RLED;
}
writeI2C(I2C_ADDRESS, toWrite);
delayMicroseconds(10);
currentTime += 0.01;
}
writeI2C(I2C_ADDRESS, 0x00);
delay(offTime);
}
Any suggestions?