So here's the basic idea: http://s1221.photobucket.com/albums/dd470/Hari_Ganti/?action=view¤t=EngineFlow.jpg
void setup() {
Timer3.setPeriod(36000000);
Timer3.setChannel2Mode(TIMER_OUTPUTCOMPARE);
Timer3.attachCompare2Interrupt(spark);
attachInterrupt(0, pulse, FALLING);
pinMode(11, PWM);
}
void loop() {
pwmWrite(11, pulseDelay); //pulseDelay has to be modified...
//Why can't pwmWrite support microseconds...
Timer3.setCompare2(sparkDelay); //Again, needs modifications...
}
void pulse() {
Timer3.setCount(0);
}
void spark() {
digitalWrite(12, HIGH);
delayMicroseconds(3); //Not sure if this is necessary
digitalWrite(12, LOW);
}
I'm pretty sure 36,000,000 microseconds is a valid period (considering the max is 1 minute). If not, let me know. My idea is that there are an integer number of cycles/count. This way, the PWM counter increments at the same rate regardless of when the counter resets. This makes later calculations (like the PWM value for pulseDelay and the counter value for spark) much easier because the count is constant.
It's like this: |---|--|----|----|---|---|--|-|-| and so on. The dash represents one count and a line represents a count reset. It'd be hard to draw what this is compared to, so I'll describe it. The other system has the period vary so that there is the same number of dashes between the lines, but not all dashes are equal valued. The second way makes calculations later much harder, and is much more math intensive (as if this project isn't!) That being said, my method, the first one, might not work. What do you all think? Meanwhile, I am going to recalculate the pulseDelay and spark compare-to values.
EDIT: A good way to compare the two systems is to imaging a an exchange of money between two people. If one owes another a dollar, that "space" could be filled with 100 pennies (many of equal values), or 1 dollar bill (same of unequal values). The next time the first guy owes the other money, assuming the same two systems, if he owes 50 cents then the break-down would be either 50 pennies (equal value to the pennies before, but a different quantity) or 1 half-dollar (inequal value to the dollar bill, but the same quantity of material exchanged- one unit). Hope that helps future readers.