Manderioli:
. . .
Please correct me if I am wrong:
-we are setting the current time as a variable of "offsetms"
-then we assign msperdeg as a function of current time minus last saved time / divided
-save time interval for loop
-camdeg is equal to current time minus saved time from above / msperdeg
Switch function runs each case independently after the previous one as long as the break is implemented?
Case # is the degrees associated with triggering a led?
Just before I address these points, I'll try and make a couple of general points about the code design.
- That code runs in the loop probably 1000's of times per second.
- It is designed so the main logic (represented as a switch/case construct) is in "camshaft" degrees and not milliseconds, because milliseconds are dependent on the engine speed which you may want/have to change and you don't want to have to change your code each time. I guess you've built the same sort of formula into your Excel table.
- It is in two main parts. Part (a) to detect the current period of the engine cycle (over 720 degrees) and build a conversion factor milliseconds to degrees. Part (b) is a sort of look up table degrees to LED events (on/off) appropriate to the current state of the camshaft.
So to your questions:
-we are setting the current time as a variable of "offsetms"
Exactly. The function millis() is a counter which starts as soon as the Arduino is started, counting milliseconds.
offsetMs is the value of millis() at the beginning of the engine cycle (camshaft at zero degrees) so by the calculation ( millis() - offsetMs ) we can work out how far into the engine cycle we are.
-then we assign msperdeg as a function of current time minus last saved time / divided [by 360]
Exactly. The the difference between the millisecond counter at the beginning of the current cycle (offsetMs) and the last cycle (offsetMsLastCycle) gives the length of the current cycle in milliseconds. We divide this by 360 to get the conversion factor milliseconds per degree (msPerDegree).
-save time interval for loop
That whole code section runs in the loop. The two calculations above are only done, however, when Zero degrees is detected and should be done only once per cycle. In the loop we know exactly what the current state of rotation of the camshaft is (camshaftDegrees) by the number of millisececonds we are into the current engine cycle. Once we know that,
we can switch the LEDs on/off. As I guess you know, a digitalWrite() which is used to change the state of a LED here, retains it state until the next digitalWrite().
-camdeg is equal to current time minus saved time from above / msperdeg
camshaftDegrees is the current state of rotation of the camshaft, calculated based on the time (mS) required for a complete revolution of the camshaft and the time (mS) we are into the current cycle. So for example, if a camshaft revolution takes 30 seconds, and we are 15 seconds into the current cycle, the camshaft is calculated to be 180 degrees.
Switch function runs each case independently after the previous one as long as the break is implemented?
Case # is the degrees associated with triggering a led?
Yes. A switch/case statement is like the classic if/then/else construct. So for example
case 270 : digitalWrite( VCOMP, LOW ) ; Break ;
Means if the camshaft is rotated 270 degrees, change the state of LED VCOMP to LOW. The break just exits the Switch/Case statement, and it will be reentered at the next iteration of the loop.