// https://forum.arduino.cc/t/urgent-help-with-this-project-please-urgent/1408628/
// sensor activity adds (timeout) to enable relay
// more than (timeout) inactivity disables relay
byte pinSensor = 12;
byte pinRelay = 3, pinBlu = 4, pinGrn = 5, pinRed = 6;
unsigned long timer, timeout = 1000, warning = 250;
void setup() {
pinMode(pinSensor, INPUT_PULLUP);
for (byte i = 0; i < 4; i++)
pinMode(i + 3, OUTPUT);
digitalWrite(pinRelay, LOW); // disable relay
timer = millis() + timeout; // set new timer
}
void loop() {
if (digitalRead(pinSensor) == 1) {
if (timer - millis() > timeout) { // calculate duration
digitalWrite(pinRelay, LOW); // disable relay
indicate(HIGH, LOW, LOW); // red
}
}
if (digitalRead(pinSensor) == 0) { // sensor active
digitalWrite(pinRelay, HIGH); // enable relay
timer = millis() + timeout; // set new timer
indicate(LOW, HIGH, LOW); // grn
}
if (timer - millis() < warning) { // time ending
indicate(LOW, LOW, HIGH); // blu
}
}
void indicate (byte red, byte grn, byte blu) {
digitalWrite(pinRed, red);
digitalWrite(pinGrn, grn);
digitalWrite(pinBlu, blu);
}SENSOR