Serial.println() returns two values?

I am using the serial monitor to send a value (1-9) to the Arduino and then have the Arduino send back a response stating what it received. For example I send value 1 then returns the value 1, I send value 2 then returns value 2, and so on.

For some reason Arduino returns value 1 and next line value -38. I have followed the code from two examples, but my results are different.

Any suggestions and advice would be much appreciated!

Hardware and Software Setup:
Arduino Uno
Arduino v1.0.1
MacBook Pro Mid 2010
OSx 10.8

Code:

// the setup routine runs once when you press reset:
void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

// the loop routine runs over and over again forever:
void loop() {

// Have the arduino wait to receive serial input:
while (Serial.available() == 0);

// Read the serial input:
int val = Serial.read() - '0';

// Return the serial input:
Serial.println(val);

}

Line feed.
Don't send it, or filter it out.

@ AWOL,

Thanks for the quick response! I spent some time to research your advice, but I still do not understand. I assume your advice applies to the Serial.println and how it was implemented in the loop? I am a complete "noob" with programing, could you please clarify "don't send it or filter it out"?

I have also tried this code:

Output from Serial Monitor:

I received: 1
I received: -38

Thanks!

I think what AWOL is saying is don't sent the linefeed, or ignore it when recieved

Serial.println, the ln part of it means send (val) + linefeed = 2 serial chars

Serial.print just sends (val) , 1 serial char

you could do this

if (Serial.available()>1) // wait for 2 bytes
  {
     1stbyte = Serial.read();  // First byte
     2ndbyte = Serial.read(); // this is LF, ignore it
  }

as per my post Arduino Forum

No, you can't have a variable called "1stByte"

true, but it makes more sense than if i had said
cPin = Serial.read(); // First Byte is Pin Number

1stbyte & 2ndbyte are for demonstration only

@P18F4550,

Thanks for the quick response! I have tried using just the Serial.print(), but this displays the values 1 and -38 on the same line. I read through your example but I am not trying to do the same thing as you. Meaning I want send the value 1 to the Arduino and receive 1 response back.

I want:

  • type the number 1 into serial monitor and press send button.
  • serial monitor returns 1

Instead I get:

  • type the number 1 into serial monitor and press send button.
  • serial monitor returns 1
  • serial monitor returns -38

Maybe a computer issue, software issue, or older Arduino board? I'll keep trying!

Make sure the dropdown box on your Serial monitor says "No Line Ending"

@Arrch,

Thank you! That was my problem!

Make sure the dropdown box on your Serial monitor says "No Line Ending"

Everything is working correctly. Now off to read more about serial monitor and its options before going further. (Something I should of done before!)

Thanks!