Hello all!
I'm making a project that connects arduino Due to Beaglebone black directly. There are no problems when sending single characters. However, I need to send signed integers with multiple digits (-315 for example) from the Beaglebone to Arduino Due. I've found some code online and modified it a little bit to adjust it to the project purpose but when printing it to the Serial monitor I'm receiving different values. For example sending a value of -13 from the beaglebone side, I'm getting it like (-13 0 0 1 3 0 0) in a periodically behaviour. The code I'm using is:
// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012
const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter = '>';
void setup ()
{
Serial.begin (9600);
Serial2.begin (9600);
Serial.println ("Starting ...");
Serial2.flush();
Serial.flush();
} // end of setup
long processInput ()
{
static long receivedNumber = 0;
static boolean negative = false;
byte c = Serial2.read ();
long result;
switch (c)
{
case endOfNumberDelimiter:
if (negative){
// processNumber (- receivedNumber);
receivedNumber *= -1;
}
else{
// processNumber (receivedNumber);
receivedNumber *= 1;
}
break;
// fall through to start a new number
case startOfNumberDelimiter:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
} // end of switch
result = receivedNumber;
return result;
} // end of processInput
void loop ()
{
while(Serial2.available()){
delay(5);
if (Serial2.available() >0 ){
delay(40);
Serial.println(processInput());
}
}
// do other stuff here
} // end of loop
Would anybody help me with this problem!?