I am currently working on a little code which needs to react fast. Here is what I am trying to accomplish:
Using a sine wave phase detector I am detecting every zero crossing and feeding that pulse into the Arduino.
Using an ISR the Arduino detects this zero crossing pulse using RISING. Once one pulse has been detected AND I have pressed a button, I need there to be a delay of 0 - 10ms before switching on an output.
If I don't use an ISR and set a delay of up to 10ms inside the loop, it won't really be 10ms and it will be some other delay, nothing consistent. (I checked with an oscilloscope).
Only when using an ISR and using delayMicrosenconds() inside the ISR I can delay the output consistently for up to 10 ms everytime.
Now I have read that doing this is wrong...Even though it works perfectly. Which better way could I use?
Thanks!
EDIT: While 100% accuracy may not be possible, it needs to be as precise as possible. More important is the repeatability. Using my method I could reproduce the outcome everytime with an accuracy in the nano seconds range.
millis()
andmicros()
) getting off, and if you don't use libraries that rely on interrupts, then your method should be OK.