For example, we engage the throttle with the arduino and the controller thinks that is the PAS at full speed but then when we let go of the throttle it switches straight to the PAS and takes the readings from it which will be synced and just re calibrates itself so it won't give error 22 or 21.
that's what the code does since the first version. The logic works like this:
The voltage at D13 has a scale of 8-bit, 256 for 5V. The threshold is 75 of 356.
if the throttle voltage threshold is 75*5/256 = 1.47V.
If the throttle voltage drops below 1.47V, we switch to pedalling code. The routine used to be called pedalon() but since version 6, Raj put it in the main loop() under 'I am pedalling'.
when in the throttle, the pulse lasts 25ms on then 25ms off. That means 50ms per cycle, 20 cycles in one second, 20*60/18 = 66 RPM . The reason to choose 20 cycles per second is because the technical specifications say that the pedal sensor produces 18 pulses per revolution.
The code reads the throttle and replicates it exactly like it is, on A5 pin.
The input has a scale of 10-bit, 1024 steps, 0-1023. To translate it to 8-bit output, 0-255, we simply divide the analogRead(A3) by 4. There is a little rounding error (<0.01V) - not to worry about.
what happens when I am pedalling?
to produce a smooth SYN (D13 pin) and T (A5 pin), the code samples SYN input (cadence input A2) and T (torque input A3) every millisecond and accumulates. It increments the SYN on time if SYN is high and increments SYN off time if SYN is low.
The SYN cycle starts with SYN low, then SYN goes high then SYN goes low.
When SYN goes from high to low, at the end of the cycle, the code saves SYN on time, SYN off time and average of T.
At the same time, the code plays back the last saved results of the last pulse in the same way it did with the throttle using the saved pulseon, pulseoff and torqueval.
There is also the issue of the pulse width modulation (PWM) frequency of the digital to analog conversion (DAC) that Raj and vfr mentioned. DAC is done by by PWM with a settable carrier whose frequency can be selected in code, the default is 480Hz or 2ms - it is good enough for our purpose.
The higher the carrier frequency, the smoother the signal is. However, I have magnified the oscilloscope traces of pas (or SYN) and torque (T) on the picture that Evian posted, it's clear that the T voltage is sampled every time SYN changes phase.
That is effectively twice the frequency of the pedal sensor. The code in version 9 samples exactly in the same way, every pulseon and pulseoff transition.