it takes time to cycle through loop (), this is where some time discrepancy is coming from, a while ago i did measure it on a due, it was microseconds if i remember, on a 328 you can times that by at least 4. you could try putting it in a while loop and see the difference.
more is coming from the 2 ifs, try an if else, so only one condition gets checked each time
but you seem to be familiar with register access, set up a pwm output, or a timer counter interrupt. then it will be accurate to clock cycles not microseconds
i dont know much about communication. but i imagine the timing would have to be fairly accurate. you wont get that with a polling approach. you havent even added any other code, once you do your timing goes out the window
I tryed this it is better but still not accurate//
unsigned long currentMicros, Micros;
int HILO = 0;
//////////////////////// SEND BIT 0
int DELAYHI0 = 262;
int DELAYLO0 = 418;
//////////////////////// SEND BIT 1
int DELAYHI1 = 471;
int DELAYLO1 = 209;
////////////////////////
void setup()
{
DDRD |= 1 << 5; // SET D5 AS OUTPUT
currentMicros = micros();
Micros = currentMicros;
}
void loop()
{
PULSE();
}
void PULSE()
{
while(1)
{
currentMicros = micros();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// SEND BIT 0
if(currentMicros - Micros > DELAYLO0 && HILO == 0) { PORTD |= 0B00100000; HILO = 1; Micros = currentMicros; } // SET D5 HIGH FOR 262us
currentMicros = micros();
if(currentMicros - Micros > DELAYHI0 && HILO == 1) { PORTD &= 0B00000000; HILO = 2; Micros = currentMicros; } // SET D5 LOW FOR 418us
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// SEND BIT 1
currentMicros = micros();
if(currentMicros - Micros > DELAYLO1 && HILO == 2) { PORTD |= 0B00100000; HILO = 3; Micros = currentMicros; } // SET D5 HIGH FOR 262us
currentMicros = micros();
if(currentMicros - Micros > DELAYHI1 && HILO == 3) { PORTD &= 0B00000000; HILO = 0; Micros = currentMicros; } // SET D5 LOW FOR 418us
}
}
joeblogs:
i dont know much about communication. but i imagine the timing would have to be fairly accurate. you wont get that with a polling approach. you havent even added any other code, once you do your timing goes out the window