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:


  //                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
  //declaring pin types
  //begin serial communication

void loop(){
  //printing values of the sensors to the serial monitor
  //line detected by both
  if(analogRead(lefts)<=400 && analogRead(rights)<=400){
  //line detected by left sensor
  else if(analogRead(lefts)<=400 && !analogRead(rights)<=400){
    //turn left;;
  //line detected by right sensor
  else if(!analogRead(lefts)<=400 && analogRead(rights)<=400){
    //turn right;;
  //line detected by none
  else if(!analogRead(lefts)<=400 && !analogRead(rights)<=400){

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?


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>


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:
//set the speed to 200/255


void loop() {
// put your main code here, to run repeatedly:


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