Serial Communication issues...

Hello, I’m new to this!

Is there a kind stranger that could help me understand the problem I’m having when trying to communicate between my two arduinos?
I’m trying to communicate between the two and they work perfectly fine with their Serial Monitors when they are not connected, but for some reason when I connect the Rx and Tx and grounds, they only complete one task and then stop. I’m really dumbfounded as to what could be going wrong.

Someone mentioned that perhaps they are stuck in a loop within the “echo” command I have, but after I hid that part of the code the problem persisted! Here are the two separate codes I have for each arduino.

Arduino Number One

int pinHappy = 2;

int Stress; // pressure
int Tears; // light

int wasStressed; 
int wasTears;

long timeSinceStressed;
long timeSinceTears;


void setup() {
  Serial.begin(9600);
   pinMode(A5,INPUT); 
   pinMode(A1,INPUT);
   pinMode(pinHappy,OUTPUT);
}

void loop() {
 
  int Stress = analogRead(A5); // variable stored for light sensor
  int Tears = analogRead(A1); // variable stored for potentiometer 
  
  processSerial();
    
  if(Stress < 800 && wasStressed == false) {  
    
    Serial.println("Stress"); // prints the data
    wasStressed = true; 
    timeSinceStressed = millis();
  }
  
  else {
  }
  
    if(Tears < 1000 && wasTears == false) {  
    
    Serial.println("Tears"); // prints the data
    wasTears = true; 
    timeSinceTears = millis();
 
  }
  
  else {
  }
  
  if(wasStressed == true && timeSinceStressed < (millis() - 1000)) {
  wasStressed = false;  
  }
  
  if(wasTears == true && timeSinceTears < (millis() - 1000)) {
  wasTears = false; 
  }
  
}




void handleSerialMessage(String msg) {

 // Serial.print("Echo:");
  Serial.println(msg);
  
   if(msg == "beHappy") {
    
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200); 
    
    digitalWrite(pinHappy,HIGH);
    delay(1000);
    digitalWrite(pinHappy,LOW);
    delay(200);
  
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200);
  
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200);
  
    digitalWrite(pinHappy,HIGH);
    delay(1000);
    digitalWrite(pinHappy,LOW);
    delay(200);
    
    digitalWrite(pinHappy,HIGH);
    delay(1000);
    digitalWrite(pinHappy,LOW);
    delay(200);
    
    digitalWrite(pinHappy,HIGH);
    delay(1000);
    digitalWrite(pinHappy,LOW);
    delay(200);
   
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200);
  
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200);
    
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);
    delay(200);
   
    digitalWrite(pinHappy,HIGH);
    delay(1000);
    digitalWrite(pinHappy,LOW);
    delay(200);
    
    digitalWrite(pinHappy,HIGH);
    delay(200);
    digitalWrite(pinHappy,LOW);

  
  } else {
    Serial.println(" "); 
  }
}
// DO I NEED THIS??

char buffer[256];
int bufferCounter = 0;

void processSerial() {
  while (Serial.available() > 0) {

    //read the next available character...
    char newChar = Serial.read();

    if (newChar == '\n' || newChar == '\r') { //\n is the same as NewLine, ASCII value 10. \r is Return, ASCII value 13
      //if we received a return character, it's time to process the message.

      //start by adding a 0 to the end of the current values in the buffer
      //(0 is a standard string terminator)
      buffer[bufferCounter] = 0;

      //now call the function that handles receiving messages (unless it was an empty message).
      if (bufferCounter > 0) {
        handleSerialMessage(buffer);
      }

      //finally, now that the message is handled, start recording into the beginning of the buffer.
      bufferCounter = 0;
    } 
    else {
      //if we received any other character, add it to the buffer, and increment the buffer counter.
      buffer[bufferCounter] = newChar;
      bufferCounter++;

    }    
  }
}
int stringToInt(String inputString) {
  char buf[16];
  inputString.toCharArray(buf, 16);
  return atoi(buf);
}

Arduino Number Two

int pinTears = 10;
int pinStress = 9;
int beHappy;

int wasHappy;
int timeSinceHappy;

void setup() {
  Serial.begin(9600);
  
  pinMode(pinTears,OUTPUT);
  pinMode(pinStress,OUTPUT);

  
}

void loop (){

   processSerial();
    beHappy = digitalRead(12);
    
   // Serial.println(beHappy);
    
    if(beHappy > 0 && wasHappy == false) {  
    
    Serial.println("beHappy"); // prints the data
    wasHappy = true; 
    timeSinceHappy = millis();
 
  }
  
  if(wasHappy == true && timeSinceHappy < (millis() - 1000)) {
  wasHappy = false;  
  }
 
}

void handleSerialMessage(String msg) {

 // Serial.print("Echo:");
  Serial.println(msg);
 
  if(msg == "Stress") {
    
    digitalWrite(pinStress,HIGH);
    delay(250);
    digitalWrite(pinStress,LOW);
  
  } else {
    Serial.println(" "); 
  }
  
    if(msg == "Tears") {
  
    digitalWrite(pinTears,HIGH);
    delay(250);
    digitalWrite(pinTears,LOW);
    
  } else {
      Serial.println(" ");
  }
  
}



// DO I NEED THIS??

char buffer[256];
int bufferCounter = 0;



void processSerial() {

  while (Serial.available() > 0) {

    //read the next available character...
    char newChar = Serial.read();

    if (newChar == '\n' || newChar == '\r') { //\n is the same as NewLine, ASCII value 10. \r is Return, ASCII value 13
      //if we received a return character, it's time to process the message.

      //start by adding a 0 to the end of the current values in the buffer
      //(0 is a standard string terminator)
      buffer[bufferCounter] = 0;

      //now call the function that handles receiving messages (unless it was an empty message).
      if (bufferCounter > 0) {
        handleSerialMessage(buffer);
      }

      //finally, now that the message is handled, start recording into the beginning of the buffer.
      bufferCounter = 0;

    } 
    else {

      //if we received any other character, add it to the buffer, and increment the buffer counter.
      buffer[bufferCounter] = newChar;
      bufferCounter++;

    }    
  }
}


int stringToInt(String inputString) {
  char buf[16];
  inputString.toCharArray(buf, 16);
  return atoi(buf);
}

I’d really appreciate any help! I’m having the worst trouble with this but I’m sure the answer isn’t complicated. Thanks so much for anyone who takes the time to help me understand :slight_smile:

Are you trying to cross-connect the serial ports from the Arduinos?

Each serial port can only talk to one device at a time, so if you connect it to the serial monitor then you can't also connect it to the other Arduino. If your Arduinos only have a single physical serial UART and you want to be able to connect using the serial monitor and also connect them to each other, then your best bet is to use the physical serial UART to connect via USB to the serial monitor, and use a software serial port on two other digital pins to establish a serial link to the other Arduino.

PeterH:
Are you trying to cross-connect the serial ports from the Arduinos?

Nope - I am using batteries for each arduino after I upload the sketches. So I don't think that's what is wrong but thanks so much for taking the time to answer! Got any other ideas as to what is going wrong?

Everytime one Arduino receives a message, it echo it back to other, which then gets echoed back to the first one, then echoed back to the seconds, etc...

Based on your code, it appears that you don't want to message to be sent back and forth continuously.

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).