Go Down

Topic: How to send numbers from one Arduino to another using Serial (Read 4 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?

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.

Go Up