I am trying to detect a change in the PVM signal using arduino uno. Basically a change in the controller, changes the PWM signal's duty cycle output from the receiver. The whole thing is an RC Car. I would like to detect the signals so that i can actuate the servo when it is steered right or left accordingly.
But i'm not able to detect the change. Firstly i used digitalRead function and tried to calculate offtime and ontime delay but didn't observe any change even when the car is steered through the controller.
Then i tried using analogRead function to calculate offtime and ontime delays. Again couldn't detect the change in signal.
I tried to convert PWM signal to analog using low pass filter and input the analog signal to arduino. I couldn't detect a small change even here.
I just print the state of the signal in serial manager and operate my controller and see if i could observe any change in the values. But its all the same.
Is my method incorrect? What might be the possible solution for this? Any other method to be tried?
I think the only thing you need to do is time the period that the pulse is actually high. To achieve this I'd setup an interrupt to trigger on the rising edge. Within this interrupt store the current value of micros() in a global variable.
Meanwhile in your loop function, you also want to poll the same pin, looking for the level to go LOW. When it does, simply deduct that global variable from the new value of micros() and store this difference in another global variable (perhaps called pulseWidth).
Also within your loop function, every second Serial.print the current value of pulseWidth. With a bit of experimentation, you'll get the drift of how the value relates to position.
Then you can modify your code to map, pulseWidth to the value you need to send to your servo.
Try the attached code and feed the PWM signal into (UNO) pin 2 (all assuming it's 5V DC). Then open a serial monitor @ 115200 baud and see what values you get when steering left & right. It only updates every second so hold the steering over for at least a couple of seconds in each direction to see what you get then post the results.