#include <Stepper.h>
#define STEPS 200
// Stepper motors setup
Stepper leftMotor(STEPS, 8, 9, 10, 11);
Stepper rightMotor(STEPS, 4, 5, 6, 7);
// Light sensors
#define LEFT_SENSOR A0
#define RIGHT_SENSOR A1
// Threshold for black line detection
int thresholdValue = 450;
void setup() {
Serial.begin(9600);
leftMotor.setSpeed(50);
rightMotor.setSpeed(50);
Serial.println("Line Following Robot Ready!");
}
void loop() {
int leftValue = analogRead(LEFT_SENSOR);
int rightValue = analogRead(RIGHT_SENSOR);
Serial.print("Left: "); Serial.print(leftValue);
Serial.print(" | Right: "); Serial.println(rightValue);
bool leftBlack = leftValue < thresholdValue;
bool rightBlack = rightValue < thresholdValue;
if (leftBlack && rightBlack) {
moveForward();
}
else if (!leftBlack && !rightBlack) {
stopMotors();
}
else if (leftBlack && !rightBlack) {
turnLeft();
}
else if (!leftBlack && rightBlack) {
turnRight();
}
delay(100);
}
void moveForward() {
leftMotor.step(10);
rightMotor.step(10);
}
void stopMotors() {
}
void turnLeft() {
leftMotor.step(0);
rightMotor.step(5);
}
void turnRight() {
leftMotor.step(5);
rightMotor.step(0);
}