I'm trying to output decimals to the serial monitor such as 30.0003 as an example... Instead, my code is outputting 30.3 ..
I know why, the code adds the value of '0' then subtracts '0' from it.. so there is nothing left.. as long as there is any number but '0' directly to the right of the decimal point, it works.. but I'm lost on how to keep the '0's to the right of the decimal point until any other number is read...
// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
void setup ()
{
Serial.begin (9600);
Serial.println ("Starting ...");
} // end of setup
void processNumbers (const long n, const long a )
{
Serial.print (n);
Serial.print ('.');
Serial.println (a);
}
void processNumber (const long n)
{
Serial.println (n);
} // end of processNumber
void processInput ()
{
static long receivedNumber = 0;
static long decN = 0;
static boolean negative = false;
static boolean dec = false;
byte c = Serial.read ();
switch (c)
{
case endOfNumberDelimiter:
if(dec){
if (negative)
processNumbers (- decN, receivedNumber);
else
processNumbers (decN, receivedNumber);
}else{
if (negative)
processNumber (- receivedNumber);
else
processNumber (receivedNumber);
// fall through to start a new number
case startOfNumberDelimiter:
receivedNumber = 0;
decN = 0;
dec = false;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
case '.':
dec = true;
decN = receivedNumber;
receivedNumber =0;
break;
} // end of switch
} // end of processInput
}
void loop ()
{
if (Serial.available ())
processInput ();
// do other stuff here
} // end of loop