Go Down

Topic: problem rc receiver signal (Read 2 times) previous topic - next topic

mustang493

Tried this no joy

Code: [Select]
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);
  }
}

Arrch

Code: [Select]

if ( 14335 < val || val < 14535) {


Can be simplified to

Code: [Select]
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.

johncc


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

mustang493

yes but the mid value is not stable and can range a few points around 14435

Arrch


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:

Code: [Select]
const int THRESHOLD = 5;
...
if ( (val > 14435 - THRESHOLD) && (val < 14435 + THRESHOLD) )
{
 // At the midpoint
}

Go Up