how to convert an array to an int in switch case

Hello. I have a question about my project. I am still a beginner in this field. I want to make a program which can filter first and second character. I have tried a lot but the result is still not appropriate with my expectations. I attach the program that I have made below.

There is no problem with the first character (“F”), but this program cannot recognize the number 0,1,2.

when I type F0 on serial monitor, it shows F,0,0,F1. if F1 is the same too F,0,0,F1. I want F1 = F,1,1,F2. I am so thankfull if you can help me.

daripakdanielkesepuluh.ino (2.02 KB)

It is easier for everyone if you include short programs directly in your post using the code button </> so it looks like this

I think you will find it easier if you receive all the data and only then try to figure it out - rather than trying to analyse it as it arrives.

The examples in serial input basics are simple reliable ways to receive data and there is also a parse example.

The second example will probably meet your need if you are just sending data from the Serial Monitor. The third example is the most reliable.

…R

Dear Robin2
Thank you for your reply. Here is the coding:

int i;                      // counter i
int inByte =0;
int numByte =0;
int value = 0;
char*arrayData= "0,1,2";

String stringA = "";

float reading = 0;

void setup() {    // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  analogReference (INTERNAL); 
  pinMode(13, OUTPUT);
  clearBuff();
}

void loop() {
  if (Serial.available()) {
  numByte = Serial.available();
  for (int i =0; i < numByte; i++){
  inByte = Serial.read();
    }
      if (inByte == 13){ //inByte is collected until carriage return;   
        if (stringA.substring(0,1)== "F"){ //if the first character of stringA is "F", this condition is true and ledShort will be operate;
        ledShort();  

        int value = atoi(arrayData);
        if(value = stringA.substring(0,2).toInt()){ //if the second character of stringA is 0,1,2 (according arrayData) this condition is true. Else, it is false.
          switch(value){        
            case 1:
              value = (arrayData[1]); // value = 0
              break;
           case 2:
              value = (arrayData[2]); // value = 1
               break;
           case 3:
              value = (arrayData[3]); // value = 2
              break;
           default:
              clearBuff;
              Serial.print("");
              break;
            } 
           }
          }
      Serial.println(stringA.substring(0,1));
      Serial.println(stringA.substring(0,2).toInt());
      Serial.println(value);
      Serial.println(stringA);
      stringA = "";
      clearBuff();
      }  
            
     else{ 
      stringA += char(inByte);
      if (stringA.length() > 15){
        clearBuff();
        stringA = "";
        }
      }
   }
}


void ledShort() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(50);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  }

void clearBuff(){          // flush chars in serial buffer
  while (Serial.available() > 0 ){
    inByte = Serial.read();
    }
  }

Thank you for your suggestion. I’ll read it. I hope it can solve my case. :slight_smile: :slight_smile: