Serial communication gives strange output

Hello guys.

After understanding how serial communication works by reading information on the arduino site and by getting answers from users on this forum i decided to test it myself. And...it doesn't work.

A simple program like

void setup(){
Serial.begin(9600);}

void loop(){
Serial.print("i received");
Serial.println(200);
delay(1000);}

works great, but as soon as i write more complex programs like this one it doesn't:

[...]
if(Serial.available() > 0)
{inByte=Serial.read();
Serial.print(inByte, DEC);}

this is what happens: -i upload the program succesfully -i send the value 100 with the serial monitor. nothing happens -i send it again, nothing. i send it a third time and i get message back: "i received:0" -i send the value 200, nothing. The second time i send it a get a message back: "i received:0" again -i try sending 200 again. This time the third time i send it i get "i received:0" back from the chip -and then again and again...sometimes three times, sometimes 2 sometimes 1 sometimes 4...and i always get "i received:0" back

I really can't understand what's going on...i guess it's a problem/misunderstanding with the transmission from chip to pc.

Additional info: -atmega328 on breadboard programmed through ICSP -for serial communication with PC i've used the Severino circuit (http://arduino.cc/en/uploads/Main/ArduinoSeverinoSchematic.png)

Thank you!

After understanding how serial communication works

Apparently, you don’t quite understand how it works.

When you send 100 from the Serial Monitor, you are not actually sending the value 100. Instead, you are sending the characters ‘1’, ‘0’, and ‘0’.

The code snippet you posted is only reading a single byte (‘1’, for instance).

Not knowing where in the loop function that snippet was extracted, we can’t provide much more in the way of clarification on how to get serial communication to work. You didn’t show how inByte is defined (byte, char, int?). You didn’t define how you are sending the data to the serial port. One could AssUMe that you used the serial monitor, but it would be better if we didn’t have to AssUMe anything.

Would you post your whole program, and clarify how you are sending the data?

hey Paul, thankks for answering. I took the example froum arduino.cc in the reference section:

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

so inByte is actually incomingByte.

Once i finish uploading the sketch i open the serial monitor, set it to 9600 baud, and write "100" or "200" in the input line and click send.

I still don't get why i only i get a response after sending the data 2 or 3 times.

PS: looks like i didn't understand serial. the arduino.cc reference doesn't offer much help. Is there an in-depth reference which really explains how serial communication works for the arduino?

I don't see anything wrong with the code. I didn't bring my Arduino with me to work today, so I can't test it. I will, when I get home this afternoon, and let you know.

Works for me... With your last posted code, I get this if I send "200"

I received: 50
I received: 48
I received: 48

I get back 3 responses because sending "200" sends 3 ASCII characters to the Arduino. 50, 48, 48 are the ASCII values. If I printed it back as BYTE I would get the actual ASCII characters back.

Serial.println(incomingByte, BYTE);

Yields:

I received: 2
I received: 0
I received: 0

Thanks for trying it out Brett. I’d say…hardware then. I’ll dobule check if everything’s fine. I’m not using an arduino board though, i just have the breadboard version of the Severino, but still…it should be working.

One possible issue i see is you don't have a delay in there to help ensure the buffer has all the data that was sent.

Here is a good guide for the arduino. Project 10 has a great example and walk threw of serial communication. http://www.earthshinedesign.co.uk/ASKManual/Site/ASKManual.html

Damn, sorry guys, my fault. There was a diode placed backwards. I started thinking it was a hardware problem after Brett told me about his test, so thank you.

If i well understood, each digit of what send with the serial monitor is considered as a byte, so if i write 2000 i will get 4 when polling "serial.available". But i still don't get it why the chip sends out these values. The reference on arduino.cc says that:

Serial.print(78, BYTE) gives "N" Serial.print(78, BIN) gives "1001110" Serial.print(78, OCT) gives "116" Serial.print(78, DEC) gives "78" Serial.print(78, HEX) gives "4E"

So if (78, DEC) gives out 78, why should (2, DEC) give 50? (if i send the value "200" with serial monitor i receive back 50, 48, 48, as Brett also wrote)

EDIT: oh wait maybe i got it. when i write "200" in the serial monitor i'm actaully sending out 3 strings of text, right?

So if (78, DEC) gives out 78, why should (2, DEC) give 50?

It won't. Where did you get the impression that it would? I must have missed something in this thread.

That's because i thought that by sending the value "2" via the serial monitor would send him the decimal number...but i guess that it's actually sendind the chip a string.

The serial monitor sends everything as strings. Other programs can send data as strings or as numbers.

Got it. Thank you very much!