Need help with Two arduinos talking to each other

I have been trying to compare scores from 7 seg displays to see which one is highest. I want to read in a number from one arduino like “567” and save it in a variable on the second arduino. Then if the score on the sending arduino is higher I’ll send it a command to make it blink. I’ve been trying to use an array to store the digits. The problem is: what if the score is just two digits or one digit? My array wants to record 3 digits. Also sometimes my arduino has timing issues. Instead of 567 I might get 675 in my debug window. In the end if I send 53 I want to save 53 and if I get 235 I want to store 235 in a single variable to print later. That is, if storeNums = 235 on this end storeNums = 235 on the other end as well. Does this make sense? It’s kind of hard to explain.

here’s what I have so far:

Aruino sending

#include <NewSoftSerial.h>

/*****************************************
*          Initialize Pins               *
*****************************************/
#define rxPin 7
#define txPin 19
NewSoftSerial mySerial(rxPin, txPin);
//void done();


/*****************************************
*          Setup                         *
*****************************************/

void setup()
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
}

/*****************************************
*          Main Loop                     *
*****************************************/

void loop()
{
  int pnts = 5;
  mySerial.print(pnts);
  delay(1);
}

/******************************************
*         Funtion Definitions             *
******************************************/

Arduino receiving

#include <NewSoftSerial.h>

/*****************************************
*          Initialize Pins               *
*****************************************/

#define rxPin 7
#define txPin 19
#define arraySize 3
NewSoftSerial mySerial(rxPin, txPin);
int digits[arraySize];
int storeNums;
int i = 0;

/*****************************************
*          Setup                         *
*****************************************/

void setup()
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);
}

/*****************************************
*          Main Loop                     *
*****************************************/

void loop()
{
  while(mySerial.available() > 0 && i < arraySize -1)
  {    ///mySerial.flush(); 

    for (i = 0; i < 3; i++)
    {
      storeNums = mySerial.read();
      digits[i] = storeNums;
      //digits[i] = '\0';
      delay(1000);
          Serial.println(storeNums -48);
    }

    /*Serial.print(digits[0] -48);
    Serial.print(digits[1] -48);
    Serial.println(digits[2] -48);
    Serial.close();*/
    
  } 
//Serial.println(storeNums -48);
}

/******************************************
*         Funtion Definitions             *
******************************************/

What you need to do is send a start character, the data, and a stop character. That way the receiver knows where a valid block of data is.

For instance, send ‘<’, ‘2’, ‘3’, ‘5’, ‘>’.

If the receiver sees ‘5’, ‘>’, ‘2’, ‘6’, ‘7’, ‘>’, it can tell that it did not receive a complete string, and ignore the incomplete string.

On the receiver, store the data as an integer, rather than a string, something like this:

int otherScore = 0;
boolean started = false;
boolean ended = false;

void loop()
{
   while(Serial.available() > 0 && !ended)
   {
      char digit = Serial.read();
      if(started)
      {
          otherScore *= 10;
          otherScore += digit - '0';
      }
      else if(char == '<')
         started = true;
      else if(char == '>')
         ended = true;
   }

   if(ended)
   {
       // Got a score (as an int). Do something with it...
}

I think I’m sending the numbers incorrectly. My program never sees the start character. The compiler would not except the comma separated argument, so I sent ‘<’, then 567, then ‘>’ on separate lines. The receiving Arduino only receives the last character. I put Serial.println(“here”) in the started condition and it never gets printed in the debug window. I guess I need help with the “sending” code.

here’s the receiving code. Is it correct?

void loop()
{
  while(mySerial.available() > 0 && ended != true)
  {

    char digit = mySerial.read();
      if(started)
      {
          otherScore *= 10;
          otherScore += digit - '0';
            Serial.println("here");
            delay(1000);
      }
    else if(digit == '<')
    {
      started = true;
    }
    else if(digit == '>')
    {
      ended = true;
    }

  }
  
   if(ended == true)
   {
      Serial.println("ended");
   } 
}

Here’s the sending code.

void loop()
{
  int pnts = 567;
  mySerial.print('<');
  mySerial.print(pnts);
  mySerial.print('>'); 
  delay(1);
}

I didn't mean to imply that you would send the data separated by commas. I was just showing the string of characters what was sent.

On the receiver, add Serial.print/Serial.println statements to echo the character read from the serial port.

The sending code looks fine.

I'm not sure what you mean buy adding the Serial.print statements to echo. where should I add them? I'm kind of a noob at this. I tried printing the content of digit but the started condition is still not working. I just get random symbols. Thanks for all of your help.

I got it to work. I had to sink them up. I was starting one arduino after the other. I really appreciate your patience. Have a wonderful day.

I have one more question. What exactly is this math doing?

otherScore *= 10;
otherScore += digit - '0';

It's shorthand for

otherScore = otherScore * 10; otherScore = otherScore + digit - '0';

There is a nice language reference by clicking "reference" at the arduino.cc site, then you can click more advanced topics such as "extended" and "libraries".

http://arduino.cc/en/Reference/IncrementCompound