Arduino Mega serial problems

I recently received an arduino mega for christmas, and after the usual LED fun, I decided to set my hand to some serial stuff. However, serial was behaving funny, and I have no idea why. Even when I use the sketch below, which is an example sketch from the official website.

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, DEC);
        }
}

What it prints is the number that I send, plus 48, and I havn't a clue why. Even if I put incomingByte - 48 before the Serial.print it makes no difference. Any help much appreaciated.

Musicboy: What it prints is the number that I send, plus 48, and I havn't a clue why.

That's the difference between numbers (from math) and text characters (which are in the bytes).

You have sent a byte intending to denote digit 3, for example. Almost all computers agree to represent this digit by the byte 51. Send a capital A, and you will see the number 65. This is basically how text is stored in computers, and transmitted over the internet. Bytes are numbers that can go from 0 to 255, but it is up to us to interpret them with a meaning (and to program computers accordingly).

When dealing with the Arduino, you will certainly use the character encoding table that is called ASCII which has 127 entries. Basically it is just a simple table which looks like this:

| Byte | Meaning | | - | - | | ... | ... | | 32 | | | 33 | ! | | ... | ... | | ... | ... | | 48 | 1 | | 49 | 2 | | 50 | 3 | | ... | ... | | 65 | A | | 66 | B | | 67 | C | | ... | ... | | 126 | ~ |

Today's modern standard is Unicode, however it has 110000 entries, so people usually use a trick called UTF8 to store the numers in bytes.

For the other question:

Musicboy:
Even if I put incomingByte - 48 before the Serial.print it makes no difference.

This might do what you intended:

                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
                if(incomingByte>=48 && incomingByte<=57)
                {
                    Serial.print("Which represents the number: ");
                    Serial.println(incomingByte-48, DEC);
                }

See also the code to transmit bigger numbers, that I posted recently:

geeek:

I assume you did it like this. The C language allows to write a mathematical expression in the middle of the code, and there are some cases where this is useful, but here all you get is a number which is not stored anywhere.

                incomingByte = Serial.read();
                // perform a calculation and forget the result :-)
                incomingByte - 48; // Does NOT change incomingByte
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);

Hope this helps, and a Merry Christmas :slight_smile:

Thank you very much, that was very concise and practically explained, perfect answer! Merry Christmas to you too, and I hope you have a great new year full of geekdom :) Many thanks again, Musicboy