PDA

View Full Version : [Question(s)] Arduino serial problems



ROBOTMAN
05-09-2009, 06:38 PM
My arduino is freaking out! I have a basic program that prints the serial value it receives. It worked fine until a while ago when it started doing this. Here is my code.


int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
}


If input a 1 in it should show 1 right?

Well it instead it says:

I received: 49

For the letter A it says:

I received: 69

Can anyone help?

ROBOTMAN
05-09-2009, 06:40 PM
I checked and my baud rate is set to 9600 so its not a baud rate problem.

Adrenalynn
05-09-2009, 06:47 PM
I think you'll find for the letter A it's displaying "65" not "69".

I suspect the code you're showing us is not the code compiled and running on the device. You actually have a byte cast in there somewhere.

65 Dec. is ASCII A
49 Dec. is ASCII 1

ROBOTMAN
05-09-2009, 06:53 PM
Ya its 65. No way I know its the code I have running. I just copied it from this page and tried it with the same results. Could it be a setting I changed?

lnxfergy
05-09-2009, 07:39 PM
Wait, so when you send A it says 65?

If so, nothing is wrong. You sent an ASCII 1 (which is decimal 49). If you want it to say 1, you have to send decimal 1, your terminal should have something like a "send hex" mode.... use that.

-Fergs

Noodle
05-09-2009, 08:15 PM
Yeah. Arduino IDE sends ASCII in serial mode. I'm sure there's a setting somewhere to change it. Or you can just compensate for ASCII instead.

Adrenalynn
05-09-2009, 09:54 PM
Good catch - I didn't realize he was sending with the IDE. If you type as Byte you'll get ASCII on the device (which is handy if you're talking to an LCD). If you type as char or int, then you get what you expect. That's how I talk to the SSC32 across the XBee link from the Arduino

ROBOTMAN
05-09-2009, 10:28 PM
OK problem solved all I have to do is convert with the BYTE command. How to implement this into my code is still a mystery. I don't know why it automatically converts to ACSII now but if anyone can find out how to fix it it would be nice.

Adrenalynn
05-09-2009, 10:41 PM
Actually - the BYTE data type will spit ascii encoded data out. You want to convert to an int, char, or similar.

But, really, why not just process the ASCII-armored byte? That's probably more robust anyway.

ROBOTMAN
05-09-2009, 10:42 PM
Ya I think your right.

Adrenalynn
05-09-2009, 10:59 PM
Just make sure you enforce the casting everywhere until you're ready to do something else with it. Loose data-typing can "byte" you in the butt every time.