How do I handle negative numbers?

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);
}

Have a look at specification of serial.read() Serial.read() - Arduino Reference

Particularly the part that says it returns "the first byte of incoming serial data available"

Your code reads a single ASCII character and treats it as a number. You'll need to do a bit more modification if you want it to be able to read more than one character and convert them into a single integer.

Steve

Thank you Steve

Chapter 4, Arduino Cookbook by Michael Margolis

If you want to handle negative numbers, your code needs to recognize the minus ('-') sign.

For example:

int value = 0;

int sign = 1;



void loop()
{
  
if( Serial.available())
{
    
char ch = Serial.read();
    
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
       
value = (value * 10) + (ch - '0'); // yes, accumulate the value
    
else if( ch == '-')
       
sign = -1;
    
else // this assumes any char not a digit or minus sign terminates the value
    
{
       
value = value * sign ;  // set value to the accumulated value
       
Serial.println(value);
       
value = 0; // reset value to 0 ready for the next sequence of digits
       
sign = 1;
    
	}
  
   }

}
1 Like

That looks like the idea. And that should allow you positive or negative multidigit integers. A major advance.

The { braces are in a bit of a mess and the treatment of the - sign is a bit odd. It looks like it (or they) could be anywhere in the number e.g. 12-34 would be interpreted as -1234 as would -1-2-3-4 but it's nearly there.

Steve