Line Follower Sketch

In order to complete the line follower challenge the robot had to follow a track made of black electrical tape on white paper without deviating from the track. The robot needed to make the complete loop in both directions. At no point could the robot receive any sort of manual override from the smartphone, or be touched by a human operator. The robot also needed to be able to stop when it detected an obstacle in front of it. In order to complete this challenge, the Arduino sketch received direct input from the two infrared line sensors.


Arduino Sketch

#include <Adafruit_MotorShield.h>
#include <lineControl.h>
#include <linesensor.h>
#include <motor.h>
#include <Range_Echo.h>
#include <Wire.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
const float UNIT_CONVERSION = 0.003937; volatile float frontSensorDistance = 0.0; bool rightLineSensor = 1; bool leftLineSensor = 1;
linesensor *leftSensor; linesensor *rightSensor;
motor *RFMotor;// = new motor(&AFMS, 1, 0); //right front motor *LFMotor;// = new motor(&AFMS, 2, 1); //left front motor *LRMotor;// = new motor(&AFMS, 3, 1); //left rear motor *RRMotor;// = new motor(&AFMS, 4, 0); //right rear
Range_Echo *frontRange;// = new Range_Echo(10, 11);
void setup() { leftSensor = new linesensor(13); rightSensor = new linesensor(12); frontRange = new Range_Echo(10, 11); RFMotor = new motor(&AFMS, 1, 0); //right front LFMotor = new motor(&AFMS, 2, 1); //left front LRMotor = new motor(&AFMS, 3, 1); //left rear RRMotor = new motor(&AFMS, 4, 0); //right rear
frontSensorDistance = (frontRange -> Timing()) * UNIT_CONVERSION;
rightLineSensor = rightSensor; leftLineSensor = leftSensor; }
void loop() { Serial.begin(115200); AFMS.begin();
frontSensorDistance = (frontRange -> Timing()) * UNIT_CONVERSION;
while ((frontSensorDistance > 1.0) && (frontSensorDistance < 7.5)) { LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); frontSensorDistance = (frontRange -> Timing()) * UNIT_CONVERSION; }
if (frontSensorDistance > 8.0) {
if (digitalRead(13) == HIGH) { LFMotor->setDrive(0.0, FORWARD); LRMotor->setDrive(0.0, FORWARD); RFMotor->setDrive(230.0, FORWARD); RRMotor->setDrive(230.0, FORWARD); } else { LFMotor->setDrive(230.0, FORWARD); LRMotor->setDrive(230.0, FORWARD); RFMotor->setDrive(0.0, FORWARD); RRMotor->setDrive(0.0, FORWARD); } if (digitalRead(12) == HIGH) { LFMotor->setDrive(230.0, FORWARD); LRMotor->setDrive(230.0, FORWARD); RFMotor->setDrive(0.0, FORWARD); RRMotor->setDrive(0.0, FORWARD); } else { LFMotor->setDrive(0.0, FORWARD); LRMotor->setDrive(0.0, FORWARD); RFMotor->setDrive(230.0, FORWARD); RRMotor->setDrive(230.0, FORWARD); } } }
//=================================================================================================== //=======================================COMMANDS CHEAT SHEET======================================== //===================================================================================================
//**********************************************ENCODER********************************************** // encoder(unsigned int slots, unsigned int diameter, unsigned int pin) // void updateTime(unsigned int time) //function to update the current time from the internal timer // double getSpeed() //calculate the speed // void zeroSpeed(); //set the speed to zero
//********************************************LINECONTROL******************************************** // lineControl(linesensor *leftPTR, linesensor *rightPTR, speedcontrol *leftSpeed, speedcontrol *rightSpeed); // void detectNone(); // void updateSpeed(); // void detectLeft(); // void detectRight();
//*********************************************LINESENSOR******************************************** // linesensor(unsigned int pin, byt sensornumber); // bool linestatus();
//***********************************************MOTOR*********************************************** // motor(Adafruit_MotorShield*shieldPtr,byte motorNumber, boolean polarity); // void setDrive(unsigned int dutyCycle, unsigned int command) // set the motor's direction // void stop(); // emergency stop - Set speed to zero // unsigned int getDrive(); // returns the speed
//**********************************************RANGE_ECHO******************************************* // Range_Echo(int TP, int EP); // long Timing(); // calculates the distance for which the object is from the sensor // long Ranging(int sys); // sets the units, SYS for centameters, INC for inches
//*********************************************SPEEDCONTROL****************************************** // speedcontrol(HardwareSerial *port, encoder *encoderPtr, motor *motorPtr1, motor *motorPtr2,double sampleTime); // void setDesiredSpeed(double speed); // sets the desired speed // double getDesiredSpeed(); // returns the value of the desired speed // void enable(); // sets the internal enable flag to 1 // void disable(); // sets the internal enable flag to 0 // void update(); // calculates the current speed // void setPGain(double gain); // void setIGain(double gain); // void setDGain(double gain); // void setILimit(double limit);