Try this - think you will find it keeps pretty accurate time - within 4-8 uS
(hopefully no compile bugs, can't try it here)
unsigned long oneHundredth = 10000UL; // 10,000uS = 0.01S
byte hundredths; // use to count to 50, 50/100ths = 0.5S
byte ledPin = 13; // = PORTB, bit 5
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedMicros;
void setup(){
pinMode (ledPin, OUTPUT);
}
void loop(){
currentMicros = micros(); // capture current time
elapsedMicros = currentMicros - previousMicros; // see how much time has passed
if (elapsedMicros >= oneHundredth){ // 0.01S passed, so:
previousMicros = previousMicros + oneHundredth; // set up for next time interval
hundredths = hundredths +1; // increment the 0.01S counter
if (hundredths == 50){ // 0.5S passed?
hundredths = 0; // reset the counter
PIND = PIND | 0b00100000; // toggle LED pin by writing 1 to Input register
} // 0.5S check
} // 0.01S check
} // end loop