Strange If Statement Behavior

Hey guys,

I am a novice in electronics and coding, so my question could be silly, thank you for understanding first,

I have a code which has if statements. Code runs two motors in one direction via two motor drivers bts7960B. When I run the code, in each condition, only one motor driver works, so only one motor works. I hav investihgate the problem, then i found a syntax mistake in If statement which can you find in below code.

what i am thinking is, how the code run only one motor, while there is a syntax error in both if statements? Normally, since if statement are faulty, no motor should run, instead of running only right motor right?

if (aoutput > pos); {

rmotor();

}

if (aoutput < pos); {

lmotor();

}

void rmotor() {

analogWrite(RPWM1,conout);
digitalWrite(LPWM1,LOW);

analogWrite(RPWM2,LOW);
digitalWrite(LPWM2,LOW);
delay (25);

}

void lmotor() {

analogWrite(RPWM1,LOW);
digitalWrite(LPWM1,LOW);

analogWrite(RPWM2,conout);
digitalWrite(LPWM2,LOW);
delay (25);

}

I did not understand why only one motor worked. I have prepared both conditions (i have an lcd which shows aoutput and pos values) and measured voltages on both drivers, however only the right motor have working.

Very strange situation for me and try to figure out. I will appreciate, IF :slight_smile: anybody can help me.

Regards.

if (aoutput > pos); {

Not strange at all. It is doing what you told it to, but the semicolon should not be there if you want the if to work otherwise the code in the braces will always be executed.

Hi, UKHeliBob,

Thank you for your comment, then as i understand, since there is a semicolon before braces, code directly implements the rmotor() function and dismiss if statement right? So if i may ask another question, there is also a lmotor() function below, why does not it implement that function after rmotor() command?

I am a little confused, sorry for a very basic question.

So if i may ask another question, there is also a lmotor() function below, why does not it implement that function after rmotor() command?

Your code calls both "rmotor" and "lmotor".

You can prove this by putting a print in each.

AWOL:
Your code calls both "rmotor" and "lmotor".

You can prove this by putting a print in each.

Yes i think so after your comments, i will check with a print command, then maybe a wiring/connection or a misplaced pin causing lmotor's driver malfunction. therefore, i cant measure a voltage on it.

Thank you for your support.