Shift Indicator using Arduino Nano

Attached is the zip file for the engine timing sensor simulator. The Arduino timer 1 is set to run in the CTC mode. The OCR1A register is set for the maximum count to be reached before the counter resets to zero, then starts counting up again. At the time of reset, an output pin is toggled, that means it changes state. Two state changes are a complete cycle, four cycles is one RPM 0-1-0-1-0-1-0-1. The period is controlled by adjusting the OCR1A, a larger value entered there means more timer ticks to reach that value and slower simulated RPM. Lesser counts go the other way and result in higher RPM.

Constant RPM is achieved with a constant value of OCR1A, but by changing it the RPM can be varied. I wrote a few lines of code to ramp the RPM from about 650 to 7,000 RPM.

Note: comments are preceded by //

while (1) // main loop of program
{
delay_ms(11500/period); // generates a variable delay between changes
if (period-- >= 1050) // period-- subtracts 1 from the period, and checks if limit is reached
{ // the timer is a 16 bit register, the micro is 8 bit, so write register with two operations
OCR1AH = WORD_HI_BYTE(period); // set next event, with atomic operation
OCR1AL = WORD_LO_BYTE(period);
}
else period = 11500; // this resets to lowest RPM
}

I was non mathematical, and arrived at 11500 for low RPM, and 1050, by measuring periods with scope, they are approximate. There are many settings for crystal divisors and time clock prescalars. Changing those changes period. The important part, timer1 is a 16 bit timer, it can count from 0 to 65,535, so the low RPM number, cannot be set above that. The simulator is running slow, it takes several seconds for a complete RPM ramp. That can all be changed by reducing the timer resolution or reducing time between changes. That is an exercise for learning.

There are initialization settings for the timer1,not shown, but done with wizard. In this case it is set to CTC mode, with 500kHz clock rate, Output toggle on compare match of OCR1A.