ASCII woes

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
lospheris
 
Posts: 17
Joined: Wed Nov 26, 2008 6:18 pm

ASCII woes

Post by lospheris »

So i am playing around with lesson 4 of the tutorials and i have run into either a problem with my Duemilanove or a lack of knowledge on my part, most likely the latter.
i am running this sketch to play around with signed and unsigned variables.

Code: Select all

/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 */

byte num = 126;
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.print("You Started with: ");
  Serial.println(num);  // prints hello with ending line break 
  Serial.print("Now you have: ");
  Serial.println(num - 128);
}

void loop()                       // run over and over again
{
                                  // do nothing!
}
Now as some of you may pick up on right a way my output is not like i thought it would be.
It looks like this

Code: Select all

You Started with: ~
Now you have: -2
I know what is happening, the serial monitor seems to be viewing my byte as an ASCII character instead of a "number"
if i change it to int then it displays fine but i didn't want an int or i would have made it that in the first place.
I would greatly appriciate any help that anyone could give me, whether it be directions on how to fix this or why it happens this way.

User avatar
halley
 
Posts: 73
Joined: Fri Nov 21, 2008 11:07 pm

Re: ASCII woes

Post by halley »

If you have a byte variable, it is an unsigned value from 0 to 255. If you try to Serial.print() a byte value, it will be put verbatim, bit for bit with no conversions, to the serial port, which appears in your terminal as a character with that ASCII code.

If you have an int variable, it is a signed value from -32768 to 32767, and the Serial.print() routine will print a '-' character if below zero, and then start printing decimal digits using ASCII characters '0' through '9'. Up to six characters will be put to the serial port in this case.

If you have a byte value but want to see the digits, use Serial.print( (int) (mybytevalue) ).

If you have an integer that you expect is in the 0-255 range and want to print that as a character by its ASCII code, use Serial.print( (byte) (myintvalue) ).

The underlined part is called a "type cast," it tells the compiler that you want to use the given value on the right as a different type, such as treating an int as a byte, or vice versa. It doesn't change the variable at all, it just changes the way the compiler understands the value in that one place in your program. Be sure to include the parentheses just like the underlined part. The italics part is an example; put whatever you want there. Those parentheses are optional but if it's a formula instead of just a single word, you probably want the parentheses around that as well.

User avatar
lospheris
 
Posts: 17
Joined: Wed Nov 26, 2008 6:18 pm

Re: ASCII woes

Post by lospheris »

thank you very much halley, that clears it up for me and now my program runs like a champ.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”