Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #15 on: January 23, 2013, 06:49:58 pm » |
Then you need something like (14330 < val) || (val < 14340))
or (abs (val-14335) < 5)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #16 on: January 23, 2013, 06:52:30 pm » |
Arrch i'll give that a go
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #17 on: January 23, 2013, 06:52:54 pm » |
This thread is a comedy of errors 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #18 on: January 23, 2013, 06:57:35 pm » |
Guys, Arrch this worked. int rcpin =7; int motpin = 13; const int THRESHOLD = 5;
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) if ( (val > 14435 - THRESHOLD) && (val < 14435 + THRESHOLD) ) { digitalWrite(motpin, LOW); } else { digitalWrite(motpin, HIGH); } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #19 on: January 23, 2013, 06:59:39 pm » |
trying to understand why it worked and not the previous code. could it have something to do with the values continually moving slightly.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #20 on: January 23, 2013, 07:04:05 pm » |
exactly. All the other code required the servo to read *exactly* 14335.
Again, run the code yourself!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #21 on: January 23, 2013, 07:12:30 pm » |
sorry guys i can't get my head round this.. i thought if i use val > 14335 it would work for any value greater than 14335 regardless of wether the value moves slightly.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 5
Posts: 471
what?
|
 |
« Reply #22 on: January 23, 2013, 07:16:56 pm » |
pulseIn(rcpin, INPUT); shouldn't this be pulseIn(rcpin, LOW); I think INPUT is defined as 0 anyway but just to clarify the code shouldn't it be LOW?
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #23 on: January 23, 2013, 07:19:33 pm » |
exactly. All the other code required the servo to read *exactly* 14335.
I never saw any code of the OPs of the form ( ( 14335 ? val) ?? (val ? 14335)), other than your reply to OP which I thought was a typo and Arrch's misreading of it. Did OP edit his 14335/14535s in the original post? Again, run the code yourself!
Agreed
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 44
|
 |
« Reply #24 on: January 23, 2013, 07:25:25 pm » |
Guys thanks for all your help on this, it's late here in the UK so calling it a day. i'll run the code and try and get a better understanding. fantastic forum and thanks for helping me out.
Cheers
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #25 on: January 23, 2013, 07:30:52 pm » |
sorry guys i can't get my head round this.. i thought if i use val > 14335 it would work for any value greater than 14335 regardless of wether the value moves slightly.
Hard to get your head around because you've gotten a lot of ideas, and some from every one of us has been wrong in one way or the other  You could probably understand better if you broke it up like this? if (val > 15555){ // joystick is FORWARD } else if (val < 14444) { // joystick is REARWARD } else { // joystick is NEUTRAL }
I changed the numbers so we would quit misreading 14335/14535
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36443
Seattle, WA USA
|
 |
« Reply #26 on: January 23, 2013, 07:45:39 pm » |
I changed the numbers so we would quit misreading 14335/14535 Actually, this is a perfect illustration of why magic numbers are a bad idea. #define LOW_VAL 14335 #define HIGH_VAL 14535
if(val < LOW_VAL) { } else if(val > HIGH_VAL) { } else { } means you correct an error/erroneous value in one place, and the logic is far easier to see.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #27 on: January 23, 2013, 07:48:02 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. Actually I think that code simplifies to if (val < 14535) { Which is still in error, but not that error 
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #28 on: January 23, 2013, 07:48:58 pm » |
Actually, this is a perfect illustration of why magic numbers are a bad idea.
Absolutely!
|
|
|
|
|
Logged
|
|
|
|
|
|