I want to send this text "A=100, B=500, C=300" from the serial monitor to the Arduino

How can I Extract the Value of the Variable "A" which is 100 from the serial monitor and Assign it to the "A" Variable in the Arduino ? Same with the B and C ?


  • You could ignore all the formatting and only detect 'A', 'B', or 'C', then check if there is a '=' and then do atoi. For which arduino board is it? If the text has a specific format, then sscanf could be used. What if your receive "ABC=10" or with spaces "A = 100" or negative numbers "B=-10" or "A=001"? – Jot Jun 1 at 20:45

You can try this code:

int A, B, C;

void setup()

void loop()
  String readData = "";
  while (Serial.available())
    readData += Serial.read();
  int a_index = readData.indexOf("A");
  int b_index = readData.indexOf("B");
  int c_index = readData.indexOf("C");
  if (!(a_index < 0))
    String a_str = readData.substring(a_index + 2, readData.indexOf(",", a_index));
    A = a_str.toInt();
  if (!(b_index < 0))
    String b_str = readData.substring(b_index + 2, readData.indexOf(",", b_index));
    B = b_str.toInt();
  if (!(c_index < 0))
    String c_str = readData.substring(c_index + 2);
    C = c_str.toInt();
  • Unfortunately the code did not parse the serial data to any of the Variables, But thanks alot for taking the time to write the code – Thulfiqar May 31 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.