Go Down

Topic: <SOLVED> Strange numbers in serial monitor (Read 2012 times) previous topic - next topic

Baghdad_boy

Feb 16, 2010, 05:21 am Last Edit: Feb 16, 2010, 06:25 am by Baghdad_boy Reason: 1
Hi all,

When I run this code:

Code: [Select]

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 :)

thurston

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:

Code: [Select]
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);
 }
}



Baghdad_boy

#2
Feb 16, 2010, 06:22 am Last Edit: Feb 16, 2010, 07:56 am by Baghdad_boy Reason: 1
Legend Thurston. Thanks heaps for the help mate :)

Problem solved.

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

Code: [Select]

numericalValue = atoi(&incomingData);


But thanks for getting me on the right track.

PaulS

#3
Feb 16, 2010, 11:51 am Last Edit: Feb 16, 2010, 11:51 am by PaulS Reason: 1
Code: [Select]
numericalValue = atoi(&incomingData);


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

Code: [Select]
numericalValue = incomingData - '0';

thurston

Code: [Select]
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.

Baghdad_boy

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.

PaulS

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".

Code: [Select]
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.

pracas

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
Be The Change...

PaulS

Quote
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?

Go Up