SWITCH CASE in a LOOP

Hi everyone,
I hope you all are doing great. Here is a sketch I've written for testing electric waterpump. I want the selected testing mode to be executed as long as there is no change in the SelectMode value. It doesn't works as I want because the selected testing mode runs only one time and then wait for the user to chose again a testing mode. Can someone please help me to solve this loop problem.

Thanks to you all

#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");
}

If you type a number in the Serial Monitor, then it is followed by Carriage Return or LineFeed or both or none or in reverse order.

Check what you receive:

void loop()
{
  // Only select a new mode if the received character is valid.
  // Ignore all the Carriage Return and LineFeeds.
  if (Serial.available() > 0)
  {
    int inChar = Serial.read();
    if( inChar >= '0' and inChar <= '7')
    {
      SelectMode = (char) inChar;
    }
  }
}

Your project in Wokwi simulation:

1 Like

or set your Serial monitor Line Endings to None

I like to add some feedback

    default:
      Serial.print(SelectMode);
      Serial.println(" is impossible. WTF?");  // Where's The Flaw?
      break;

to any switch/case statement.

a7

Hi Koepel,
Thank you very much, your solution is working.

Hi Koepel,
I've a question regarding my sketch.

The sketch is doing very very well a I want but on the serial monitor I've this.

this is how it should lookand i how it actually look:

11:11:59.513 -> Pump 1 had been turned on/off: 1 times
11:12:09.503 -> Pump 2 had been turned on/off: 1 times
11:12:19.496 -> Pump 3 had been turned on/off: 1 times
11:12:29.519 -> Pump 1 had been turned on/off: 2 times
11:12:29.559 -> Pump 2 had been turned on/off: 2 times
11:12:29.585 -> Pump 3 had been turned on/off: 2 times
11:12:39.608 -> Pump 1 had been turned on/off: 3 times
11:12:39.649 -> Pump 2 had been turned on/off: 3 times
11:12:49.666 -> Pump 3 had been turned on/off: 3 times
11:12:59.653 -> Pump 1 had been turned on/off: 4 times
11:13:09.646 -> Pump 2 had been turned on/off: 4 times
11:13:19.674 -> Pump 3 had been turned on/off: 4 times

but sometimes I've this:

11:13:49.669 -> mp 3 had been turned on/off: 5 times
11:14:19.674 -> mp 3 had been turned on/off: 6 times
11:26:20.368 -> p 1 had been turned on/off: 35 times

or this

11:18:49.917 -> Pump 2 had been turned mp 3 had been turned on/off: 17 times

what could be the problem

Then we have to know which Arduino board that you use, with a link to where you bought it or a photo, which operating system you have, which driver is installed. And we have to see the full sketch.

It could be caused by a missing "Serial.begin(9600)", or the baudrate is too high, for example "Serial.begin(2000000);"

My best guess is that you have an Arduino Uno or Nano and use far too much SRAM or there is a bug with an array.
Do you know the F() macro ? Scroll down to the bottom of this page: https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

You're rigth, I'm using an Arduino Uno on windows and I can't find any bug with the array

here is the sketch

#include <Arduino.h>

// Define LED pin connections
const int PumpPins[] = {2, 3, 4, 5, 6, 7};
const int AnzahlPumpen = 6;
int chronoON = 10000;
int chronoOFF = 3500;

// Define operation modes
enum Mode { ALL_ZUSAMMEN, NACHEINANDER_01, NACHEINANDER_02, NACHEINANDER_03 };
const int numModes = 4;

// Declare functions
void AllePumpenEinschalten(int AnzahlPumpen);
void EinePumpeNacheinanderEinschalten(int AnzahlPumpen);
void ZweiPumpenNacheinanderEinschalten(int AnzahlPumpen);
void DreiPumpenNacheinanderEinschalten(int AnzahlPumpen);
int ModeBeantragen(int AnzahlPumpen);
void ModeAnzeigen();

// Array of function pointers for mode selection
void (*modeFunctions[])(int) = {AllePumpenEinschalten, EinePumpeNacheinanderEinschalten, ZweiPumpenNacheinanderEinschalten, DreiPumpenNacheinanderEinschalten};
const char* ModeBeschreibung[] = {"-- ALLE PUMPEN GLEICHZEITICH TESTEN --", "-- EINE PUMPE NACHEINANDER TESTEN --", "-- ZWEI PUMPEN NACHEINANDER TESTEN --", "-- DREI PUMPEN NACHEINANDER TESTEN --"};

void setup() {
  // Initialize LED pins as outputs
  for (int i = 0; i < AnzahlPumpen; i++) {
    pinMode(PumpPins[i], OUTPUT);
  }
  Serial.begin(9600); // Start serial communication
}

void loop() {
  int AnzahlPumpenEingeschaltet;
  bool RichtigeAnzahlPumpen = false;
  
  // Request number of LEDs to light (with validation)
  while (!RichtigeAnzahlPumpen) {
    Serial.println("Wieviel Pumpen wollen Sie testen (min 1 - max 6): ");
    while (!Serial.available()) {} // Wait for user input
    AnzahlPumpenEingeschaltet = Serial.parseInt(); // Read user input as integer
    if (AnzahlPumpenEingeschaltet > 0 && AnzahlPumpenEingeschaltet <= AnzahlPumpen) {
      Serial.print(AnzahlPumpenEingeschaltet); // Inform user about the chosen number of LEDs
      Serial.println(" Pumpen werden getestet.");
      RichtigeAnzahlPumpen = true; // Exit loop if valid input received
    }
    else {
      Serial.println("Bitte geben Sie eine Nummer zwischen 1 et 6 ein. ");
    }
  }
  int GewaeltMode = ModeBeantragen(AnzahlPumpenEingeschaltet); // Request and validate mode choice
  Serial.print(AnzahlPumpenEingeschaltet);
  Serial.print(" Pumpen werden getestet, in Modus: ");
  Serial.println(ModeBeschreibung[GewaeltMode]); // Inform user about the chosen mode
  modeFunctions[GewaeltMode](AnzahlPumpenEingeschaltet); // Process the chosen mode (execute the corresponding function)
}

int ModeBeantragen(int AnzahlPumpen) {
  int mode;
  bool RichtigeMode = false;
  while (!RichtigeMode) {
    ModeAnzeigen();
    Serial.println("Wählen Sie bitte einen Ziffer (Test). ");
    while (!Serial.available()) {} // Attendre l'entrée de l'utilisateur
    mode = Serial.parseInt(); // Lire l'entrée de l'utilisateur comme un entier

    if (mode >= 0 && mode < numModes) {
      RichtigeMode = true;
    } else {
      Serial.println("Bitte einen Ziffer zwischen 0 & 3 eingeben. ");
    }
  }
  return mode;
}

void AllePumpenEinschalten(int AnzahlPumpen) {
  int count = 0;
  while(true){
    for (int i = 0; i < AnzahlPumpen; i++) {
      digitalWrite(PumpPins[i], HIGH);
    }      
    delay(chronoON);
    for (int i = 0; i < AnzahlPumpen; i++ ){
      digitalWrite(PumpPins[i], LOW);
    }
    delay(chronoOFF);
    count++;
    Serial.print("All Pumps had been turned on/off: ");
    Serial.print(count);
    Serial.println(" times");
    if (Serial.available()){
      char userInput = Serial.read();
      if (userInput == 'b'){
        break;
      }
    }
  }
}

void EinePumpeNacheinanderEinschalten(int AnzahlPumpen) {
  int count01 = 1;
  while(true){
    for (int i = 0; i < AnzahlPumpen; i++) {
      digitalWrite(PumpPins[i], HIGH);
      delay(chronoON);
      digitalWrite(PumpPins[i], LOW);
      Serial.print("Pump ");
      Serial.print(i+1);
      Serial.print(" had been turned on/off: ");
      Serial.print(count01);
      Serial.println(" times");
    }
    count01++;
    if (Serial.available()){
      char userInput = Serial.read();
      if (userInput == 'b'){
        break;
      }
    }
  }
}

void ZweiPumpenNacheinanderEinschalten(int AnzahlPumpen) {
  int cycleCount = 1; // Track the overall cycle number
  int pairCount = 1; // Count for each LED pair within a cycle
  while (true) {
    for (int i = 0; i < AnzahlPumpen; i += 2) {
      // Light the current pair of LEDs (i and i+1)
      if (i + 1 < AnzahlPumpen) {
        digitalWrite(PumpPins[i], HIGH);
        digitalWrite(PumpPins[i + 1], HIGH);
      } 
      else {
        digitalWrite(PumpPins[i], HIGH);
      }
      delay(chronoON); // Wait to see the LEDs lit
      digitalWrite(PumpPins[i], LOW);
      if (i + 1 < AnzahlPumpen) {
        digitalWrite(PumpPins[i + 1], LOW);
      }
      Serial.print("Pumps ");
      Serial.print(i + 1);
      if (i + 1 < AnzahlPumpen) {
        Serial.print(" & ");
        Serial.print(i + 2);
      }
      Serial.print(" turned on/off: ");
      Serial.print(cycleCount);
      Serial.println(" times");
      pairCount++; // Increment count for each LED pair
      // Reset pairCount and increment cycleCount at the end of the cycle
      if (i == AnzahlPumpen - 2) { // Check if it's the last LED pair
        pairCount = 1;
        cycleCount++;
      }
    }
    if (Serial.available()){
      char userInput = Serial.read();
      if (userInput == 'b'){
        break;
      }
    }
  }
}

void DreiPumpenNacheinanderEinschalten(int AnzahlPumpen) {
  int cycleCount = 1; // Track the overall cycle number
  int pairCount = 1; // Count for each LED pair within a cycle
  while (true) {
    for (int i = 0; i < AnzahlPumpen; i += 3) {
      // Light the current pair of LEDs (i and i+1)
      if (i + 1 < AnzahlPumpen) {
        digitalWrite(PumpPins[i], HIGH);
        digitalWrite(PumpPins[i + 1], HIGH);
        digitalWrite(PumpPins[i + 2], HIGH);
      } else {
        digitalWrite(PumpPins[i], HIGH);
      }
      delay(chronoON); // Wait to see the LEDs lit
      digitalWrite(PumpPins[i], LOW);
      if (i + 1 < AnzahlPumpen) {
        digitalWrite(PumpPins[i + 1], LOW);
        digitalWrite(PumpPins[i + 2], LOW);
      }
      Serial.print("Pumps ");
      Serial.print(i + 1);
      if (i + 1 < AnzahlPumpen) {
        Serial.print(", ");
        Serial.print(i + 2);
        Serial.print(" & ");
        Serial.print(i + 3);
      }
      Serial.print(" turned on/off: ");
      Serial.print(cycleCount);
      Serial.println(" times");
      pairCount++; // Increment count for each LED pair
      // Reset pairCount and increment cycleCount at the end of the cycle
      if (i == AnzahlPumpen - 3) { // Check if it's the last LED pair
        pairCount = 2;
        cycleCount++;
      }
    }
    if (Serial.available()){
      char userInput = Serial.read();
      if (userInput == 'b'){
        break;
      }
    }
  }
}

void ModeAnzeigen() {
  Serial.println("Welcher Test wollen Sie durchführen: ");
  for (int i = 0; i < numModes; ++i) {
    Serial.print(i);
    Serial.print(": ");
    Serial.println(ModeBeschreibung[i]);
  }
}

Sorry, but I don't see the problem.

I copied the project to Wokwi simulation:

Do you have the newest Arduino IDE ?
A noise pulse from a pump could disturb the Arduino board, but then it resets or starts showing random characters.
Do you know if your Arduino board was damaged in a previous project ?

I hope that someone else has a good idea.

Hi Koepel,

This is my electric plan. Is it possible that it has any influence on the Arduino ??
The Arduino command the realy board and the relay board command the car relay (30A/14V) and the car relay command the Pump.

It seems isolated, but I don't know how the GND and 12V and 5V powers lines are.
If there would a single GND wire for both the pumps and the Arduino board, then anything can happen.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.