Go Down

Topic: code problem about a line follower (Read 102 times) previous topic - next topic

saurio

Hi.

I am making a line follower car but in the programming i cannot make a motor stop when the sensor detects the black line, it seems that the motors are either always running or are always stopped.
This is my code:
//motor derecho es A
// motor izquierdo es B
int enableA = 5;
int motorA1 = 6;
int motorA2 = 7;
//Motor B
int enableB = 8;
int motorB1 = 9;
int motorB2 = 10;
const int i =13;
const int l =12;
void setup() {

Serial.begin (9600);
//configuración
pinMode (enableA, OUTPUT);
pinMode (motorA1, OUTPUT);
pinMode (motorA2, OUTPUT);  
pinMode (3, OUTPUT);
pinMode (2, INPUT);
pinMode (enableB, OUTPUT);
pinMode (motorB1, OUTPUT);
pinMode (motorB2, OUTPUT);  
digitalWrite (motorA1,LOW);
digitalWrite (motorA2,LOW);  
digitalWrite (motorB1,LOW);
digitalWrite (motorB2,LOW);  
}

void loop() {
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);


if (digitalRead (l) == HIGH){
  Serial.println("se salio");
  digitalWrite (motorA1,HIGH);
digitalWrite (motorA2,LOW);  
digitalWrite (motorB1,LOW);
digitalWrite (motorB2,HIGH);  
}


if (digitalRead(i)== HIGH);{
Serial.println("se salio");
digitalWrite (motorA1,LOW);
digitalWrite (motorA2,HIGH);  
digitalWrite (motorB1,HIGH);
digitalWrite (motorB2,LOW);

}
if ((digitalRead(i)==LOW) and (digitalRead (l)==LOW))

 digitalWrite (motorA1,LOW);
digitalWrite (motorA2,HIGH);  
digitalWrite (motorB1,LOW);
digitalWrite (motorB2,HIGH);
}


If anyone knows the problem I would appreciate your help.

pcbbc

#1
Aug 30, 2020, 09:23 pm Last Edit: Aug 30, 2020, 09:34 pm by pcbbc
Please use [​code]code tags[​/code] around code when posting on the forum.

It's subtle, but spot what is wrong with this "if" compared to the others (correct) in you code...
Code: [Select]
if (digitalRead(i)== HIGH);{

Edit:

Variable names "i" and "l" are bad choices of names for pins. They tell you nothing about what the variable represents.  Something like "pinLeftSensor" and "pinRightSensor' are better.

Also it is generally a good idea to read the states of the pins ONCE into variables at the start of loop() and then test them, rather than reading pins multiple times...
Code: [Select]
byte stateLeftSensor = digitalRead(pinLeftSensor);
byte stateRightSensor = digitalRead(pinRightSensor);
if (stateLeftSensor == HIGH) {
....


This debug is not helpful...
Code: [Select]
Serial.println("se salio");
...it doesn't tell you Which of "i" or "l" are HIGH.

Also there is no debug for...
Code: [Select]
if ((digitalRead(i)==LOW) and (digitalRead (l)==LOW))
And also, more importantly, I now also notice it contains a bug as well.
It is missing a { } code block.
That means only the first statement that follows the "if" is part of that if.

ballscrewbob

@saurio

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.



It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

TomGeorge

#3
Aug 31, 2020, 11:31 am Last Edit: Aug 31, 2020, 11:32 am by TomGeorge
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

saurio

Thank you very much for the help, it helped me a lot.

Sorry for the errors in the forum.

Go Up