arduino BT and serial commands

Hi

I am getting weird results with this code. If I send an "a" I get a response, but only once. Sending a "d" freezes the serial monitor. If hit "b" after "a" nothing happens, but no character works after that.

thanks for help

int ledPin =13;

int SOLENOID1 = 5;
int SOLENOID2 = 7;
int SOLENOID3 = 10;
int SOLENOID4 = 12;

int solenoids[] = {SOLENOID1,SOLENOID2,SOLENOID3,SOLENOID4};
int cnt = 0;

void setup()
{
  Serial.begin(115200); //init of the serial com
  pinMode(ledPin, OUTPUT);
  for(int i=0;i<4;i++){
    pinMode(solenoids[i], OUTPUT);
  }
}

void loop()
{
  
  cnt++;
  cnt %= 20;
  if(cnt < 10) digitalWrite(ledPin, LOW);
  else digitalWrite(ledPin, HIGH);
  
  
  for(int i=0;i<4;i++){
    digitalWrite(solenoids[i], LOW);
  }
  delay(50);
  checkInput();
  delay(50);
  //Serial.println("loop");
}

void checkInput(){
//  boolean hadInput = false;
  if ( Serial.available()) { // serial reception
    char ch = Serial.read();
    int solenoidID = -1;
    switch(ch) {
    case 'a': // numeric value
      solenoidID = 0;
      break;
    case 'b': // reading of the letter
      solenoidID = 1;
      break;
    case 'c':
      solenoidID = 2;
      break;
    case 'd':
      solenoidID = 3;
      break;
    }
    
    if(solenoidID > -1){
      Serial.print("SOLENOID : ");
      Serial.println(solenoidID);
      digitalWrite(solenoids[solenoidID], HIGH);
    }    
    
  }
}

Is it possible that the serial data needs to be flushed somehow?

thank you

fubbi

help?

Works for me. (v16)
I copied and pasted your code and ran it on a Mega

Here's what my serial monitor outputs when I type a b c d a couple of times

SOLENOID : 0
SOLENOID : 1
SOLENOID : 2
SOLENOID : 3
SOLENOID : 0
SOLENOID : 1
SOLENOID : 2
SOLENOID : 3

Maybe your baud setting for the serial monitor is incorrect?

(You really should add a default: case to your switch statement to handle characters other than a-d even if it's just to ignore them.)

so the code is okay i guess? It must be something related to the bluetooth and sending serial.

Is there any way to make the way I am working with the serial more folproof or safe?