Can anyone see anything wrong with this simple code. Arduino uno, expect the LED on pin 13 to be on or of dependant on the position of the control stick on the rc transmitter. regardless of the stick position the LED remains on.
int rcpin =7;
int motpin = 13;
void setup() {
Serial.begin(9600);
pinMode(rcpin, INPUT);
pinMode(motpin, OUTPUT);
}
int val;
void loop()
{
val = pulseIn(rcpin, INPUT);
Serial.println(val);
delay(1000);
if ((val > 14535) && (val < 14335)) {
digitalWrite(motpin, LOW);
}
else {
digitalWrite(motpin, HIGH);
}
}
the value is changing when the rc transmitter switch is operated. mid value is 14435 with the full deflections reading 14072 + or - a couple of points and 14926 + or - respectively
Also, the best way to debug these things is to sit down and "run" the code yourself - even if you have to sit with a C reference book in front of you to sort out just what each operator does and precedence issues.
That is how we can look at your code and find problems without actually running it on the Arduino.
KeithRB:
You have an && there when you want an || (or).
And says if either is false, the result is false.
Or says if either is true, the result is true.
mustang493:
in mid position it is neither therefore the else statement is true??
if ( 14335 < val && val < 14535) // means "if val is between 14335 and 14535
if ( val > 14535) || (val < 14335) ) // means "if val is NOT between 14335 and 14535
if ( 14535 < val || val < 14335) // ditto: "if val is NOT between 14335 and 14535