Nano doesn't dislplay correct data values

I can't get My Nano to write the correct data values to serial. I'm fairly new to arduino, mostly used it for lighting leds before. Code is attached i'll copy the serial bellow

-1
49
50
51

(It started with -1, then I sent it 1,2,3)
it's using No Line Editing and 9600 Buad

Code bellow:

void setup() {
 // put your setup code here, to run once:
Serial.begin(96010);
}

void loop() {
 // put your main code here, to run repeatedly:
int c=Serial.read();
Serial.println(c);
Serial.read();
while (Serial.available()==0){}
}

(deleted)

49
50
51

What you are seeing are the ASCII values of the characters '1', '2' and '3'

Try subtracting 48 (or '0') from them

Because this code:

while (Serial.available()==0){}

isn't doing anything, it's possible that the compiler optimizes it away. At the top of this Forum is a topic titled Tutorials. Inside that is a general heading Communications. A subclass in that area is Serial Basics. Read that and you will likely answer your own questions. Also, before you post your code, place the cursor in the IDE Source Code Window and press Ctrl-T. This will reformat your code to a standard C style which most of us will find easier to read.

econjack:
Because this code:

while (Serial.available()==0){}

isn't doing anything, it's possible that the compiler optimizes it away.

I disagree. The statement is a vaild wait for something from Serial and will not optimized away.

I think that it's a bad startegy which is very rarely sensible, but it's absolutely valid.

UKHeliBob:

49

50
51



What you are seeing are the ASCII values of the characters '1', '2' and '3'

Try subtracting 48 (or '0') from them

So far this has been most helpful, i typed it in 2 mins, to test something and you were the only one explaining the result thx

Try this

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

void loop()
{
  if (Serial.available() > 0)
  {
    char c = Serial.read();
    Serial.println(c);
  }
}

Note that c is a char so you can't use it as a number and check what your line ending is set to as the simple program will read it and print it too.

UKHeliBob:
Note that c is a char so you can't use it as a number

I think you should rephrase this, a char is a signed 1 byte number.

Something like

Note that c contains ASCII coded data where '0' (49) represents 0, '1' (50) represents 1, ...

Whandall:
I disagree. The statement is a vaild wait for something from Serial and will not optimized away.

What I meant was that the loop does nothing. I checked the assembler code and it does spin around waiting for the Serial object. Still, with nothing controlled by the loop, it seems kinda silly.

econjack:
Still, with nothing controlled by the loop, it seems kinda silly.

Why? What could you do to speedup serial events if you ever choose to wait for some?*
Which is usually a bad idea anyway, but that does not really matter, does it?

added: * on an esp8266 a delay(0); would avoid watchdog resets on longer waits