Hello all, This is my first ever project and i'm still very new to programming. I have a basic setup of 4 buttons, 4 relays and 4 LED's. When button 1 is press it activates relay1 and LED 1. Pressing button 2 would deactivate relay 1 and LED 1 and activate relay 2 and LED 2 etc.
The code below is doing just that, but now I would like to introduce a function that after a time delay the LED (whichever is on) would switch off. For testing purposes this can be set to 5 sec but for the project i would like 10 mins.
This is completely out of my scope and would like some help, please.
#define LED_PIN1 6
#define LED_PIN2 7
#define LED_PIN3 12
int relay1 = 11; //Amp CH1 relay
int relay2 = 10; //Amp CH2 relay
int relay3 = 9; //Amp CH3 relay
int relay4 = 8; //Amp CH4 relay
int button1 = 2; //CH1 Button
int button2 = 3; //CH2 Button
int button3 = 4; //CH3 Button
int button4 = 5; //CH4 Button
void setup()
{
// Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Initialize digital inputs with internal pullup resistors:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
//Set the initial startup to OFF:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
// Initialize LED output pin:
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
}
void loop()
{
//Button 1 Enable
bool button1State ;
button1State = digitalRead(button1);
if(button1State == LOW)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN3, LOW);
}
//Button 2 Enable
bool button2State ;
button2State = digitalRead(button2);
if(button2State == LOW)
{
digitalWrite(relay2, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, HIGH);
digitalWrite(LED_PIN3, LOW);
}
//Button 3 Enable
bool button3State ;
button3State = digitalRead(button3);
if(button3State == LOW)
{
digitalWrite(relay3, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN3, HIGH);
}
//Button 4 Enable
bool button4State ;
button4State = digitalRead(button4);
if(button4State == LOW)
{
digitalWrite(relay4, HIGH);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN3, LOW);
}
}
I know this is probably written poorly and could be done with a few lines of coding, so after the problem has been solved, is there a better way of writing the above code?
Thank you for your help.