Hi guys, I've got this simple program that reads a PPM signal from a single channel on a RC receiver and "should" return a value from 0 to 511. The logic seems to work and I am getting a value proportional to the RC transmitter inputs. But the constrain and or map functions are giving me odd results.
Basically it looks for a high pulse and once it finds one it returns the duration in microseconds. The extremes (for int RXSG) are about 910 and 2085. So I'm constraining it to between 1000 and 2000, and then mapping that from 0 to 511.
But I'm getting a range form -47 to like 560. Is it because RXSG is a volatile int? BTW does it even need to be a volatile variable?
Any help is appreciated. Thanks -Tony
BTW, I didn't write this code, I just modified it a fair bit for my purposes.
//Reads PPM signals from one channel out of a RX reciever
int RXCH = 4; //pin 4
volatile int RXSG = 0;
int RXOK = 0;
int PWMSG = 0;
void setup() {
//Start communication to serial port
Serial.begin(115200);
pinMode(RXCH, INPUT); // pin 4 is an input
}
void loop() {
RXSG = pulseIn(RXCH, HIGH, 50000); //read the receiver signal
if (RXSG == 0) {RXSG = RXOK;} else {RXOK = RXSG;} //if the signal is good then use it, else use the previous signal
constrain (RXSG, 1000, 2000); //make sure that the value stays within the disired boundries
PWMSG = map(RXSG, 1000, 2000, 0, 511); //substitute the high values to a value between 0 and 511
// Print RX values
Serial.print(" || Pin: ");
Serial.print(RXCH);
Serial.print(" / PWMSG: ");
Serial.print(PWMSG); // print the value
Serial.println();
delay(100);
}