Hello friends,
Adult learner, Arduino Mega 2560 R3, going through the Arduino Workshop book by John Boxall. Chapter 5, project #13: Multiply a number by two, page 106.
I have modified the code to prompt the user to enter a number in the serial monitor. But if I enter a negative number the serial monitor outputs 2 different executions of the number, one for the (-) sign and one for the actual number.
I'm lost as to why it does this, since using int allows for negative numbers.
How do I fix this? please: in layman (slow a$$ adult learner) speak.
Here is the code I'm working with:
// Arduino Workshop- John Boxall:
//Project 13 - Multiplying a Number by Two
int number;
void setup(){
Serial.begin(115200);
}
void loop(){
// zero the incoming number ready for a new read
number = 0;
// clear any "junk" out of the serial buffer before waiting
Serial.flush();
//Prompt user for input
Serial.println("Enter a number to perform multiplication by 2.");
// do nothing until something enters the serial buffer
while (Serial.available() == 0){
// wait for serial input
}
// read the number in the serial buffer,
// remove the ASCII text offset for zero: '0'
while (Serial.available() > 0){
number = Serial.read() - '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);
}