I'm working on a project to control multiple ham radio antennas. What I have is 4 buttons controlling 4 relays.
The rules are that only 1 relay can be active at a time. Each button turns on it's respective relay and turns off the previously active relay (per rule 1) and each relay stays active until another button is pressed.
The code I have written initiates relay 1 but will not allow me to change to another relay. Buttons carry 5v and drop to 0 when pressed (confirmed with multimeter) but make no change when pressed.
I have attempted to rewrite this code with if...then and/or break statements, and changing the button status from inputs to outputs (pullup and not) with no progress.
It seems like the code to hold the previous button press is also keeping the rest of the code from running.
Where am I going wrong? Thanks in advance.
#include <liquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int relay1 = 12;
int relay2 = 11;
int relay3 = 10;
int relay4 = 9;
int button1 = 7;
int button2 = 6;
int button3 = 5;
int button4 = 4;
byte oldSwitchState = HIGH;
bool toggle;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Remote");
lcd.setCursor(0,1);
lcd.print("Antenna Switch");
delay(4000);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH); }
void loop() {
if (digitalRead(button1 = LOW)) ;{
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("DIPOLE");
lcd.setCursor(3, 1);
lcd.print("IS ACTIVE");
{digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH); }}
byte switchState = digitalRead (button1);
if (switchState != oldSwitchState) { oldSwitchState = switchState;
toggle = !toggle;
delay (100); // debounce }
if (digitalRead(button2 = LOW)) ;
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("15M YAGI");
lcd.setCursor(3, 1);
lcd.print("IS ACTIVE");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
byte switchState = digitalRead (button2);
if (switchState != oldSwitchState) { oldSwitchState = switchState;
toggle = !toggle;
delay (100); // debounce }
if(digitalRead(button3 == LOW))
{ lcd.clear();
lcd.setCursor(3, 0);
lcd.print("10M VERTICAL");
lcd.setCursor(3, 1);
lcd.print("IS ACTIVE");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
digitalWrite(relay4, HIGH); }
byte switchState = digitalRead (button3);
if (switchState != oldSwitchState) { oldSwitchState = switchState; //
toggle = !toggle; //
delay (100); // debounce }
if(digitalRead(button4 == LOW))
{ lcd.clear();
lcd.setCursor(4, 0);
lcd.print("6M YAGI");
lcd.setCursor(3, 1);
lcd.print("IS ACTIVE");
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, LOW);
byte switchState = digitalRead (button4);
if (switchState != oldSwitchState) { oldSwitchState = switchState;
toggle = !toggle;
delay (100); // debounce }
}}}}}}