Struggling with If/Else

Hey everyone, my name is Jake.

I’m having a problem to where once an if condition is started, it never hits the else condition.

Here is part of my code:

if(cm <= 20){
moveStop();
delay(50);
turnRight();
delay(50);
moveStop();
} else {
moveForward();
}

If the robot starts to turn right, it never again moves forward. However, if it has never turned right, it will always move forward. Not sure what is going on.

Thank you

Edit: I should add that once it becomes greater than 20 cm, then it stops turning right.

Try this (please remember to use code tags next time)

  if (cm <= 20) {
    moveStop()
  };
  else {
    moveForward();
  }
  delay(50);
  turnRight();
  delay(50);
  moveStop();

Sorry, new to this, are code tags comments?

And this causes it to just constantly turn right, it doesn't move forward at all.

Your code is correct. Did you ensure that cm is greater than 20 in any case (serial print out)?

Yes, I’m using serial print.

Here is my full code (total disclosure, I’m frankensteining this code from multiple different sources, I didn’t write much of this)

const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

int trigPin = 11;    // Trigger
int echoPin = 12;    // Echo
long duration, cm, inches;
int leftRight = 0;

boolean goesForward = false;


void setup(){
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

}

void loop(){


  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // Convert the time into a distance
  cm = (duration/2) / 29.1;     
  inches = (duration/2) / 74;   
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(250);
  
  if(cm <= 20){
    moveStop();
    delay(50);
    turnRight();
    delay(50);
    moveStop();
  } else {
     moveForward();
  } 
}




void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

If the robot starts to turn right, it never again moves forward.

Does this mean it was moving forward before turning right?

The forward move is controlled with a boolean variable which needs to be set back to false for a second forward move. The only place I see that happening is in moveBackward().

void moveForward(){

  if(!goesForward){

    goesForward=true;
   
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

You may want to add the resetting of the boolean control variable in the left and right turn sections as well as the moveBackward().

goesForward=false;