// Forum: https://forum.arduino.cc/t/switch-case-in-a-loop/1235008
// This Wokwi project: https://wokwi.com/projects/392248697514098689
#define PIN_RELAY_IN1 2 // the Arduino pin, which connects to the IN1 pin of relay module
#define PIN_RELAY_IN2 3 // the Arduino pin, which connects to the IN2 pin of relay module
#define PIN_RELAY_IN3 4 // the Arduino pin, which connects to the IN3 pin of relay module
#define PIN_RELAY_IN4 5 // the Arduino pin, which connects to the IN4 pin of relay module
#define PIN_RELAY_IN5 6 // the Arduino pin, which connects to the IN5 pin of relay module
#define PIN_RELAY_IN6 7 // the Arduino pin, which connects to the IN6 pin of relay module
int chronoON = 2500; // this variable is there to define the duration of the pump ON
int chronoOFF = 1000; // this variable is there to define the duration of the pump OFF
char SelectMode; // this variable is there to take the choosen mode
void setup() {
Serial.begin(9600);
pinMode(PIN_RELAY_IN1, OUTPUT);
pinMode(PIN_RELAY_IN2, OUTPUT);
pinMode(PIN_RELAY_IN3, OUTPUT);
pinMode(PIN_RELAY_IN4, OUTPUT);
pinMode(PIN_RELAY_IN5, OUTPUT);
pinMode(PIN_RELAY_IN6, OUTPUT);
delay(2000);
Serial.println("******************************************************");
Serial.println(" PUMPS must be connected KL30.P1.NT1 with KL30.P1.NT1 ");
Serial.println(" and so on up to KL30.P6.NT6 with KL30.P6.NT6. Thanks ");
Serial.println("******************************************************");
Serial.println("Choose your testing Mode");
Serial.println("Type 0 for testing all the Pumps at the same time");
Serial.println("Type 1 for testing 1 Pump one at a time");
Serial.println("Type 2 for testing 2 Pumps at a time one after the other");
Serial.println("Type 3 for testing 3 Pumps at a time one after the other");
Serial.println("Type 4 for testing only 1 pump");
Serial.println("Type 5 for testing only 2 pumps");
Serial.println("Type 6 for testing only 3 pumps");
Serial.println("Type 7 for testing only 4 pumps");
}
// in the loop function, the switch case will run forever
// as long as there's no testing mode changement
void loop() {
if(Serial.available()){
SelectMode = Serial.read();
Serial.print("You typed: ");
Serial.println(SelectMode);
}
switch(SelectMode) {
case '0':
Testing_All_Zusammen();
break;
case '1':
Testing_1_Nacheinander();
break;
case '2':
Testing_2_Nacheinander();
break;
case '3':
Testing_3_Nacheinander();
break;
case '4':
Testing_Only_One();
break;
case '5':
Testing_Only_Two();
break;
case '6':
Testing_Only_Three();
break;
case '7':
Testing_Only_Four();
break;
default:
break;
}
}
// this function will run all the pumps at the same time
// for the value of chronoON in HIGH and chronoOFF in LOW
void Testing_All_Zusammen(){
int count = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
digitalWrite(PIN_RELAY_IN3, HIGH);
digitalWrite(PIN_RELAY_IN4, HIGH);
digitalWrite(PIN_RELAY_IN5, HIGH);
digitalWrite(PIN_RELAY_IN6, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
digitalWrite(PIN_RELAY_IN3, LOW);
digitalWrite(PIN_RELAY_IN4, LOW);
digitalWrite(PIN_RELAY_IN5, LOW);
digitalWrite(PIN_RELAY_IN6, LOW);
delay(chronoOFF);
count++;
Serial.print("PUMPS 1, 2, 3, 4, 5 & 6 had been turned on/off: ");
Serial.print(count);
Serial.println(" times");
}
// this function will run 1 pump after another at a time
void Testing_1_Nacheinander(){
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
count1++;
Serial.print("PUMP 1 had been turned on/off: ");
Serial.print(count1);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN2, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN2, LOW);
count2++;
Serial.print("PUMP 2 had been turned on/off: ");
Serial.print(count2);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN3, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN3, LOW);
count3++;
Serial.print("PUMP 3 had been turned on/off: ");
Serial.print(count3);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN4, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN4, LOW);
count4++;
Serial.print("PUMP 4 had been turned on/off: ");
Serial.print(count4);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN5, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN5, LOW);
count5++;
Serial.print("PUMP 5 had been turned on/off: ");
Serial.print(count5);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN6, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN6, LOW);
count6++;
Serial.print("PUMP 6 had been turned on/off: ");
Serial.print(count6);
Serial.println(" times");
}
// this function will run all 2 pumps at the same time after another
void Testing_2_Nacheinander(){
int count12 = 0, count34 = 0, count56 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
count12++;
Serial.print("PUMPS 1 & 2 had been turned on/off: ");
Serial.print(count12);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN3, HIGH);
digitalWrite(PIN_RELAY_IN4, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN3, LOW);
digitalWrite(PIN_RELAY_IN4, LOW);
count34++;
Serial.print("PUMPS 3 & 4 had been turned on/off: ");
Serial.print(count34);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN5, HIGH);
digitalWrite(PIN_RELAY_IN6, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN5, LOW);
digitalWrite(PIN_RELAY_IN6, LOW);
count56++;
Serial.print("PUMPS 5 & 6 had been turned on/off: ");
Serial.print(count56);
Serial.println(" times");
}
// this function will run all 3 pumps at the same time after another
void Testing_3_Nacheinander(){
int count123 = 0; int count456 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
digitalWrite(PIN_RELAY_IN3, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
digitalWrite(PIN_RELAY_IN3, LOW);
count123++;
Serial.print("PUMPS 1, 2 & 3 had been turned on/off: ");
Serial.print(count123);
Serial.println(" times");
digitalWrite(PIN_RELAY_IN4, HIGH);
digitalWrite(PIN_RELAY_IN5, HIGH);
digitalWrite(PIN_RELAY_IN6, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN4, LOW);
digitalWrite(PIN_RELAY_IN5, LOW);
digitalWrite(PIN_RELAY_IN6, LOW);
count456++;
Serial.print("PUMPS 4, 5 & 6 had been turned on/off: ");
Serial.print(count456);
Serial.println(" times");
}
// this function will run only 1 pump
// for the value of chronoON in HIGH and chronoOFF in LOW
void Testing_Only_One(){
int countO1 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
delay(chronoOFF);
countO1++;
Serial.print("PUMP 1 had been turned on/off: ");
Serial.print(countO1);
Serial.println(" times");
}
// this function will run only 2 pumps at the same time
// for the value of chronoON in HIGH and chronoOFF in LOW
void Testing_Only_Two(){
int countO2 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
delay(chronoOFF);
countO2++;
Serial.print("PUMPS 1 & 2 had been turned on/off: ");
Serial.print(countO2);
Serial.println(" times");
}
// this function will run only 3 pumps at the same time
// for the value of chronoON in HIGH and chronoOFF in LOW
void Testing_Only_Three(){
int countO3 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
digitalWrite(PIN_RELAY_IN3, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
digitalWrite(PIN_RELAY_IN3, LOW);
delay(chronoOFF);
countO3++;
Serial.print("PUMPS 1, 2 & 3 had been turned on/off: ");
Serial.print(countO3);
Serial.println(" times");
}
// this function will run only 4 pumps at the same time
// for the value of chronoON in HIGH and chronoOFF in LOW
void Testing_Only_Four(){
int countO4 = 0;
digitalWrite(PIN_RELAY_IN1, HIGH);
digitalWrite(PIN_RELAY_IN2, HIGH);
digitalWrite(PIN_RELAY_IN3, HIGH);
digitalWrite(PIN_RELAY_IN4, HIGH);
delay(chronoON);
digitalWrite(PIN_RELAY_IN1, LOW);
digitalWrite(PIN_RELAY_IN2, LOW);
digitalWrite(PIN_RELAY_IN3, LOW);
digitalWrite(PIN_RELAY_IN4, LOW);
delay(chronoOFF);
countO4++;
Serial.print("PUMPS 1, 2, 3 & 4 had been turned on/off: ");
Serial.print(countO4);
Serial.println(" times");
}