Go Down

Topic: How to send numbers from one Arduino to another using Serial (Read 4971 times) previous topic - next topic

matinzk

When I do it this way:

Code: [Select]

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';

long _rpm = 0;


void setup ()
  {
  Serial.begin (115200);
  Serial2.begin (115200);
  Serial3.begin (115200);
 
   pinMode(8, INPUT); 
  } // end of setup
 
void processNumber (const long n)
  {
      Serial.println(n);
  }  // end of processNumber
 
void processInput ()
  {
  static long receivedNumber = 0;
  static boolean negative = false;
 
  byte c = Serial2.read ();
   
  switch (c)
    {
     
    case endOfNumberDelimiter: 
      if (negative)
        processNumber (- receivedNumber);
      else
        processNumber (receivedNumber);

    // 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 
  }  // end of processInput
 
void loop ()
  {
       

        if (Serial2.available ())
        {
          processInput ();
        }

  }


the output I get from the second Arduino on the Serial Monitor is correct. So when I send <8000> from the 1st Arduino I get 8000 on the Serial Monitor of the 2nd Arduino.

When I do send <8000> this way from the 1st Arduino however,

Code: [Select]

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';

long _rpm = 0;


void setup ()
  {
  Serial.begin (115200);
  Serial2.begin (115200);
  Serial3.begin (115200);
 
   pinMode(8, INPUT); 
  } // end of setup
 
void processNumber (const long n)
  {
     _rpm = n;
  }  // end of processNumber
 
void processInput ()
  {
  static long receivedNumber = 0;
  static boolean negative = false;
 
  byte c = Serial2.read ();
   
  switch (c)
    {
     
    case endOfNumberDelimiter: 
      if (negative)
        processNumber (- receivedNumber);
      else
        processNumber (receivedNumber);

    // 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 
  }  // end of processInput
 
void loop ()
  {
       

        if (Serial2.available ())
        {
          processInput ();
        }
                       
       Serial.println(_rpm);

  }


then I don't get the correct values, sometimes I get 800, sometimes 80 sometimes 8 etc.

Hope this is more clear.

Nick Gammon

Code: [Select]
void loop ()
  {
        if (Serial2.available ())
        {
          processInput ();
        }
                       
       Serial.println(_rpm);
  }


As PaulS said, you are printing _rpm before it arrives. That's why I made a function processNumber. You are supposed to print it from there, when it has totally arrived. Why are you changing a method that works, to one that doesn't?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
As PaulS said, you are printing _rpm before it arrives. That's why I made a function processNumber. You are supposed to print it from there, when it has totally arrived. Why are you changing a method that works, to one that doesn't?

That's what I thought initially. But, in processNumber, he's copying the value to _rpm, rather than printing it. Since processNumber doesn't get called until the > arrives, copying the value in processNumber should be OK.

matinzk


Quote
As PaulS said, you are printing _rpm before it arrives. That's why I made a function processNumber. You are supposed to print it from there, when it has totally arrived. Why are you changing a method that works, to one that doesn't?

That's what I thought initially. But, in processNumber, he's copying the value to _rpm, rather than printing it. Since processNumber doesn't get called until the > arrives, copying the value in processNumber should be OK.


Exactly, I print it after it has arrived and setting _rpm within processNumber.

Nick Gammon


Exactly, I print it after it has arrived and setting _rpm within processNumber.


You are printing _rpm every time through loop. The time taken to do that (which uses interrupts) is likely to make you miss incoming data.

BTW I wouldn't be using leading underscores in variables names if I were you.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Maybe:

Code: [Select]

long _rpm = -1;

...

    if (_rpm != -1)
      {
      Serial.println(_rpm);
      _rpm = -1;
      }


Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

matinzk

This works beautifully thanks.

However if I have a set of extra data to also send via another serial port (Serial 3) to a device using a Bluetooth module, then I get missing data again.

So I receive the _rpm from first Arduino to my second Arduino Mega, then I send _rpm with 3 more data to Serial3 and receive these on my phone, the _rpm data is sometimes missing as before

Nick Gammon

Why are you doing it that way? Why not do the serial print in the processNumber function, which is the way that works anyway?

Quote
However if I have a set of extra data to also send via another serial port (Serial 3) to a device using a Bluetooth module, then I get missing data again.


Post this new code please.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

matinzk


Go Up