I have been trying on inputting a number on a serial monitor and according to the codes provided by the book,Arduino Workshop ed.2013, the output should return a value a number multiplied by two. However, in my case based on the image I have provided, every time I input a number it returns a number multiplied by two and a redundant output by which the correct output precedes:
"You entered: -38
"-38 multiplied by two is -76".
And Here's the code by the way:
int number;
void setup()
{
Serial.begin(9600);
}
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 = Serial.read() - '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.println(number);
Serial.print(number);
Serial.print(" multiplied by two is ");
number = number * 2;
Serial.println(number);
}
I've been trying find ways to debug the code but nowhere to find. Hope to find an early solution
By the way, the error is on the picture I provide