Switch case... problem

Dear Community,

i have a Nextion display, which is serial connected (TX-RX) to an arduino mega 2650. The screen shows a menu with many buttons, which sends a value to the arduino when they are clicked. The value is stored in the variable "X". I check this process with a Serial.println(X); code in the Serial Monitor. I can see that it´s always correct. So there should be no problem in the connection between the arduino and the nextion display.

After the value is stored in X, i do a switch.. case statement. Now it comes to an unlogic problem. In every case Statement i do a Serial.print("...");. From case 1 to 3 i get the information in the Serial Monitor. So the compiler is at the correct position. After Case 3 i get nothing in the Serial Monitor besides the value which is stored in X (value is correct). I hope i just got a mistake in the code, but I checked it a thousand times.

//Bibliotheken einbinden----
#include <EEPROM.h>
#include "arduinoFFT.h"
 
#define SAMPLES 512            
#define SAMPLING_FREQUENCY 5000

arduinoFFT FFT = arduinoFFT();
 
unsigned int samplingPeriod = 200;
unsigned long microSeconds;

int x;
int rock;
int pop;
int klassik;
int hiphop;
int Frequenz=0;
int Drehmoment=0;
int analogPin = A0;
int avalue=0;
int Ygauge=50;
int Multibox;
double vReal[SAMPLES];
double vImag[SAMPLES];
unsigned long previousMillis =0;
const long interval = 1000;
unsigned long currentMillis = 0;
int i=0;

boolean waitformeasure = false;
boolean soften = true;
boolean tighten = false;

float turns=2;

void setup() 
{
delay(1000);  
//Serielle Schnittstelle startet  
Serial.begin(250000);
Serial2.begin(9600);

Serial.println("EEPROM auslesen..");

EEPROM.get(0, rock);
EEPROM.get(4, pop);
EEPROM.get(8, klassik);
EEPROM.get(12, hiphop);

samplingPeriod = 1000000*(1.0/SAMPLING_FREQUENCY);

//N0 Darstellung Tonfrequenz [Hz]
//N1 Darstellung Drehmoment [Nmm]
//VA1 Darstellung Graph y=50 <==> 0Hz
Serial2.print("n0.val=" + String(Frequenz));  
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff); 

Serial2.print("n1.val=" + String(Drehmoment));  
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff); 

Serial2.print("va1.val=" + String(Ygauge));  
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff); 

}

void loop()
{         
                
Nextion_serial_listen();
delay(5);
}

void Nextion_serial_listen()
{
  if(Serial2.available()>0)
          {         
            x=Serial2.read();
            Serial.print("X=\t");
            Serial.println(x);
          
switch (x) {

  case 1:

        while(Serial2.available()<=0){
        //Warteschleife 
        }
           Multibox = Serial2.read();
          
          if (Multibox == 0){
            turns = 2;
            Serial.println("Turns = 2");
          }
          else if (Multibox == 1){
            turns = 1;
            Serial.println("Turns = 1");
          }
          else if (Multibox == 2){
            turns = 0.5;
            Serial.println("Turns = 0,5");
          }
          else if (Multibox == 3){
            turns = 0.25;
            Serial.println("Turns = 0,25");
          }
          else if (Multibox == 4){
            turns = 0.1;
            Serial.println("Turns = 0,1");
          }           
          else {
            Serial.println("Fehlerhafter Multiboxwert");
            Serial.print("Multibox=  ");
            Serial.println(Multibox);
            turns=0;
         }
          break;      

  case 2:

          if (soften==true && tighten == false){
            soften=false;
            tighten=true;
            Serial.println("Tighten Mode");

          }
            else if ( soften == false && tighten == true){
            soften=true;
            tighten=false;  
            Serial.println("Soften Mode");
          }
          
          break;  

  case 3:
          Serial.println("messen..");
          waitformeasure=true;
          avalue = analogRead(analogPin);
          
          while ((avalue<390 && avalue>310) && (waitformeasure==true)){
          avalue = analogRead(analogPin);
          //Serial.print(avalue);
          }
          Serial.println("start measuring..");
          waitformeasure = false;
          
          i=0;
          while (i<=SAMPLES-1){  
          microSeconds = micros();
          vReal[i] = analogRead(0);
          vImag[i] = 0;
          i=i+1;
          while(micros() < (microSeconds + samplingPeriod)){        
                         //wait..
                      }
          }

          Serial.println("Fast Fourier Transformation..");
          FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
          FFT.Compute(vReal,vImag, SAMPLES, FFT_FORWARD);
          FFT.ComplexToMagnitude(vReal,vImag, SAMPLES);
          double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);     
          peak = peak * 0.97560976;    //Fehlerkorrekturfaktor
          Frequenz=peak;
          Serial.println(peak);
          Serial.println(Frequenz);

          Serial2.print("n0.val=" + String(Frequenz));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff); 

          for (int n=0; n<SAMPLES-1; n++){   //Darstellung der wahren Messwerte
          Ygauge = vReal[n];   
          //Serial.println(Ygauge);      
          Serial2.print("va1.val=" + String(Ygauge-305));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff); 
          } 
          
          Ygauge = 50;
          Serial2.print("va1.val=" + String(Ygauge));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff); 
          break;    

 case 4:
          Serial.println("Storage");
          Serial2.print("n3.val=" + String(rock));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff); 

          Serial2.print("n4.val=" + String(pop));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);          

          Serial2.print("n5.val=" + String(klassik));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff); 

          Serial2.print("n6.val=" + String(hiphop));  
          Serial2.write(0xff);
          Serial2.write(0xff);
          Serial2.write(0xff);   
          break;                     
             
  case 5:
          //TON E1
          Serial.println("Ton E1");
          break;

case 6:
          //TON F1#
          Serial.println("Ton F1#");

          break;   

  case 7:
          //TON F1
          Serial.println("Ton F1");

          break;
          
  case 8:
          //TON G1
          Serial.println("Ton G1");

          break;   

  case 9:
          //TON G1#
          Serial.println("Ton G1#");

          break;
          
  case 10:
          //TON A1
          Serial.println("Ton A1");

          break;   

  case 11:
          //TON A1#
          Serial.println("Tighten Mode");
          
          break;   

  case 12:
          //TON B1
          Serial.println("Ton B1");

          break;
          
  case 13:
          //TON C2
          Serial.println("Ton C2");

          break;        

  case 14:
          //TON C2#
          Serial.println("Ton C2#");

          break;   

  case 15:
          //TON D2
          Serial.println("Ton D2");

          break;   

  case 16:
          //TON D2#
          Serial.println("Ton D2#");

          break;
          
  case 17:
          //TON E2
          Serial.println("Ton E2");

          break;        

 //SNAREdrum------------------------------------------------------           
   case 18:
          //TON C3
          Serial.println("Ton C3");

          break;
          
  case 19:
          //TON C3#
          Serial.println("Ton C3#");

          break;   

  case 20:
          //TON D3
          Serial.println("Ton D3");

          break;
          
  case 21:
          //TON D3#
          Serial.println("Ton D3#");

          break;   

  case 22:
          //TON E3
          Serial.println("Ton E3");

          break;
          
  case 23:
          //TON F3
          Serial.println("Ton F3");

          break;   

  case 24:
          //TON F3#
          Serial.println("Ton F3#");

          break;   

  case 25:
          //TON G3
          Serial.println("Ton G3");

          break;
          
  case 26:
          //TON G3#
          Serial.println("Ton G3#");

          break;        

  case 27:
          //TON A3
          Serial.println("Ton A3");

          break;   

  case 28:
          //TON A3#
          Serial.println("Ton A3#");

          break;   

  case 29:
          //TON B3
          Serial.println("Ton B3");

          break;
          
  case 30:
          //TON C4
          Serial.println("Ton C4");

          break;        

  case 31:
          //TON C4#
          Serial.println("Ton C4#");

          break;
          
  case 32:
          //TON D4
          Serial.println("Ton D4");

          break;    
//Tomdrum----------------------------------------------------    
   case 33:
          //TON A1
          Serial.println("Ton A1");

          break;
          
  case 34:
          //TON A1#
          Serial.println("Ton A1#");

          break;   

  case 35:
          //TON B1
          Serial.println("Ton B1");

          break;
          
  case 36:
          //TON C2
          Serial.println("Ton C2");

          break;   

  case 37:
          //TON C2#
          Serial.println("Ton C2#");

          break;
          
  case 38:
          //TON D2
          Serial.println("Ton D2");

          break;   

  case 39:
          //TON D2#
          Serial.println("Ton D2#");

          break;   

  case 40:
          //TON E2
          Serial.println("Ton E2");

          break;
          
  case 41:
          //TON F2
          Serial.println("Ton F2");

          break;        

  case 42:
          //TON F2#
          Serial.println("Ton F2#");

          break;   

  case 43:
          //TON G2
          Serial.println("Ton G2");

          break;   

  case 44:
          //TON G2#
          Serial.println("Ton G2#");

          break;
          
  case 45:
          //TON A2
          Serial.println("Ton A2");

          break;        

  case 46:
          //TON A2#
          Serial.println("Ton A2#");

          break;
          
  case 47:
          //TON B2
          Serial.println("Ton B2");

          break;                                                         

  case 48:
          //TON C3
          Serial.println("Ton C3");

          break;        

  case 49:
          //TON C3#
          Serial.println("Ton C3#");

          break;   

  case 50:
          //TON D3
          Serial.println("Ton D3");

          break;   

  case 51:
          //TON D3#
          Serial.println("Ton D3#");

          break;
          
  case 52:
          //TON E3
          Serial.println("Ton E3");

          break;        

  case 53:
          //TON F3
          Serial.println("Ton F3");

          break;
          
  case 54:
          //TON F3#
          Serial.println("Ton F3#");

          break;   

  case 55:
          //TON G3
          Serial.println("Ton G3");

          break;   

  case 56:
          //TON G3#
          Serial.println("Ton G3#");

          break;
          
  case 57:
          //TON A3
          Serial.println("Ton A3");

          break;        

  case 58:
          //TON A3#
          Serial.println("Ton A3#");

          break;
          
  case 59:
          //TON B3
          Serial.println("Ton B3");

          break;    
  default:
          Serial.println("NIx");
          break;           
}
}           
}

And there is a picture from the Serial Monitor:
Screenshot 2022-05-06 024802

Thankyou for your help!

I wrote something wrong. I even don´t get any Serial.print... after case 3: .

Format the code, check the curly braces. They don't look right to me...

If the {} are in the right place, Try to put the content of case 3 within {}

  case 3 : {
      …
      break;
    }

You are getting compiler warnings as follows...

/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_843771/Blink.ino: In function 'void Nextion_serial_listen()':
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_843771/Blink.ino:195:12: warning: jump to case label [-fpermissive]
       case 4:
            ^
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_843771/Blink.ino:168:16: note:   crosses initialization of 'double peak'
         double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
                ^~~~
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_843771/Blink.ino:218:12: warning: jump to case label [-fpermissive]
       case 5:
            ^
/var/folders/4z/3g9th3gs4cng3b803dg5bhw40000gn/T/arduino_modified_sketch_843771/Blink.ino:168:16: note:   crosses initialization of 'double peak'
         double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
                ^~~~

This is happening because you are declaring new variables in the preceding case block. e.g.

      case 3:
        double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);

Either move the declaration to the start of the function:

void Nextion_serial_listen()
{
  double peak;

or use {} to enclose any case block that declares a variable.

Have a look here if you want to know more.

This is what I had suspected for case 3 (did not look at the code on iPhone).

defining a var in the case causes the case to puke. Define

double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_

Put:

double peak;

BEFORE the SWITCH statement and remove from the double from that statement & it'll start to work.

The nasty part is the compiler doesn't flag it and you get weird runtime behaviors.

Did you not read post #5 where I showed the compiler warnings?

...when the compiler warning level is not set to "all"

Thankyou very much. I didn´t know that. It immediatly worked out.
Thanks for your help and have a nice day! :slight_smile:

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