I had time to do some tests…
I used this code, with PWM read on pin 2 to use arduino interrupts:
#define pinPWM 2
volatile unsigned long _mic, time_passed, time_now, period_time_passed, period_time_now;
void setup() {
Serial.begin(115200);
pinMode(pinPWM, INPUT);
attachInterrupt(digitalPinToInterrupt(pinPWM), pwm_callback, CHANGE);
delay(1000);
}
// min 8
// max 930
void loop() {
if (period_time_passed > 950) period_time_passed = 950;
//Serial.println(period_time_passed);
//Serial.print(",");
Serial.println(map(time_passed, 8, 930, 0, 359));
delay(1);
}
void pwm_callback() {
if (bitRead(PIND, pinPWM) == 1) {
time_now = micros();
} else {
_mic = micros();
period_time_passed = _mic - period_time_now;
period_time_now = _mic;
time_passed = _mic - time_now;
}
}
It kind of works, but even if I translate 8-930 range to 0-359 angle range, I see the value continuosly jumping ± 2° or 3°. which is too much for any kind of control…
Maybe we can try the timer way, to gain in precision, but honestly I feel it is not worth trying…