Pages: [1]   Go Down
Author Topic: <SOLVED> Strange numbers in serial monitor  (Read 1551 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

When I run this code:

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 smiley
« Last Edit: February 16, 2010, 12:25:29 am by Baghdad_boy » Logged

Boise, ID USA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Legend Thurston. Thanks heaps for the help mate smiley

Problem solved.

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

Code:
numericalValue = atoi(&incomingData);

But thanks for getting me on the right track.
« Last Edit: February 16, 2010, 01:56:23 am by Baghdad_boy » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
numericalValue = atoi(&incomingData);

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

Code:
numericalValue = incomingData - '0';
« Last Edit: February 16, 2010, 05:51:31 am by PaulS » Logged

Boise, ID USA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Chennai, India
Offline Offline
God Member
*****
Karma: 0
Posts: 752
Peace!!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Be The Change...

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: