0% found this document useful (0 votes)
13 views3 pages

3 Senor Max

Uploaded by

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

3 Senor Max

Uploaded by

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

#include <AFMotor.

h>
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR34_64KHZ);
AF_DCMotor motor4(4, MOTOR34_64KHZ);

// Define pins for the three IR sensors


int leftSensor = A0;
int centerSensor = A1;
int rightSensor = A2;

void setup()
{
Serial.begin(9600);

// Set motor speeds


motor1.setSpeed(160);
motor2.setSpeed(160);
motor3.setSpeed(160);
motor4.setSpeed(160);

// Set IR sensor pins as input


pinMode(leftSensor, INPUT);
pinMode(centerSensor, INPUT);
pinMode(rightSensor, INPUT);
}

void loop()
{
// Read sensor values
int leftValue = digitalRead(leftSensor);
int centerValue = digitalRead(centerSensor);
int rightValue = digitalRead(rightSensor);

// Print sensor values for debugging


Serial.print("Left: "); Serial.print(leftValue);
Serial.print(" | Center: "); Serial.print(centerValue);
Serial.print(" | Right: "); Serial.println(rightValue);

// Line following logic

// Case 1: If center sensor is on the line, go straight


if(centerValue == LOW && leftValue == HIGH && rightValue == HIGH)
{
moveForward();
}
// Case 2: If left sensor is on the line, turn right
else if(leftValue == LOW && centerValue == HIGH && rightValue == HIGH)
{
turnRight();
}
// Case 3: If right sensor is on the line, turn left
else if(rightValue == LOW && centerValue == HIGH && leftValue == HIGH)
{
turnLeft();
}
// Case 4: If left and center sensors are on the line, turn slightly right
(correcting)
else if(leftValue == LOW && centerValue == LOW && rightValue == HIGH)
{
slightRight();
}
// Case 5: If right and center sensors are on the line, turn slightly left
(correcting)
else if(rightValue == LOW && centerValue == LOW && leftValue == HIGH)
{
slightLeft();
}
// Case 6: If no sensors detect the black line, keep moving forward
else if(leftValue == HIGH && centerValue == HIGH && rightValue == HIGH)
{
moveForward();
}
// Default case: Stop the robot (optional, rarely triggered)
else
{
stopMotors();
}

delay(10); // Small delay to allow the robot to react smoothly


}

// Function to move forward


void moveForward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

// Function to turn left


void turnLeft()
{
motor1.run(FORWARD); // Left motors stop or slow down
motor2.run(RELEASE); // Right motors move forward
motor3.run(FORWARD);
motor4.run(RELEASE);
}

// Function to turn right


void turnRight()
{
motor1.run(RELEASE); // Right motors stop or slow down
motor2.run(FORWARD); // Left motors move forward
motor3.run(RELEASE);
motor4.run(FORWARD);
}

// Function to make a slight right adjustment


void slightRight()
{
motor1.run(FORWARD); // Left motors move forward slowly
motor2.run(FORWARD);
motor3.run(FORWARD); // Right motors move slower
motor4.run(RELEASE);
}
// Function to make a slight left adjustment
void slightLeft()
{
motor1.run(FORWARD); // Left motors move slower
motor2.run(RELEASE); // Right motors move forward
motor3.run(FORWARD);
motor4.run(FORWARD);
}

// Function to stop all motors


void stopMotors()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}

You might also like