0% found this document useful (0 votes)
1 views

Lab5 Mechatronics

The document outlines a lab activity focused on building and testing a line-following robot using infrared sensors. It details the programming of the robot's movement based on sensor readings, including actions for following a line, making corrections, and stopping when no line is detected. Additionally, it describes the use of an IR sensor to initiate movement and the robot's behavior in response to line detection and loss.

Uploaded by

Cheryll Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab5 Mechatronics

The document outlines a lab activity focused on building and testing a line-following robot using infrared sensors. It details the programming of the robot's movement based on sensor readings, including actions for following a line, making corrections, and stopping when no line is detected. Additionally, it describes the use of an IR sensor to initiate movement and the robot's behavior in response to line detection and loss.

Uploaded by

Cheryll Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MECH-4212

MECHATRONICS
LAB-5
Navigating with Line Follower Sensors

Members:
Garret Turner
Mason Dupuis
Cheryll Reyes
Activity 1: Build and Test the Object Detectors

Activity 2 checking the sensor

Output

Observations
The four sensors (L, ML, MR, R) register either 0 or 1, with ML consistently reading 1 while the others
remain 0. This indicates that the middle-left sensor is detecting an object. The objective of this activity
is to ensure that all four sensors are functioning properly and capable of detecting an object
Activity 3 Line following navigation with boebot

Program
#include <Servo.h> // Include the Servo library

Servo servoLeft;
Servo servoRight;

const int sensorFarLeft = 7;


const int sensorMidLeft = 6;
const int sensorMidRight = 5;
const int sensorFarRight = 4;

void setup() {
Serial.begin(115200);
Serial.println("Testing Line Follower - ML & MR Center Priority");

// Setup motors
servoLeft.attach(13);
servoRight.attach(12);
halt();
delay(1000);

// Set sensor pins as INPUT with pull-ups (for active-low sensors)


pinMode(sensorFarLeft, INPUT_PULLUP);
pinMode(sensorMidLeft, INPUT_PULLUP);
pinMode(sensorMidRight, INPUT_PULLUP);
pinMode(sensorFarRight, INPUT_PULLUP);
}

void loop() {
// Read sensor values
byte left = digitalRead(sensorFarLeft);
byte midLeft = digitalRead(sensorMidLeft);
byte midRight = digitalRead(sensorMidRight);
byte right = digitalRead(sensorFarRight);

// Print sensor values for debugging


Serial.print("Sensors: L=");
Serial.print(left);
Serial.print(" ML=");
Serial.print(midLeft);
Serial.print(" MR=");
Serial.print(midRight);
Serial.print(" R=");
Serial.print(right);
Serial.print(" | ");

// Prioritize staying centered when ML and MR are both ON


if (midLeft == 1 && midRight == 1) {
Serial.println("Moving Forward (Centered)");
moveForward(50);
}
// Stop if no line is detected
else if (left == 0 && midLeft == 0 && midRight == 0 && right == 0) {
Serial.println("STOP (No line detected)");
halt();
}
// Slight right correction
else if (midLeft == 1 && midRight == 0) {
Serial.println("Veer Slightly Right");
veerRight(10);
}
// Slight left correction
else if (midRight == 1 && midLeft == 0) {
Serial.println("Veer Slightly Left");
veerLeft(10);
}
// Curve right
else if (right == 1 && midRight == 1) {
Serial.println("Curve Right");
veerRight(15);
}
// Curve left
else if (left == 1 && midLeft == 1) {
Serial.println("Curve Left");
veerLeft(15);
}
// Sharp left turn
else if (left == 1 && midLeft == 0 && midRight == 0 && right == 0) {
Serial.println("Sharp Left");
veerLeft(20);
}
// Sharp right turn
else if (right == 1 && midLeft == 0 && midRight == 0 && left == 0) {
Serial.println("Sharp Right");
veerRight(20);
}
// Stop if all sensors are ON (possible junction)
else if (left == 1 && midLeft == 1 && midRight == 1 && right == 1) {
Serial.println("Moving Forward (Centered)");
moveForward(50);
} else {
Serial.println("Unknown State - Stopping");
halt();
}

delay(50); // Short delay to stabilize readings


}

//-- Motion Functions --


void veerLeft(int time) {
servoLeft.writeMicroseconds(1500 + 25);
servoRight.writeMicroseconds(1500 - 75);
delay(time);
}

void veerRight(int time) {


servoLeft.writeMicroseconds(1500 + 75);
servoRight.writeMicroseconds(1500 - 25);
delay(time);
}

void halt() {
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
}

void moveForward(int time) {


servoLeft.writeMicroseconds(1600);
servoRight.writeMicroseconds(1400);
delay(time);
}

void moveBackward(int time) {


servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1700);
delay(time);
}

Serial Monitor Output

Observations
The line-following robot has four infrared sensors that help it detect and stay on track. It moves straight
when the line is centered, makes small adjustments by turning left or right, and takes sharper turns
when only the far-left or far-right sensor picks up the line. If none of the sensors detect the line, the
robot comes to a stop. Its motors are controlled using PWM signals, which adjust speed and direction
for smooth movement
Activity 4. Exercise (Homework)

Program
#include <IRremote.hpp>
#include <Servo.h>
#include <IRremote.h>

Servo servoLeft;
Servo servoRight;

const int sensorFarLeft = 7;


const int sensorMidLeft = 6;
const int sensorMidRight = 5;
const int sensorFarRight = 4;

bool moving = false; // Tracks if the robot has started moving

void setup() {
Serial.begin(115200);
Serial.println("Testing Line Follower");

// Setup motors
servoLeft.attach(13);
servoRight.attach(12);
halt();
delay(1000);

// Set sensor pins as INPUT with pull-ups (for active-low sensors)


pinMode(sensorFarLeft, INPUT_PULLUP);
pinMode(sensorMidLeft, INPUT_PULLUP);
pinMode(sensorMidRight, INPUT_PULLUP);
pinMode(sensorFarRight, INPUT_PULLUP);
pinMode(3, INPUT); // IR Sensor as INPUT
pinMode(2, OUTPUT); // IR LED Emitter as OUTPUT
}

void loop() {
int detect = irDetect(2, 3, 38000); // Get IR detection result
Serial.println(detect);
// If object detected, set moving to true (start moving)
if (detect == 0 && !moving) {
Serial.println("Start moving");

moving = true;
delay(500); // Short delay to prevent flickering detection
}

// If moving, follow the line


if (moving) {
followLine();
} else {
Serial.println("NO BLACK LINES IN THE AREA");
halt();
}

delay(50); // Short delay for stability


}

void followLine() {
// Read sensor values
byte left = digitalRead(sensorFarLeft);
byte midLeft = digitalRead(sensorMidLeft);
byte midRight = digitalRead(sensorMidRight);
byte right = digitalRead(sensorFarRight);

// Print sensor values for debugging


Serial.print("Sensors: L=");
Serial.print(left);
Serial.print(" ML=");
Serial.print(midLeft);
Serial.print(" MR=");
Serial.print(midRight);
Serial.print(" R=");
Serial.print(right);
Serial.print(" | ");

// Prioritize staying centered when ML and MR are both ON


if (midLeft == 1 && midRight == 1) {
Serial.println("Moving Forward (Centered)");
moveForward(50);
}
// Stop if no line is detected
else if (left == 0 && midLeft == 0 && midRight == 0 && right == 0) {
Serial.println("STOP (No black lines in the area)");
moveBackward(50);
moving = false; // Stop movement only when the line is completely lost
}
// Slight right correction
else if (midLeft == 1 && midRight == 0) {
Serial.println("Veer Slightly Right");
veerRight(10);
}
// Slight left correction
else if (midRight == 1 && midLeft == 0) {
Serial.println("Veer Slightly Left");
veerLeft(10);
}
// Curve right
else if (right == 1 && midRight == 1) {
Serial.println("Curve Right");
veerRight(15);
}
// Curve left
else if (left == 1 && midLeft == 1) {
Serial.println("Curve Left");
veerLeft(15);
}
// Sharp left turn
else if (left == 1 && midLeft == 0 && midRight == 0 && right == 0) {
Serial.println("Sharp Left");
veerLeft(30);
}
// Sharp right turn
else if (right == 1 && midLeft == 0 && midRight == 0 && left == 0) {
Serial.println("Sharp Right");
veerRight(30);
}
// If all sensors detect black (possible junction), move forward
else if (left == 1 && midLeft == 1 && midRight == 1 && right == 1) {
Serial.println("Moving Forward (Centered)");
moveForward(50);
} else {
Serial.println("Unknown State - Stopping");
halt();
moving = false; // Stop movement in case of an unknown state
}
}

int irDetect(int irLedPin, int irReceiverPin, long frequency) {


tone(irLedPin, frequency, 8);
delay(1);
int ir = digitalRead(irReceiverPin);
delay(1);
return ir;
}

//-- Motion Functions --


void veerLeft(int time) {
servoLeft.writeMicroseconds(1500 + 10);
servoRight.writeMicroseconds(1500 - 40);
delay(time);
}

void veerRight(int time) {


servoLeft.writeMicroseconds(1500 + 40);
servoRight.writeMicroseconds(1500 - 10);
delay(time);
}

void halt() {
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
}

void moveForward(int time) {


servoLeft.writeMicroseconds(1575);
servoRight.writeMicroseconds(1425);
delay(time);
}

void moveBackward(int time) {


servoLeft.writeMicroseconds(1400);
servoRight.writeMicroseconds(1600);
delay(time);
}

Serial Monitor Output

Observations
The robot remains still until its IR sensor detects an object. Once detected, it waits for two seconds
before starting to move. It then follows a black line using four infrared sensors at the front, with the
middle-left and middle-right sensors playing a key role in maintaining stability. If the robot loses track
of the line, it stops and resets its movement flag to zero

You might also like