The problem I'm having is that when delayMicroseconds is used I get all sort of finicky RPM on the output of this thing. When I use delay (and divide by 1000) it works but it doesn't smoothly transition
because i need the resolution of microseconds, not milliseconds. For example, 9000 RPM has a delay between spark of 0.01333 seconds, or 13.3 ms [1/(9000/60)*2]. delay doesn't accept decimals so only 13 would be recognized.
Is there something I'm overlooking?
int engspeedV=analogRead(A5);
engspeedV = map(engspeedV, 0, 1023, 1000, 9000); //sim engine speed with a variable voltage input
double period=spark(engspeedV); //converts the engine speed to a time slice per second
analogWrite(sparkPin,1023); //Coilpack activate and discharges
delay(5); //discharge period of 5ms
analogWrite(sparkPin,0); //end discharge period
double finalDelay=(period-5)*1000); //the period of wait before the other discharge cycle fires again
delayMicroseconds(finalDelay); //try to apply the delay here
Delaymicroseconds() does not take a double as an argument but an unsigned long
Yeah, but it should convert automatically for you...
You may be running into the fact that floating point calculations (and also analogRead) take quite a long time compared to microseconds - 150+ cycles for simple operations, ~500 cycles for divide, up to 3000 cycles for some of the math functions... https://www.nongnu.org/avr-libc/user-manual/benchmarks.html