Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Scott Beasley
Published © CC BY-SA

Simple Line Follower Robot Using a Actobotics Runt Rover

A basic line follower bot.

BeginnerFull instructions provided2 hours2,160
Simple Line Follower Robot Using a Actobotics Runt Rover

Things used in this project

Hardware components

Sprout Runt Rover chassis
×1
Dagu Micro Magician controller V2
×1
TCRT5000 IR board
×3
Female/Female Jumper Wires
Female/Female Jumper Wires
×1
Actobotics 90deg bracket
×1
3.85" Actobotics small beam part #585410
×2
.5” long 6/32 or m3 screws with nut
×2
.25” long 6/32 or m3 screws with nut
×3

Software apps and online services

The Metro library
The Micro magician library
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Dagu Micro Magician wire diagram

Dagu Micro Magician wire diagram

Code

Dagu Micro Magician code

Arduino
Version for the Dagu Micro Magician controller
/*
   Basic line follower.

   Goal in life...
      Finding lines to follow :-)

   Written by Scott Beasley - 2015
   Free to use or modify. Enjoy.

   NOTES: Before starting the bot up, place it where the CENTER sensor is on the
   line and the left and right is off.
*/

/*
   Uses the MicroM library for the Micro Magician robot contoller board.

   Download from https://sites.google.com/site/daguproducts/home/arduino-libraries
   Unzip and copy to your Arduino library folder or follow the instructions
   here: http://arduino.cc/en/guide/libraries

   Uses the Metro libary as well. See http://playground.arduino.cc/code/metro
   for downloding and more infomation.
*/

#include <microM.h>
#include <Metro.h>

// Sensor pin defines
#define SENSOR_L A3 // Left sensor pin
#define SENSOR_M A4 // Middle sensor pin
#define SENSOR_R A5 // Right sensor pin

// Speed defines
#define ONLINESPEED 110 // Speed to move when the bot in on the line
#define FORWARDTURNSPD 100 // Turning forward speed
#define BACKWARDTURNSPD -85 // Backup turning speed

// Micro Magician uses 0 for no brake and 1 for brake
#define BRAKEOFF 0
#define BRAKEON 1

/*
   Globals area. Try to keep them to a minimum :-)
*/

// Create the motor and Metor objects with use to interface with
Metro sensorrd = Metro (5); // Timer for every 5ms

// Storage for the "calibation" numbers. Only using the online_avg
// but you may want to look at the offline one for compairing sake.
int online_avg = 0, offline_avg = 0;

void setup ( )
{
   // Do a SIMPLE calibration. Not a great one, but one.
   for (int i=0; i < 20; i++) {
      offline_avg += analogRead (SENSOR_L);
      offline_avg += analogRead (SENSOR_R);
      online_avg += analogRead (SENSOR_M);
   }

   online_avg = online_avg / 20;
   offline_avg = offline_avg / 40; // Average of the Left and Right sensors

   // Start moving the bot forward.
   microM.Motors (ONLINESPEED, ONLINESPEED, BRAKEOFF, BRAKEOFF);

   Serial.begin (9600);
}

void loop ( )
{
   // Read the sensors every 5ms and act upon it.
   if (sensorrd.check ( ) != 1) {
      return;
   }

   // Read all 3 senors and return line position condition
   byte direction = read_sensors ( );

   switch (direction) {
      case 0: // On the line Middle sensor. Keep moving forward.
         microM.Motors (ONLINESPEED, ONLINESPEED, BRAKEOFF, BRAKEOFF);
         Serial.println ("moving forward");
         break;

      case 1: // On the line Left sensor. Move right forward.
         microM.Motors (FORWARDTURNSPD, BACKWARDTURNSPD, BRAKEOFF, BRAKEOFF);
         Serial.println ("moving right");
         break;

      case 2: // On the line Right sensor. Move left forward.
         microM.Motors (BACKWARDTURNSPD, FORWARDTURNSPD, BRAKEOFF, BRAKEOFF);
         Serial.println ("moving left");
         break;

      case 3: // Lost! Move right (spin).
         microM.Motors (BACKWARDTURNSPD, 0, BRAKEOFF, BRAKEOFF);
         Serial.println ("lost!");
         break;
   }
}

// Read all the sensors and figure out where the line is
byte read_sensors ( )
{
   byte sensors = 3; // Init with lost setting and change otherwise.

   // Read Sensor in middle
   int mid_read = readSensor (SENSOR_M, 2) - online_avg;

   // Read Sensor on Left
   int left_read = readSensor (SENSOR_L, 2) - online_avg;

   // Read Sensor on Right
   int right_read = readSensor (SENSOR_R, 2) - online_avg;

   // Figure out line postion in regards to the 3 sensors
   // The sensor with the largest number wins!
   if (mid_read > left_read && mid_read > right_read)
      sensors = 0;
   else if (left_read > right_read)
      sensors = 1;
   else if (left_read < right_read)
      sensors = 2;

   // Return postion found or '3' if no line was found
   return sensors;
}

// Read sensor n times and return an average reading
int readSensor (int analog_pin, int sample_rate)
{
    int sum = 0;

    for (int i = 0; i < sample_rate; i++) {
       sum += analogRead (analog_pin);
    }

    return (sum / sample_rate);
}

Credits

Scott Beasley
9 projects • 62 followers
Well odd
Contact

Comments

Please log in or sign up to comment.