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");
}
}
}
4 Answers 4
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 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– Matthew Amisi07/05/2017 13:41:42Commented Jul 5, 2017 at 13:41
if (My_Decoder.value == 0xFF807F) {
if (cm<6)
Are you wanting both "ifs" to occur? Maybe a bitwise operator like logical AND (&
)?
-
&
only makes sense if the bits are related to each other--a logical&&
would be used here;value == 0xFF807F && cm < 6
.Dave Newton– Dave Newton05/23/2021 16:30:01Commented May 23, 2021 at 16:30
This looks like a good use for a State Machine.
It would look something like this:
typedef enum State
{
STATE_IDLE,
STATE_THIS,
STATE_THAT,
STATE_OTHER
} State;
State state;
const int IR_BUTTON_1 = 0xFF807F;
const int IR_BUTTON_2 = 0x112233;
const int IR_BUTTON_3 = 0X445566;
const int IR_BUTTON_STOP = 0x778899;
void setup()
{
Serial.begin(115200);
Serial.println(F("setup()"));
state = STATE_IDLE;
}
void loop()
{
Serial.println(F("loop()"));
switch(my_decoder.value)
{
case IR_BUTTON_1:
state = STATE_THIS;
Serial.println(F("Entered STATE_THIS"));
break;
case IR_BUTTON_2:
state = STATE_THAT;
Serial.println(F("Entered STATE_THAT"));
break;
case IR_BUTTON_3:
state = STATE_OTHER;
Serial.println(F("Entered STATE_OTHER"));
break;
case IR_BUTTON_STOP:
state = STATE_IDLE;
Serial.println(F("Entered STATE_IDLE"));
break;
}
switch(state)
{
case STATE_THIS:
DoThis();
break;
case STATE_THAT:
DoThat();
break;
case STATE_OTHER:
DoOther();
break;
}
}
void DoThis()
{
Serial.println(F("DoThis()"));
}
void DoThat()
{
Serial.println(F("DoThat()"));
}
void DoOther()
{
Serial.println(F("DoOther()"));
}
This locks the state machine into an active state as per the OP's comment:
I want it to repeat the "void doSomething();" function until another button is pressed.
However, as Dave Newton pointed out in the comments, locking a machine into a particular active state can be dangerous, so I've edited the code to add a STOP button to transition back to the IDLE state. But this still relies on the operator pressing the STOP button to return to idle; there is no automatic return to idle which could be a hazard depending on the application.
-
1Hm, maybe. I think if this was going to be a "non-degenerate" state machine, though, you'd have a "received input" state that then looped over the
cm
value until whatever its "done" condition is, then go back into a "waiting" or "processing" state, depending on what the OP's actual use-case is. As described here this is just breaking up a value check and a method call. The methods, all for it. The handling of states--dangerous, because no example of exiting a state is provided :)Dave Newton– Dave Newton05/24/2021 00:48:54Commented May 24, 2021 at 0:48
My_Decoder.value
? If cm is exactly 6 you want to do nothing, is that right?