Problems using switch case

Hello everyone, I hope you are having a good day, this is my first post and I hope I do not break any rule, the problem is the following, I must implement an offset on screens with 8x8 matrix, but they are programmed as state machines , and that the change of states is programmed through bluetooth. For that we use the MAX7219 with its library and the Parola library. The case is that I have not been able to advance much since it does not change between any of the cases. I don’t know if the problem is that it is not reading the bluetooth data correctly or if there is an error in the syntax. I would really appreciate if you could guide me.

#include <MD_Parola.h> 
#include <MD_MAX72xx.h> 
#include <SPI.h> 

#define MAX_DEVICES 4  //Cantidad de matrices LED


//Definición de pines del arduino
const int CLK_PIN = 13; // Terminal de reloj
const int DATA_PIN = 11; // Terminal de datos
const int CS_PIN = 10; // Terminal de load


const char separadorDatos = '#'; // Carácter que indica que es un Dato lo que enviamos

MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);  // Configuración de librería y comunicación SPI

textEffect_t efecto = PA_SCROLL_LEFT; //desplazamiento por defecto
textPosition_t alineacion = PA_CENTER; //alineación del texto

char *texto_defecto = "INSERTE MENSAJE"; //texto por defecto 
char *texto_nuevo = "";
int brillo = 15;     // variable que almacenará el dato de brillo enviado por el bluetooth (15 por defecto)
uint16_t velocidad = 100;  // variable que almacenará el dato de velocidad enviado por el bluetooth (100 por defecto)



void setup() {
    // I will use these pins to find out what state you are currently in
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);

    P.begin (); // Start the library and therefore the MAX7219
    P.setIntensity (brillo); 
    P.setSpeed(velocidad);
    P.displayScroll (texto_defecto, alineacion, efecto, velocidad);  // Project default settings
      
    // Inicializamos el puerto serial con una velocidad de 9600
    Serial.begin(9600);
}

// Este método se ejecuta infinitas veces
void loop() {
    // Agregamos el caracter # para indicar que es un dato
    Serial.print("#");

    // We expect a time of 20 milliseconds
    delay(20);
}

/*The SerialEvent function is executed when a new data reaches the hardware through the Rx terminal. * /
void serialEvent() {
    // If the serial port is available we read the data
    while (Serial.available()) {

    // Gets the next byte that was received, this is a character
    char comando = (char)Serial.read();

    // Depending on the received character, we execute an action
    switch (comando) {
        case 'i':
            cambiarEstado0();
            break;
        case 'v':
            cambiarEstado1();
            break;
        case 'a':
            cambiarEstado2();
            break;
        case 'n':
            cambiarEstado3();
            break;
        default:
// If it is not a command character we return the following message
            Serial.println("Comando no reconocido");
            break;
        }
    }
}

void cambiarEstado0() {
    // Cambiamos el estado del LED
   /* char primer = (char)Serial.read();

    P.displayAnimate(); 
    if (P.displayAnimate()){
       P.displayReset();
       }
    
    if (primer == '1'){
      brillo = 0; P.setIntensity (brillo);
      }   
    else if (primer == '2'){
      brillo = 15; P.setIntensity (brillo);
      }
    
    else if (primer == '3'){
      velocidad = 150; P.setSpeed(velocidad);
      }  
    else if (primer == '4'){
      velocidad = 100; P.setSpeed(velocidad);
      }  
    else if (primer == '5'){
      velocidad = 50; P.setSpeed(velocidad);
      } 
    */
   
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
}

void cambiarEstado1() {
    efecto = PA_MESH;
    P.setTextEffect(efecto, efecto);
    
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    
    Serial.println("Estado 1");
}

void cambiarEstado2() {
    efecto = PA_DISSOLVE;
    P.setTextEffect(efecto, efecto);
   

    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    Serial.println("Estado 2");
}
void cambiarEstado3() {
    texto_nuevo += (char)Serial.read(); 
    strcpy(texto_defecto, texto_nuevo);
    P.displayAnimate ();
   

    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
}

My partner also has his version of the code in a slightly different way, I hope that with these two the objective of this project is understood a little.

#include <MD_Parola.h> 
#include <MD_MAX72xx.h> 
#include <SPI.h> 

#define MAX_DEVICES 4  //Cantidad de matrices LED

//Definición de pines del arduino
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
   
MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);  // Configuración de librería y comunicación SPI

textEffect_t efecto = PA_SCROLL_LEFT; //desplazamiento por defecto
textPosition_t alineacion = PA_CENTER; //alineación del texto

char *texto_defecto = "INSERTE MENSAJE"; //texto por defecto 
char *texto_nuevo = "";
int brillo = 15;     // variable que almacenará el dato de brillo enviado por el bluetooth (15 por defecto)
uint16_t velocidad = 100;  // variable que almacenará el dato de velocidad enviado por el bluetooth (100 por defecto)
int data_bluetooth = 0; // variable que almacenará el dato de que botón se presionó 
int  state = 0;


void setup ()
{ 
  P.begin (); // Inicia la librería y  por ende el MAX7219
  P.setIntensity (brillo); 
  P.setSpeed(velocidad);
  P.displayScroll (texto_defecto, alineacion, efecto, velocidad);  // Proyecta la configuración por defecto
  Serial.begin(9600);

  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop () 
{ 
  
if (Serial.available ()> 0) 
{ 
    data_bluetooth = Serial.read();
}

else
{
   switch(state)
  {
    case 0:  

    P.displayAnimate(); if (P.displayAnimate()){P.displayReset();}
    
    if (data_bluetooth == 1){brillo = 0; P.setIntensity (brillo);}   
    if (data_bluetooth == 2){brillo = 15; P.setIntensity (brillo);}
    
    if (data_bluetooth == 3){velocidad = 150; P.setSpeed(velocidad);}  
    if (data_bluetooth == 4){velocidad = 100; P.setSpeed(velocidad);}  
    if (data_bluetooth == 5){velocidad = 50; P.setSpeed(velocidad);} 
    
    if (data_bluetooth == 7){state = 1;} 
    if (data_bluetooth == 8){state = 2;}
    if (data_bluetooth == 9){state = 3;}
    else {state = 0;}
   
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    break;

    case 1:
    efecto = PA_MESH;
    P.setTextEffect(efecto, efecto);
    if (data_bluetooth == 9){state = 3;}
    else {state = 1;}
    
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    break;

    case 2:
    efecto = PA_DISSOLVE;
    P.setTextEffect(efecto, efecto);
    if (data_bluetooth == 9){state = 3;}
    else {state = 2;}

    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    break;

    case 3:
    P.displayReset();
    Serial.flush();
    while (Serial.available() > 0) {texto_nuevo += (char)Serial.read();} 
    strcpy(texto_defecto, texto_nuevo);
    P.displayAnimate ();
    if (data_bluetooth == 6){state = 0;}
    else {state = 3;}

    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    break;
 }
 /* P.displayAnimate ();
  if (P.displayAnimate()) {P.displayScroll (texto_defecto, alineacion, efecto, velocidad); P.displayAnimate ();}*/
  }
}

I also add the simulation in proteus just in case it’s of any use.

The first program can't do anything because all the relevant code has been commented out

In the second program I suspect this

if (data_bluetooth == 1){brillo = 0; P.setIntensity (brillo);}   
    if (data_bluetooth == 2){brillo = 15; P.setIntensity (brillo);}
    // etc

would be better like this

if (data_bluetooth == '1') {
     brillo = 0; P.setIntensity (brillo);
}   
else if (data_bluetooth == '2') {
      brillo = 15; 
      P.setIntensity (brillo);
}
//etc

Note that I have put the numbers in single quotes assuming that you are sending characters. And I have added ELSE.

Also I have put each statement on a separate line as that makes it much easier to read a program. Never put multiple statements on the same line.

...R

Uh, thank you very much for the advice, I will use the else and organize the code structure well, but forgive me I think I did not express myself well, the problem I have is when changing cases, that's why I had commented the code, because I have not been able to use it since it still does not enter the respective case. The situation is, the data that is sent from the application apparently if it is received on the arduino, since with the virtual terminal of proteus you can see each character that the application sends from the cell phone, but that's all, it just shows them, If I send you the letter "i" which would be to enter one of the cases, it shows it in the terminal, but it does not enter case "i"

huberfuentes:
since with the virtual terminal of proteus

I have no experience with Proteus. I had assumed you are using a real Arduino.

...R

In theory it should be the same since it is a simulator. Is your part the switch and case well written?
Thank you very much for responding so quickly

Have you tried modifying the second program in the way I suggested? What happened when you tried it?

I'm not going to offer help with two separate programs intended for the same purpose.

...R

In theory it should be the same since it is a simulator.

We have, in several instances, found that that is not always true. The best simulator of an Arduino is a real
Arduino.