const int PirPin = 19; // Switch 1 GPIO pin
bool pirState = LOW; // we start, assuming no motion detected
int pirval = 0; // variable for reading the pin status
const int buttonPin1 = 2; // Switch 1 GPIO pin
const int buttonPin2 = 3; // Switch 2 GPIO pin
const int buttonPin3 = 4; // Switch 3 GPIO pin
const int relayPin1 = 6; // Relay 1 control GPIO pin
const int relayPin2 = 7; // Relay 2 control GPIO pin
const int relayPin3 = 8; // Relay 3 control GPIO pin
bool relayState1 = LOW; // Initial state for Relay 1 (OFF)
bool relayState2 = LOW; // Initial state for Relay 2 (OFF)
bool relayState3 = LOW; // Initial state for Relay 3 (OFF)
bool buttonState1 = HIGH;
bool buttonState2 = HIGH;
bool buttonState3 = HIGH;
bool lastButtonState1 = HIGH;
bool lastButtonState2 = HIGH;
bool lastButtonState3 = HIGH;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(PirPin, INPUT); // declare Pir sensor as input
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
digitalWrite(relayPin1, relayState1);
digitalWrite(relayPin2, relayState2);
digitalWrite(relayPin3, relayState3);
Serial.begin(115200);
Serial.println("3 Switches and 3 Relays Example");
}
void loop() {
int pirReading = digitalRead(PirPin);
if (pirReading == HIGH) {
// PIR sensor detected motion
if (relayState1 == LOW) {
relayState1 = HIGH; // Turn on relay1
digitalWrite(relayPin1, relayState1);
Serial.println("Motion Detected - Relay 1 ON");
}
} else {
if (relayState1 == HIGH) {
relayState1 = LOW; // Turn off relay1
digitalWrite(relayPin1, relayState1);
Serial.println("No Motion - Relay 1 OFF");
}
}
int buttonReading1 = digitalRead(buttonPin1);
int buttonReading2 = digitalRead(buttonPin2);
int buttonReading3 = digitalRead(buttonPin3);
// Button 1 debouncing
if (buttonReading1 != lastButtonState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (buttonReading1 != buttonState1) {
buttonState1 = buttonReading1;
if (buttonReading1 == LOW) {
relayState1 = !relayState1;
digitalWrite(relayPin1, relayState1);
if (relayState1 == HIGH) {
Serial.println("Switch 1 Pressed - Relay 1 ON");
} else {
Serial.println("Switch 1 Pressed - Relay 1 OFF");
}
}
}
}
lastButtonState1 = buttonReading1;
// Button 2 debouncing
if (buttonReading2 != lastButtonState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (buttonReading2 != buttonState2) {
buttonState2 = buttonReading2;
if (buttonReading2 == LOW) {
relayState2 = !relayState2;
digitalWrite(relayPin2, relayState2);
if (relayState2 == HIGH) {
Serial.println("Switch 2 Pressed - Relay 2 ON");
} else {
Serial.println("Switch 2 Pressed - Relay 2 OFF");
}
}
}
}
lastButtonState2 = buttonReading2;
// Button 3 debouncing
if (buttonReading3 != lastButtonState3) {
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
if (buttonReading3 != buttonState3) {
buttonState3 = buttonReading3;
if (buttonReading3 == LOW) {
relayState3 = !relayState3;
digitalWrite(relayPin3, relayState3);
if (relayState3 == HIGH) {
Serial.println("Switch 3 Pressed - Relay 3 ON");
} else {
Serial.println("Switch 3 Pressed - Relay 3 OFF");
}
}
}
}
lastButtonState3 = buttonReading3;
// You can add other code here
// Optional: Add a delay or other code as needed.
}