#include <AFMotor.h>
AF_DCMotor Motor1(1); // Motor on M1
const int irPin = A2; // Analog pin
int threshold = 400; // Between white (32) and black (769)
void setup() {
Serial.begin(9600);
Motor1.setSpeed(200);
Motor1.run(RELEASE);
}
void loop() {
int irValue = analogRead(irPin);
Serial.print(“IR Value: “);
Serial.println(irValue);
// Your sensor:
// WHITE = low value (31)
// BLACK = high value (769)
if (irValue > threshold) {
// BLACK detected → run motor
Motor1.run(FORWARD);
} else {
// WHITE or air → stop motor
Motor1.run(RELEASE);
}
delay(10);
}
