Entering 'else if' statement when it should not meet the condition to enter it.

In my program I’m making a cart move and dodge very basic obstacles using sensors and mechanical devices. The sensors read the distance and have the program determine what to do with those readings.

However when I run the program if it enters the main if statement:

if( distanceL <= 200 || distanceR <= 200 )//main sensors

and inside it reaches the else statement (where distanceFR and distanceFL are less than 200) for the first if,else if,else part where it will print.

Serial.println(“Cant dodge anything, should be stopped.”);

The program will make it to the else if in the next if,else if,else part even though the condition, which I printed right before it should be checked twice, is NOT met. and it will enter the else if statement:

else if (turningdirection = 1)
{ //we stopped and turned to the right, need to go and turn left now.
Serial.println(“I dodged to the right so I need to turn left to ‘correct’ myself”);

I have attached the main loop as well as the serial print for when this bug is occurring when running the program.
turningdirection is initialized as “int turningdirection;”
Also I am running this on an Arduino Due.

main loop.txt (3.67 KB)

bug.txt (237 Bytes)

KlaytonH:
The program will make it to the else if in the next if,else if,else part even though the condition, which I printed right before it should be checked twice, is NOT met.

wat.
okay, herels what i’m pulling
1)your if else statement won’t work, when a certain code is executed.
So,
a)what does it do when you run it?
b) what do you want it to do?

else if (turningdirection = 1)
{ //we stopped and turned to the right, need to go and turn left now.
Serial.println("I dodged to the right so I need to turn left to 'correct' myself");

should be turningdirection == 1

its wrong in a couple of places in your code.

OMG I just looked this up to be sure, I was thinking I only needed one '=' for int types for some reason. Thanks, I'll try this in the next few days, appreciate it. Hopefully you won't have to hear from me again :smiley: