const int IR_PIN = 2; // Pin connected to the proximity sensor
const int relay1 = 6; // Pin connected to the relay module 1
const int relay2 = 7; // Pin connected to the relay module 2
const int button1 = 3;
const int button2 = 4;
unsigned long lastTime1;
unsigned long lastTime2;
int relayDuration = 200;
unsigned long timer;
int relay1On = 0;
int relay2On = 0;
int relay2Toggle = 0;
void setup() {
pinMode(IR_PIN, INPUT_PULLUP);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW); // Ensure relay1 is off
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW); // Ensure relay2 is off
timer = millis();
}
void loop() {
if (millis() - timer > 2500){
if (digitalRead(IR_PIN) == LOW && relay2Toggle == 0) {
digitalWrite(relay1, HIGH);
lastTime1 = millis();
relay1On = 1;
relay2Toggle = 1;
timer = millis();
} else if (digitalRead(IR_PIN) == LOW && relay2Toggle == 1) {
digitalWrite(relay2, HIGH);
lastTime2 = millis();
relay2On = 1;
relay2Toggle = 0;
timer = millis();
}
}
if (digitalRead(button1) == LOW) {
digitalWrite(relay1, HIGH);
lastTime1 = millis();
relay1On = 1;
relay2Toggle = 1;
timer = millis();
}
if (digitalRead(button2) == LOW) {
digitalWrite(relay2, HIGH);
lastTime2 = millis();
relay2On = 1;
relay2Toggle = 0;
timer = millis();
}
if (relay1On == 1 && digitalRead(button1) == HIGH) {
if (millis() - lastTime1 > relayDuration) {
digitalWrite(relay1, LOW);
relay1On = 0;
}
}
if (relay2On == 1 && digitalRead(button2) == HIGH) {
if (millis() - lastTime2 > relayDuration) {
digitalWrite(relay2, LOW);
relay2On = 0;
}
}
}