#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the pin numbers for the relays
const int relayPins[6] = {2, 3, 4, 5, 6, 7}; // Relay pins (adjust these according to your wiring)
// Define the pin numbers for the push buttons
const int buttonPins[6] = {8, 9, 10, 11, 12, 13}; // Button pins (adjust these according to your wiring)
// Create an LCD object with I2C address (0x3F or 0x27 based on your LCD)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the address if needed (common ones: 0x27 or 0x3F)
bool lastButtonState[6] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; // Last state of each button (initially HIGH, as using pull-ups)
bool relayState[6] = {LOW, LOW, LOW, LOW, LOW, LOW}; // Current state of each relay (off initially)
unsigned long lastDebounceTime[6] = {0, 0, 0, 0, 0, 0}; // To handle debouncing
unsigned long debounceDelay = 50; // Debounce delay time in milliseconds
int relayIndex = 0; // To track the current relay (0 to 5)
void setup() {
// Initialize relay pins as OUTPUT
for (int i = 0; i < 6; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Ensure relays are initially OFF
}
// Initialize button pins as INPUT with internal pull-up resistors
for (int i = 0; i < 6; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Using internal pull-up resistors
}
// Initialize the LCD
lcd.begin(20, 4);
lcd.backlight(); // Turn on the backlight
lcd.print("MIC & AMP Controller");
lcd.setCursor(0, 1); // Second line (row 2)
lcd.print(" Product of:");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Fixmation (Pvt) Ltd.");
lcd.setCursor(0, 3); // Fourth line (row 2)
lcd.print("Press Any AMP Button");
delay(3000); // Display message for 2 seconds
}
void loop() {
// Check the button states and call the corresponding functions
if (checkButtonState(0)) {
Amp1();
} else if (checkButtonState(1)) {
Amp2();
} else if (checkButtonState(2)) {
Amp3();
} else if (checkButtonState(3)) {
turnOffAll();
} else if (checkButtonState(4)) {
toggleIndividually();
} else if (checkButtonState(5)) {
toggleOnOff();
}
}
// Check if a button has been pressed with debouncing
bool checkButtonState(int buttonIndex) {
int reading = digitalRead(buttonPins[buttonIndex]); // Read the button state
// Check if the button state has changed (debouncing)
if (reading != lastButtonState[buttonIndex]) {
lastDebounceTime[buttonIndex] = millis(); // Reset debounce timer
}
// Only act if debounce time has passed
if ((millis() - lastDebounceTime[buttonIndex]) > debounceDelay) {
if (reading == LOW) { // If button is pressed (active LOW)
lastButtonState[buttonIndex] = reading; // Save the current state
return true;
}
}
lastButtonState[buttonIndex] = reading;
return false;
}
void Amp1() {
// Set relay 1 and 4 ON, all others OFF
relayState[0] = HIGH; // Relay 1 ON
relayState[3] = HIGH; // Relay 4 ON
relayState[1] = LOW; // Relay 2 OFF
relayState[2] = LOW; // Relay 3 OFF
relayState[4] = LOW; // Relay 5 OFF
relayState[5] = LOW; // Relay 6 OFF
updateRelays();
displayStatus("AMP 1 is Active");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Reception Floor");
}
void Amp2() {
// Set relay 2 and 5 ON, all others OFF
relayState[1] = HIGH; // Relay 2 ON
relayState[4] = HIGH; // Relay 5 ON
relayState[0] = LOW; // Relay 1 OFF
relayState[2] = LOW; // Relay 3 OFF
relayState[3] = LOW; // Relay 4 OFF
relayState[5] = LOW; // Relay 6 OFF
updateRelays();
displayStatus("AMP 2 is Active");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Ladies Floor");
}
void Amp3() {
// Set relay 3 and 6 ON, all others OFF
relayState[2] = HIGH; // Relay 3 ON
relayState[5] = HIGH; // Relay 6 ON
relayState[0] = LOW; // Relay 1 OFF
relayState[1] = LOW; // Relay 2 OFF
relayState[3] = LOW; // Relay 4 OFF
relayState[4] = LOW; // Relay 5 OFF
updateRelays();
displayStatus("AMP 3 is Active");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Gents Floor");
}
void turnOffAll() {
// Turn off all relays
for (int i = 0; i < 6; i++) {
relayState[i] = LOW;
}
updateRelays();
displayStatus("All Lines are OFF");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("In Every Floors");
}
void toggleIndividually() {
// Toggle each relay individually
for (int i = 0; i < 6; i++) {
relayState[i] = !relayState[i]; // Toggle relay state
}
updateRelays();
displayStatus("AMPs are Toggled");
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Function is Inverse");
}
void toggleOnOff() {
// Switch ON/OFF relays one by one
if (relayState[relayIndex] == LOW) {
relayState[relayIndex] = HIGH; // Turn the current relay ON
} else {
relayState[relayIndex] = LOW; // Turn the current relay OFF
}
// Update relays and show status
updateRelays();
displayStatus("Line " + String(relayIndex + 1) + " is" + (relayState[relayIndex] ? " ON" : " OFF"));
lcd.setCursor(0, 2); // Third line (row 2)
lcd.print("Switching One by One");
// Move to the next relay
relayIndex = (relayIndex + 1) % 6; // Cycle through relay indices (0 to 5)
}
void updateRelays() {
// Update the relay pins based on the relayState array
for (int i = 0; i < 6; i++) {
digitalWrite(relayPins[i], relayState[i] ? HIGH : LOW);
}
}
void displayStatus(String status) {
// Display the status message on the LCD
lcd.clear();
lcd.print("MIC & AMP Controller");
lcd.setCursor(0, 1);
lcd.print(status);
delay(1000); // Display status for a moment
}
Amp 1
Amp 2
Amp 3
All Off
Toggle Amps
ON/OFF Sequence
Relay 1
Relay 2
Relay 3
Relay 4
Relay 5
Relay 6