Infrared sensor maze

I want to get my elegoo 3.0 robot car to solve a maze using left hand strategy. I have attempted many things, and they all failed because i am new at this.

here is my code:
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)
#define LT_R !digitalRead(10)

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11

#define carspeed 125

void forward(){
analogWrite(ENA, carspeed);
analogWrite(ENB, carspeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go forward!");
}

void left(){
analogWrite(ENA, carspeed);
analogWrite(ENB, carspeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("go left!");
}

void right(){
analogWrite(ENA, carspeed);
analogWrite(ENA, carspeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go right!");
}

void setup(){
Serial.begin(9600);
pinMode(LT_M,INPUT);
pinMode(LT_L,INPUT);
pinMode(LT_R,INPUT);
}

void loop() {
if(LT_M){
forward();
while(LT_L, false);
}
else if(LT_L){
left();
while(LT_M, false);
}
else if(LT_R){
right();
while(LT_M and LT_L, false);
}
}

any advise?

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please explain exactly what the problem is that you're having with the code you posted. Presumably you're expecting it to do something but it's doing something else. We need more information before we can help you.

How do you want to control the exact robot orientation, after a left or right turn?

IMO you need more feedback for proper movements.

Does your code compile for example does this not generate errors;
while(LT_L, false);

Put print statements throughout your code.

That's not a syntactic but a logic error: the condition is always false, so that the loops never execute.

DrDiettrich:
That's not a syntactic but a logic error: the condition is always false, so that the loops never execute.

The syntax of the while statement is;
while(expression) statement
I am sure you are correct but I am a bit surprised that the compiler would accept
LT_L,false
or
!digitalRead(2), false
as a valid boolean expression.
Also are you right in saying it would always be false, would the result not be arbitrary depending on the compiler?

but I am a bit surprised that the compiler would accept
LT_L,false
or
!digitalRead(2), false
as a valid boolean expression.

Read up on the comma operator. That is what is being misused here.

OP: Those #define statements suck. They do NOTHING to make the code more readable. Get rid of them.

  pinMode(LT_M,INPUT);

becomes

  pinMode(!digitalRead(4),INPUT);

which is clearly nonsense. What pin did you just set the mode for?

The pins you have named IN1 to IN4 are input to the H-Bridge, NOT to the Arduino. Therefore, they have stupid names, and you have left them as INPUT, so they are not going to make the H-bridge do anything.

PaulS:
Read up on the comma operator. That is what is being misused here.
...

Damn me, I never knew about or missed the comma operator :slight_smile:

"The comma operator is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered".

So;

a = (b=3, b+2);

sets b to 3 and a to 5. That is really useful and readable.