<SOLVED> Strange numbers in serial monitor

Hi all,

When I run this code:

int ledPin = 13; // Built in LED on Arduino
int incomingData; // To hold value of data coming through COM port

void setup(){
  pinMode(ledPin, OUTPUT); // Set ledPin to output
  Serial.begin(9600); // Setup serial with baud rate of 9600 (this must be same on Roborealm)
}

void loop(){
  // Wait until there is new data available at COM port
  if (Serial.available() > 0){
    // Read the value and store in incomingData
    incomingData = Serial.read();
  }
  
  Serial.println(incomingData);
  
  // If the value of the data is greater than 0 then turn on LED
  if (incomingData > 0){
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

If I type 0 in the serial monitor and hit send, it returns the value 48. If I type 1 it returns 49 and so on. Does anyone know whats wrong?

Thanks for the help :slight_smile:

When you send a 0 from Serial, you are sending an ASCII 0.
Since you are reading this into an int variable, the Arduino treats it as it's hex value. Search Google ascii table to see the hex value for each character.

What you need to do is capture the incoming serial data to a char variable. Then convert it to an integer using the atoi() function.
Something like:

int ledPin = 13; // Built in LED on Arduino
//thurston - changed the incoming variable type to char
char incomingData; // To hold value of data coming through COM port

void setup(){
  pinMode(ledPin, OUTPUT); // Set ledPin to output
  Serial.begin(9600); // Setup serial with baud rate of 9600 (this must be same on Roborealm)
}

void loop(){
  // Wait until there is new data available at COM port
  if (Serial.available() > 0){
    // Read the value and store in incomingData
    incomingData = Serial.read();
  }
  
   Serial.println(incomingData);
//thurston - the next line converts the character "0" to a number 0
   int numericVal = atoi(incomingData);
  // If the value of the data is greater than 0 then turn on LED
  //thurston - change the reference to the number 0 instead of the character 0.
  if (numericVal > 0){
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

Legend Thurston. Thanks heaps for the help mate :slight_smile:

Problem solved.

EDIT: Btw I couldn't use atoi since that takes a string value. So I had to change it to

numericalValue = atoi(&incomingData);

But thanks for getting me on the right track.

numericalValue = atoi(&incomingData);

That takes up a lot more memory, and is a much slower way of doing this:

numericalValue = incomingData - '0';
numericalValue = incomingData - '0';

I'm pretty new to C (I've worked in Python and PHP in the past) and these "tricks" keep blowing my mind.
I'll have to play around with that a bit to see if I can cut a few more bytes out of one of my projects.

Sweet thanks for the tip PaulS. I managed to read the number into a buffer now and then myint = atoi(buffer)

But thanks for the trick. I'll use it in the future. With the Arduino sometimes you need to save as much as possible since its only a small processor.

Instead of saving the characters as a string, and using atoi to convert the string to an integer, you can build up the number "on the fly".

int numFromPort;
void loop()
{
   numFromPort = 0;
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar >= '0' && inChar <= '9')
      {
         numFromPort *= 10;
         numFromPort += (inChar - '0');
      }
   }

   // numFromPort now contains the data read from the port, as an integer
}

If the serial buffer contains '8', '3', '4', numFromPort will equal 0, 8, 80, 83, 830, and 834 at various times through loop.

Hey Paul,

Great one there... I just finished writing some code for this in a longish fashion and then saw this and i'm stumped!

Cheers,
Pracas

I just finished writing some code for this in a longish fashion and then saw this and i'm stumped!

What can I do to un-stump you?