Hi all,
I have to build an RC boat for a school project, and the radio controller I have outputs a 54hz PWM signal. The width of the pulse varies between 1100 and 2000 microseconds, which was verified with an oscilloscope. I have 2 inputs I need to monitor, collect the value of the pulse width, and then output that to the motors... problem is that not matter what I do I cannot get accurate values for pulse width.
Here's my code.
#include <TimerOne.h>
void GetInputs();
volatile unsigned long initial;
volatile unsigned long Drivein;
void setup() {
// put your setup code here, to run once:
pinMode(2,INPUT);
Timer1.initialize(500000);
attachInterrupt(digitalPinToInterrupt(2),GetInputs,RISING);
Serial.begin(57600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(Drivein);
}
void GetInputs() {
int state;
initial = Timer1.read();
state = digitalRead(2);
while (state == HIGH){
state = digitalRead(2);
}
Drivein = Timer1.read() - initial;
}
so what this is supposed to do is go into the interrupt when the pulse goes from low to high. It should collect a value from the internal timer register, and fall into a while loop for the duration of the pulse. Then, once it falls out of the while loop, it will find the elapsed time, and print that to the serial monitor.
Problem is that it never falls into the while loop. I eventually threw a counter in the while loop, and it was always zero. Does anyone have any idea why the pulse wouldn't still be high once it's in the interrupt?
Also, does anyone have any other ideas as to how to measure time? I tried using micros(), and I found this link here... nothing works satisfactorily.
http://www.benripley.com/diy/arduino/three-ways-to-read-a-pwm-signal-with-arduino/
any help would be appreciated,
thanks