Read Integer from Serial monitor

Hello Everyone,

I am trying to enter single digit integer number through Arduino serial monitor. Code is given below.
it does reads the integer as I can see it on serial monitor but prints -38 with every value which I am trying to read through serial monitor.

As i want to enter number through serial monitor and use that input as a choice to switch case.

I have attached one snapshot with this query to show the output issue.

char incomingByte = 0;
int choice = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0)
  {
     incomingByte = Serial.read();
     choice = incomingByte - '0';
    Serial.println(choice);

    switch(choice)
   { 

   }

  }
}

Hi

Thanks for the solution. It helped me to solve this problem.

Otherwise - for robustness - just implement a way to ignore bad input. This way the \r or \n or any bad communication error characters will just be skipped

void loop()
{
  if(Serial.available() > 0)  {
    int incomingData= Serial.read(); // can be -1 if read error
    switch(incomingData) { 
        case ‘1’:
           // handle ‘1’
           break;

        case ‘2’:
           // handle ‘2’
           break;

        case ‘3’:
           // handle ‘3’
           break;

        default:
           // handle unwanted input here
           break;
   }
 }
   // do other stuff here
}

Note the way you can handle ASCII codes in the switch without doing any math, just compare with the character symbol you expect. It’s faster as no extra code incomingData - '0' needed, no second variable cluttering your memory and code easier to read.

(By the way, Your incomingByte variable most likely does not need to be global, so I have it only as a local variable whose scope is just with the “if I have received something” part and I use an int instead of a char because read will return -1 if there is a read error, which if you use a char will mean you will see a 0xFF data and can’t tell if that was really the 0xFF extended ascii / byte entry or a read error. Probably does not matter in your use case, but good habit to take when you’ll want to deal with binary communication over serial for example)

Serial Input Basics - simple reliable ways to receive data.

...R

1 Like

Robin2:
Serial Input Basics - simple reliable ways to receive data.

whilst it's a fantastic tutorial for the general case and everyone should read it and use it where applicable, here OP wants a single byte input - so he probably does not need to go beyond doing a Serial.read() at the right time and handle the input

J-M-L:
whilst it's a fantastic tutorial for the general case and everyone should read it and use it where applicable, here OP wants a single byte input - so he probably does not need to go beyond doing a Serial.read() at the right time and handle the input

I don't disagree and that was the reason I posted the link without any exhortation to read it or use it.

However in my experience in many cases a newbie will wish to expand his simple project and may benefit from a wider understanding of the possibilities - even if only by noting that there is a resource for future reference.

...R