serial monitor returns extras calculations


I'm working on project#13 from "Arduino Workshop". The sketch is to send a number to the arduino then the arduino will multiply it by 2 then return the answer the the serial monitor. The problem is when I send any number I also get another answer which is always the same value(sent -38/ multiplied by two -76).

here's the code:

// Project 13 - Multiplying a Number by Two
int number;
void setup()
void loop()
  number = 0; // zero the incoming number ready for a new read
  Serial.flush(); // clear any "junk" out of the serial buffer before waiting
  while (Serial.available() == 0)
    // do nothing until something enters the serial buffer
  while (Serial.available() > 0)
    number = - '0';
    // read the number in the serial buffer,
    // remove the ASCII text offset for zero: '0'
  // Show me the number!
  Serial.print("You entered: ");
  Serial.print(" multiplied by two is ");
  number = number * 2;

attached at the bottom is a pic of the results:

IDE updated to latest 1.05
using arduino duemilanove

You sent a digit '1', 0x31 followed by a newline character 0x0A. you subtract '0' from each, so '1' becomes 1,
newline becomes -0x26 (hex, ie -38 decimal).

The serial monitor sends a line at a time with a newline character terminator. Your Arduino code isn't
checking for newlines and spaces.

Thanks, when I set the monitor to "no line ending" it displays what it's supposed to.

So if I understand this correctly, because the serial monitor is set to "newline" that new line also acts as a value? but why is the value of the new line -0x26(hex -38)?

but why is the value of the new line -0x26(hex -38)?

It isn't. It's value is 0x10. But, you subtract '0' from it. Look up the ASCII value of '0'.

By the way,

  Serial.flush(); // clear any "junk" out of the serial buffer before waiting

No, it doesn't. Not after 1.0, anyway. What it did prior to 1.0 was delete random amounts of unread data. You shouldn't have sent data you didn't want the Arduino to read.

Get rid of this statement.