My Arduino-based line-tracer outputs its motor speed so low that the wheels don’t turn on their own—I have to push it from behind for it to start moving. However, if I raise the speed, it drives fine but then struggles to detect the line accurately. Is there a way to run it more slowly so that it still recognizes the line well, or to improve line detection with the HW-870 sensor at higher speeds? I’d prefer the robot to operate at a reduced speed for better line tracking
// Sensor pin definitions
const int leftSensorPin = A0;
const int rightSensorPin = A1;
// Motor control pin definitions
const int leftMotorForward = 3;
const int leftMotorBackward = 11;
const int rightMotorForward = 5;
const int rightMotorBackward= 6;
// Speed settings
const int motorSpeed = 60; // Base forward speed
const int turnSpeed = 60; // Turning speed
// Black line encounter counter and previous state
int blackCount = 0;
bool prevBothOn = false;
void setup() {
// Configure sensor input pins
pinMode(leftSensorPin, INPUT);
pinMode(rightSensorPin, INPUT);
// Configure motor output pins
pinMode(leftMotorForward, OUTPUT);
pinMode(leftMotorBackward, OUTPUT);
pinMode(rightMotorForward, OUTPUT);
pinMode(rightMotorBackward, OUTPUT);
// Ensure motors are stopped initially
stopMotors();
}
void loop() {
int leftSensor = analogRead(leftSensorPin);
int rightSensor = analogRead(rightSensorPin);
bool leftOnLine = leftSensor > 400;
bool rightOnLine = rightSensor > 400;
bool bothOn = leftOnLine && rightOnLine;
if (bothOn && !prevBothOn) {
// Edge: both sensors detect the line simultaneously
blackCount++;
stopMotors();
if (blackCount >= 6) {
delay(10000); // 6th encounter: stop for 10 seconds
blackCount = 0;
} else {
delay(1000); // 1st–5th encounter: stop for 1 second
}
// After stopping, resume forward movement immediately
moveForward();
}
else if (!leftOnLine && !rightOnLine) {
// No line detected: move forward
moveForward();
}
else if (leftOnLine && !rightOnLine) {
// Left sensor only: turn left
turnLeft();
}
else if (!leftOnLine && rightOnLine) {
// Right sensor only: turn right
turnRight();
}
else {
// Otherwise: maintain forward motion
moveForward();
}
prevBothOn = bothOn;
delay(50);
}
// === Motor control functions ===
// Move forward at base speed
void moveForward() {
analogWrite(leftMotorForward, motorSpeed);
analogWrite(leftMotorBackward, 0);
analogWrite(rightMotorForward, motorSpeed);
analogWrite(rightMotorBackward,0);
}
// Turn left (left wheel stopped, right wheel forward)
void turnLeft() {
analogWrite(leftMotorForward, 0);
analogWrite(leftMotorBackward, 0);
analogWrite(rightMotorForward, turnSpeed);
analogWrite(rightMotorBackward,0);
}
// Turn right (right wheel stopped, left wheel forward)
void turnRight() {
analogWrite(leftMotorForward, turnSpeed);
analogWrite(leftMotorBackward, 0);
analogWrite(rightMotorForward, 0);
analogWrite(rightMotorBackward,0);
}
// Stop all motors
void stopMotors() {
analogWrite(leftMotorForward, 0);
analogWrite(leftMotorBackward, 0);
analogWrite(rightMotorForward, 0);
analogWrite(rightMotorBackward,0);
}
I tried configuring
TCCR0B = TCCR1B & 0b11111000 | 0x01;
TCCR2B = TCCR2B & 0b11111000 | 0x01;
but it made no difference.