const int sensorPin = 12; // TCRT5000 digital output
const int relayPin = 3; // Relay control pin
int counter = 0; // Counts down after last detection
const int countMax = 1000; // Adjust to control how long relay stays ON (1000 ~ 1 second)
void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // relay OFF initially
Serial.begin(9600);
counter = 0;
}
void loop() {
// Sensor detects white patch (active LOW on many modules)
if (digitalRead(sensorPin) == LOW) {
counter = countMax; // Reset counter whenever white patch detected
digitalWrite(relayPin, HIGH); // Relay ON
Serial.println("Wheel moving → Relay ON");
} else {
// Countdown: relay stays ON for a short time even if sensor reads black
if (counter > 0) {
counter--;
digitalWrite(relayPin, HIGH); // Keep relay ON
} else {
digitalWrite(relayPin, LOW); // Relay OFF
Serial.println("Wheel stopped → Relay OFF");
}
}
delay(1); // Small delay for stability
}