Offline
Newbie
Karma: 0
Posts: 44
|
 |
« on: January 23, 2013, 05:50:34 pm » |
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
|
|
|
|
« Last Edit: January 23, 2013, 06:16:03 pm by mustang493 »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #1 on: January 23, 2013, 06:09:13 pm » |
the three stick positions are giving me three values 14072, 14435 and 14925 (plus or minus 1-6)
|
|
|
|
« Last Edit: January 23, 2013, 06:17:55 pm by mustang493 »
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 386
|
 |
« Reply #2 on: January 23, 2013, 06:26:57 pm » |
You have( (val >14335) && (val <14335)) since it cannot be both at once, it will always be false.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #3 on: January 23, 2013, 06:30:46 pm » |
You have( (val >14335) && (val <14335)) since it cannot be both at once, it will always be false.
Typo on the numbers, but nice catch!! That's why I usually format these things something like: if ( 14335 < val && val << 14535) {...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #4 on: January 23, 2013, 06:31:04 pm » |
in mid position it is neither therefore the else statement is true??
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #5 on: January 23, 2013, 06:32:18 pm » |
i'll tryif ( 14335 < val && val << 14535) {...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 386
|
 |
« Reply #6 on: January 23, 2013, 06:33:00 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 386
|
 |
« Reply #7 on: January 23, 2013, 06:34:03 pm » |
johncc has a bug. He did not mean << he meant '<'. << is the shift operator.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #8 on: January 23, 2013, 06:35:46 pm » |
johncc has a bug. He did not mean << he meant '<'. << is the shift operator.
True that!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 386
|
 |
« Reply #9 on: January 23, 2013, 06:40:05 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #10 on: January 23, 2013, 06:40:32 pm » |
Tried this no joy 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); Serial.println(val); // if ((val > 14535) && (val < 14335)) if ( 14335 < val || val < 14535) { digitalWrite(motpin, LOW); } else { digitalWrite(motpin, HIGH); } }
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1827
|
 |
« Reply #11 on: January 23, 2013, 06:44:07 pm » |
if ( 14335 < val || val < 14535) {
Can be simplified to if ( val != 14335) { So you want the LED to be off at that value, and on otherwise? Seems a bit loft to expect it to be EXACTLY that value.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #12 on: January 23, 2013, 06:45:33 pm » |
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.
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #13 on: January 23, 2013, 06:46:42 pm » |
yes but the mid value is not stable and can range a few points around 14435
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1827
|
 |
« Reply #14 on: January 23, 2013, 06:49:24 pm » |
yes but the mid value is not stable and can range a few points around 14435
So you need to change it to something like: const int THRESHOLD = 5; ... if ( (val > 14435 - THRESHOLD) && (val < 14435 + THRESHOLD) ) { // At the midpoint }
|
|
|
|
|
Logged
|
|
|
|
|
|