Is there a way to clean up this code...or do some smart coding?
In this example I use 6 relays, in the final code it will be 10.
Also I now use 5 switches and that will be 8, each with an own led. Now I only stated 1 led.
The code reads a button, and a few relays will get a signal. Also the buttonled will go on. I will add more cases later. But I was wondering, is it possible/easier/just another way of writing the high/low to the relays and leds.
Can I somehow combine the LOWs or HIGHs within a case?
int relay1 = A5;
int relay2 = A4;
int relay3 = A3;
int relay4 = A2;
int relay5 = A1;
int relay6 = A0;
int led1 = 3;
int sw1 = 5;
int sw2 = 6;
int sw3 = 7;
int sw4 = 8;
int sw5 = 9;
int sw1Status, sw2Status, sw3Status, sw4Status, sw5Status;
int order = 1; // default is to set all relays off.
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
// input pull-up, so default state of buttons is HIGH.
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
pinMode(sw4, INPUT_PULLUP);
pinMode(sw5, INPUT_PULLUP);
}
void loop() {
// read the buttons status
sw1Status = digitalRead(sw1);
sw2Status = digitalRead(sw2);
sw3Status = digitalRead(sw3);
sw4Status = digitalRead(sw4);
sw5Status = digitalRead(sw5);
if (sw1Status == LOW) { // if button 1 pressed
order = 2; // go to case 2
}
if (sw2Status == LOW) {
order = 3;
}
if (sw3Status == LOW) {
order = 4;
}
if (sw4Status == LOW) {
order = 5;
}
if (sw5Status == LOW) { // button 4 toggle
if (order != 7) { // if order is not equal 5 turn on relay 1 & 4
order = 6;
}
else { // else go to case 1, which will turns off all relays
order = 1;
}
}
switch (order) {
case 1: // if order equals 1
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(relay5, LOW);
digitalWrite(relay6, LOW);
digitalWrite(led1, LOW);
break;
case 2: // if order equals 2
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, LOW);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, LOW);
digitalWrite(led1, HIGH);
break;
case 3: // if order equals 3
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, LOW);
digitalWrite(relay6, HIGH);
digitalWrite(led1, LOW);
break;
case 4: // if order equals 4
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, LOW);
digitalWrite(relay5, LOW);
digitalWrite(relay6, LOW);
digitalWrite(led1, LOW);
break;
case 5: // if order equals 5
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(led1, LOW);
break;
case 6: // if order equals 5
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, LOW);
digitalWrite(relay6, LOW);
digitalWrite(led1, LOW);
break;
default: // if none of above cases is valid go to case 1
order = 1;
break;
}
}
case
relay values