Trouble understanding Serial double-printing

Hi,

If i open serial controller and for example send 1 i get 49 and 10 as a reply. Sending 2 returns 50 & 10, 3 returns 51 & 10 and so on...

How come? Am i missing something obvious?

int val = 0;
void setup() {  
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    val = Serial.read();
    Serial.println(val);
  }
  
  delay(20);
}

Look up 10 in the ASCII table. Newline.

  if (Serial.available() > 0) {
    val = Serial.read();

If there is serial data to be read, read() returns the data in the low order byte. The return type needs to be int, in order to deal with the possibility of there being nothing to read, but you deal with that possibility, so, the return value could (and should) be stored in a char type variable, not an int type variable. If you change the type, you'll see much different output.

If you change the type of data appended by the serial monitor, you can get rid of the carriage return.

aarg: Look up 10 in the ASCII table. Newline.

Ok, that explains the 10, can't still figure out 49,50...

PaulS:  if (Serial.available() > 0) {    val = Serial.read();

If there is serial data to be read, read() returns the data in the low order byte. The return type needs to be int, in order to deal with the possibility of there being nothing to read, but you deal with that possibility, so, the return value could (and should) be stored in a char type variable, not an int type variable. If you change the type, you'll see much different output.

If you change the type of data appended by the serial monitor, you can get rid of the carriage return.

Tried changing val to a char, still doesn't work as expected! Isn't there any easy way of just sending an int and returning it, with no carriage returns?

Isn't there any easy way of just sending an int and returning it, with no carriage ret

Of course. The easiest is to use Serial.parseInt().

granis: Ok, that explains the 10, can't still figure out 49,50...

You would if you had actually looked at an ASCII chart. It's easy to google.

Isn’t there any easy way of just sending an int and returning it, with no carriage returns?

Do you have the carriage return and/or new line turned on in the serial monitor? Below is some simple servo control code that might be close to what you are looking for.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}

aarg: You would if you had actually looked at an ASCII chart. It's easy to google.

Googled it, makes sense!

Tried the servo-code, was exactly what i needed! Many thanks to you all!