Combine sensor thresholds and motor decisions for line following robots.
Open in compiler →/*
Roboda verified Arduino sample 0950
Feature: Line Follow Logic
Generated for online build, classroom practice, and hardware upload testing.
*/
const byte leftSensor = A0;
const byte rightSensor = A1;
const byte leftMotor = 5;
const byte rightMotor = 6;
const int lineThreshold = 610;
void setup() {
pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
}
void loop() {
int left = analogRead(leftSensor);
int right = analogRead(rightSensor);
analogWrite(leftMotor, left > lineThreshold ? 120 : 210);
analogWrite(rightMotor, right > lineThreshold ? 120 : 210);
delay(20);
}
How it works
Use two analog sensors to make basic line-following motor decisions.
- Wire left and right sensors to A0 and A1.
- Wire motor driver speed inputs to D5 and D6.
- Tune lineThreshold for the track and sensor height.
- Reduce one side speed when that sensor sees the line.
Connections
| Pin | Connect to | Note |
|---|---|---|
| A0, A1 | IR reflectance sensor outputs | Tune lineThreshold for your surface. |
| D5, D6 | Motor driver speed inputs | Use separate motor power and common GND. |