#include <Keypad.h>
#include <EEPROM.h>
#include "HD44780_LCD_PCF8574.h"
// I2C LCD at address 0x27, 16 columns x 2 rows
HD44780LCD lcd(2, 16, 0x27, &Wire);
// Relay configuration
const int relayPins[] = {3, 4, 5, 6, 7, 8, 9, 10}; // Relay pins connected to digital pins
int activeRelay = -1; // Relay currently selected by keypad
int pttRelay = -1; // Relay activated when PTT is HIGH
const int pttPin = 2; // Pin for PTT control (HIGH activates pttRelay)
const int EEPROM_ACTIVE_RELAY_ADDR = 0; // EEPROM address to store activeRelay
const int EEPROM_PTT_RELAY_ADDR = 1; // EEPROM address to store pttRelay
bool pttSelectMode = false; // Flag for PTT selection mode
// Keypad configuration
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, 13}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, 11, 12}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize LCD without cursor
lcd.PCF8574_LCDInit(lcd.LCDCursorTypeOff);
lcd.PCF8574_LCDClearScreen();
lcd.PCF8574_LCDBackLightSet(true);
lcd.PCF8574_LCDGOTO(lcd.LCDLineNumberOne, 0);
lcd.PCF8574_LCDSendString("<Select Antenna>");
delay(2000);
// Set relay pins as output and turn them off
for (int i = 0; i < 8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW);
}
pinMode(pttPin, INPUT); // Set PTT pin as input
// Load settings from EEPROM
activeRelay = EEPROM.read(EEPROM_ACTIVE_RELAY_ADDR);
pttRelay = EEPROM.read(EEPROM_PTT_RELAY_ADDR);
if (activeRelay >= 0 && activeRelay <= 7) {
activateRelay(activeRelay);
lcdPrint(String("Antenna ") + (activeRelay + 1) + " On");
} else {
activeRelay = -1; // No saved relay is active
lcdPrint("All Antennas Off");
}
}
void loop() {
// Check if PTT pin is HIGH or LOW
if (digitalRead(pttPin) == HIGH && pttRelay != -1) {
// PTT mode is on, activate the PTT antenna
activateRelay(pttRelay);
lcdPrint(String("TRANSMIT BY: ") + (pttRelay + 1));
} else if (digitalRead(pttPin) == LOW) {
// PTT mode is off, return to last keypad-selected antenna
if (activeRelay != -1) {
activateRelay(activeRelay);
lcdPrint(String("Antenna ") + (activeRelay + 1) + " On");
} else {
turnOffAllRelays();
lcdPrint("All Antennas Off");
}
}
// Check keypad input
char key = keypad.getKey();
if (key) {
if (pttSelectMode) {
waitForPttSelection(); // Enter PTT selection mode
} else {
// Normal antenna selection mode
if (key >= '1' && key <= '8') {
int relayIndex = key - '1'; // Convert '1' to 0, ..., '8' to 7
activeRelay = relayIndex; // Update activeRelay to store the last selection
if (EEPROM.read(EEPROM_ACTIVE_RELAY_ADDR) != activeRelay) {
EEPROM.write(EEPROM_ACTIVE_RELAY_ADDR, activeRelay); // Save to EEPROM only if different
}
activateRelay(activeRelay);
lcdPrint(String("Antenna ") + (relayIndex + 1) + " On");
} else if (key == '0') {
activeRelay = -1; // Reset active relay variable
if (EEPROM.read(EEPROM_ACTIVE_RELAY_ADDR) != activeRelay) {
EEPROM.write(EEPROM_ACTIVE_RELAY_ADDR, activeRelay); // Save only if changed
}
turnOffAllRelays(); // Turn off all relays
lcdPrint("All Antennas Off");
} else if (key == '*') {
// Enter PTT antenna selection mode
pttSelectMode = true;
lcdPrint("Select PTT Ant:");
waitForPttSelection(); // Start waiting for PTT antenna selection
}
}
}
}
void waitForPttSelection() {
int tempPttRelay = -1; // Temporary variable to hold the selected antenna number
while (true) {
char pttKey = keypad.getKey();
if (pttKey) {
if (pttKey >= '1' && pttKey <= '8') {
// Set the temporary relay selection and display confirmation prompt
tempPttRelay = pttKey - '1';
lcdPrint(String("Set PTT Ant. ") + (tempPttRelay + 1) + ", Press # to Confirm");
} else if (pttKey == '0') {
// Option to clear the PTT antenna
lcdPrint("Clear PTT Antenna, Press # to Confirm");
tempPttRelay = -1; // Indicate clearing the PTT relay
} else if (pttKey == '#') {
// Confirm the PTT relay selection or clearing
pttRelay = tempPttRelay;
if (EEPROM.read(EEPROM_PTT_RELAY_ADDR) != pttRelay) {
EEPROM.write(EEPROM_PTT_RELAY_ADDR, pttRelay); // Save only if changed
}
if (pttRelay == -1) {
lcdPrint("PTT Antenna Cleared");
} else {
lcdPrint(String("PTT Antenna ") + (pttRelay + 1) + " Saved");
}
pttSelectMode = false; // Exit PTT selection mode
break;
} else {
lcdPrint("Invalid Input, Press 1-8 or 0 to Clear");
}
}
}
}
void lcdPrint(const String &text) {
lcd.PCF8574_LCDClearScreen();
lcd.PCF8574_LCDGOTO(lcd.LCDLineNumberOne, 0);
lcd.PCF8574_LCDSendString(" 4L7ZS ");
lcd.PCF8574_LCDGOTO(lcd.LCDLineNumberTwo, 0);
lcd.PCF8574_LCDSendString(text.c_str());
}
void activateRelay(int relayIndex) {
turnOffAllRelays(); // Ensure only one relay is active
digitalWrite(relayPins[relayIndex], HIGH);
}
void turnOffAllRelays() {
for (int i = 0; i < 8; i++) {
digitalWrite(relayPins[i], LOW);
}
}