Line follow module

SMARS IR line following module


Stl file - ir_sensor_a.stl

Stl file - ir_sensor_b.stl

Line follow module image

Line follow code:

#include <AFMotor.h>

// SMARS Demo 1 with Line sensor
// This sketch makes the robot move inside a delimitated Area 
// (you can make a perimeter with insulating tape)
// you'll need an Adafruit Motor shield V1 https://goo.gl/7MvZeo 
// and a IR sensor https://goo.gl/vPWfzx

AF_DCMotor R_motor(2); // defines Right motor connector
AF_DCMotor L_motor(1); // defines Left motor connector

// declare variables
int lineNumber; //defines the variable where it will store the line 
// sensor value

void setup() {
    Serial.begin(9600); // sets up Serial library at 9600 bps

    // changes the following values to make the robot drive as straight 
    // as possible

    L_motor.setSpeed(200); // sets L motor speed
    R_motor.setSpeed(140); // sets R motor speed
    R_motor.run(RELEASE); // turns L motor on
    L_motor.run(RELEASE); // turns R motor on
}

void loop() {
    // Read the sensor value from pin A4 and
    // stores it in the variable lineNumber
    lineNumber = analogRead(A4);

    // repeats the following part of code until the light sensor
    // will find a darker zone
    while(lineNumber < 800)
    
    {
        L_motor.run(FORWARD); // moves motor L Forward
        R_motor.run(FORWARD); // moves motor L Forward
        
        // reads the sensor value from pin A4
        // and stores it in the variable lineNumber
        lineNumber = analogRead(A4);
    };
    
    // the following operations will make the robot goes 
    // backward for 2 seconds and turns left for 1.5 seconds
    R_motor.run(RELEASE);
    L_motor.run(RELEASE);
    
    //go backward
    R_motor.run(BACKWARD);
    L_motor.run(BACKWARD);
    delay(2000);
    
    // turning left
    R_motor.run(FORWARD);
    L_motor.run(BACKWARD);
    delay(1500);

    // send the value to the serial monitor
    Serial.println(lineNumber);
}