Arduino line follower Help

Hi, im not sure if this is the right place to post this but i am in need of some help. I am new to Arduino and thought i would try and follow a YouTube tutorial on how to make a line follower. i followed a few tutorials and bought the equipment they recommended and i cant seem to get it to work at all. I have linked the code i am using and the pictures. I am guessing im doing something very stupid! Many Thanks :slight_smile:

Code: https://www.instructables.com/id/Autonomous-Line-Follower-Robot-Using-Arduino/

   ////////////////////////////////////////////////////////
  //                LinoBot v1.0                        //             
 //               By Aarav Garg                        //
////////////////////////////////////////////////////////

//I have added the possibilities of testing
//The values of analogRead could be changed for trouble shooting

//including the libraries
#include <AFMotor.h>

//defining pins and variables
#define lefts A4 
#define rights A5 

//defining motors
AF_DCMotor motor1(4, MOTOR12_8KHZ); 
AF_DCMotor motor2(3, MOTOR12_8KHZ);
/*
AF_DCMotor motor1(3, MOTOR12_8KHZ); 
AF_DCMotor motor2(4, MOTOR12_8KHZ);
 */

void setup() {
  //setting the speed of motors
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  //declaring pin types
  pinMode(lefts,INPUT);
  pinMode(rights,INPUT);
  //begin serial communication
  Serial.begin(9600);
  
}

void loop(){
  //printing values of the sensors to the serial monitor
  Serial.println(analogRead(lefts));
  Serial.println(analogRead(rights));
  //line detected by both
  if(analogRead(lefts)<=400 && analogRead(rights)<=400){
    //stop
    motor1.run(RELEASE);
    motor2.run(RELEASE);
  }
  //line detected by left sensor
  else if(analogRead(lefts)<=400 && !analogRead(rights)<=400){
    //turn left
    motor1.run(BACKWARD);
    motor2.run(FORWARD);
    /*
    motor1.run(RELEASE);
    motor2.run(FORWARD);
     */
  }
  //line detected by right sensor
  else if(!analogRead(lefts)<=400 && analogRead(rights)<=400){
    //turn right
    motor1.run(FORWARD);
    motor2.run(BACKWARD);
    /*
    motor1.run(FORWARD);
    motor2.run(RELEASE);
     */
  }
  //line detected by none
  else if(!analogRead(lefts)<=400 && !analogRead(rights)<=400){
    //stop
    motor1.run(FORWARD);
    motor2.run(FORWARD);
    /*
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
     */
  }
  
}

We can't see your code.

Use code tags.

Please post the actual code you are using (in </> code tags as described in “How to use this forum - please read”). Not a link to a web page which as far as I can see has no code on it at all.

I can’t tell much useful from your pictures except that you do seem to be using a separate battery for the motors which is good.

What exactly does “i cant seem to get it to work at all” actually mean? Does the code compile and load? Since you still have it plugged into USB have you trying putting some Serial.prints into your code so you can see what it’s doing e.g. are the sensors working, recognising a line etc?

Steve

I think i added the code correctly? Thanks for the replies!

Sorry for the lack of information, i was following the tutorial and the motors are meant to start turning etc i can only assume i have done something very stupid. The code complies and uploads find, the IR sensors are also working as when i block them i can see the LED indicator change.

Many Thanks

<>#include <AFMotor.h>

//define

AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);

//declaring pins
int LeftSensor = A0;
int RightSensor = A1;
int L_sensor_val=0;
int R_sensor_val=0;

int threshold=500;

void setup() {
// put your setup code here, to run once:
motor1.setSpeed(150);
motor2.setSpeed(150);
//set the speed to 200/255

}

void loop() {
// put your main code here, to run repeatedly:
L_sensor_val=analogRead(LeftSensor);
R_sensor_val=analogRead(RightSensor);

if(L_sensor_val>threshold&&R_sensor_val>threshold){
motor1.run(FORWARD);
motor2.run(FORWARD);
}
if(L_sensor_val<threshold&&R_sensor_val<threshold){
motor1.run(BACKWARD);
motor2.run(FORWARD);
}
if(L_sensor_val<threshold&&R_sensor_val>threshold){
motor1.run(BACKWARD);
motor2.run(FORWARD);
}
if(L_sensor_val>threshold&&R_sensor_val<threshold){
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
}
</>

When you wrote a simple sketch to test the motors without the sensors, that worked, right?

Yes