0

This code is part of a long code for a robot that is both obstacle-avoiding and remote control. I am trying to get the code to check both "if" statements when the code number FF807F is received from an IR remote. When the code number is received the program checks both "if" statements and carries out the one that is true. I want the program, once it has finished carrying out the true "if statement, to check both "if" statements again.

void loop () {
if (My_Decoder.value==0xFF807F){
 if (cm<6)
 {
  motor_stop();
  digitalWrite(backlights, HIGH);
  delay(2000);
  motor_backward();
  digitalWrite(backlights, HIGH);
  Serial.println("backward");
  delay(2000);
  motor_stop();
  digitalWrite(backlights, HIGH);
  delay(900);
  motor_right();
  digitalWrite(backlights, LOW);
  Serial.println("right");
  delay(1000);
}
if (cm>6)
 {
  motor_forward();
  digitalWrite(backlights, LOW);
  Serial.println("forward");
}
}
}       
  • What changes My_Decoder.value? If cm is exactly 6 you want to do nothing, is that right? – Nick Gammon Aug 1 '17 at 7:36
1

Your are missing the case when "cm == 6".

You don't need to do two "if"; one "if-else" is enough because is either one or the other.

You don't have/need to check twice; "cm" doesn't change inside the "if". But i you want, use functions:

void loop () {
  if (My_Decoder.value == 0xFF807F) {
    doSomething();
    doSomething();
  }
}

void doSomething() {
  if (cm < 6) {
    motor_stop();
    digitalWrite(backlights, HIGH);
    // ...
    delay(1000);
  } else {
    motor_forward();
    digitalWrite(backlights, LOW);
    Serial.println("forward");
  }
}
  • The above code is working, however I want it to repeat the "void doSomething();" function until another button is pressed. When I press the remote button it will only carry out the function once. – Matthew Amisi Jul 5 '17 at 13:41
0

I am going to assume you don't want to include cm == 6 which is why you have both cases. I would consider using a while loop instead of a singular 'if' statement in order to continuously check if those buttons are being pressed or not (while (cm<6){do this stuff}). Otherwise, it will check once and never check again.

Instead it will jump down to the next 'if' statement, it will fail and then jump to the top of the loop where it will check for the code 0xFF807F. Let me know how it goes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.