Arduino SoftwareSerial porst stops sending data after a time

I am not sure if this is the right place, but I guess so..

I have 3 Arduino UNO on the desk now, one of them is attached to 2 different HCSR04 ultrasonic sensor.
The other one is connected to first one by a serial connection(I have 2 cables connects arduinos serial on digital pins 5 and 6) The last one work with a 9v batters, uses another HCSR04.

The idea is basically using the onee with battery as a transmitter, using others as emitter and measure distance diifference between two sensors on the first arduino with the remote one. Then, converting this integer type info to char array and send it by serial connection to another arduino. :slight_smile:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 6);

unsigned long time;

char c = ' ';
int u[5];
boolean NL = false;
int sayi;
int sayac=-1;
//char

void setup() {
 Serial.begin(38400);
 BTSerial.begin(38400);
}

void loop()
{


  if (BTSerial.available())
  { 
    c = BTSerial.read();
    if(c=='k')
    {Serial.print("  ");}
    if(c!='h'&&c!='k')
   { Serial.write(c);}}
   /* if(c=='h')
    {sayac++;}
    if(c=='k')
    {NL=true;}
    if((c>=48&&c<=57)&&sayac>=0)
    {u[sayac]=c-48;
    sayac++;
    }
    if(sayac>0&&c=='A')
    { Serial.print("enter");
      Serial.print(sayac);
      Serial.print(u[0]); 
      if(sayac==2)
        {sayi=u[0]*10+u[1];}
        if(sayac==3)
        {sayi=u[0]*100+u[1]*10+u[2];}
        if(sayac==4)
        {sayi=u[0]*1000+u[1]*100+u[2]*10+u[3];}
        }
  
   /* if(NL==true)
    {Serial.print("   ");
    Serial.println(sayi);
    }
    else if(NL=false)
    {Serial.print(sayi);
      }
    
        sayac=-1;
}*/
}

//}
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 6);
int veri1;
int veri2;
char c=' ';
 char tt[5];
// char jk='A';
  char tt2[5];
void setup() {
 pinMode(9, INPUT);
 pinMode(11, INPUT);
 pinMode(10, OUTPUT);
 pinMode(12, OUTPUT);
 BTSerial.begin(38400);
 Serial.begin(38400);
 long duration, distance;
  digitalWrite(10, LOW);
  delayMicroseconds(2);
  digitalWrite(10, HIGH);
  delayMicroseconds(10);
  digitalWrite(10, LOW);
  duration=pulseIn(5, HIGH, 20000);
//Serial.println("basla");
}

long mesafe()
{
  long duration3, distance3;
  digitalWrite(10, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(10, LOW);
  duration3=pulseIn(9, HIGH);
  distance3=duration3/58.2;
  //BTSerial.print(distance3);
  //BTSerial.print("  ");
  return distance3;
  }
 long mesafe2()
{
  long duration2, distance2;
  digitalWrite(12, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(12, LOW);
  duration2=pulseIn(11, HIGH);
  distance2=duration2/58.2;
  //BTSerial.println(distance2);
 // Serial.print("  ");
 return distance2;
  } 
void arranger(int olcumm, int yklhm)
{
int olcum=olcumm;
 
  char sayavc;
  if(olcum>=1000)
  {sayavc=3;}
  if(olcum>=100&&olcum<=1000)
  { tt[3]='A';
  sayavc=2;}
  if(olcum<100)
  {tt[3]='A';
  tt[2]='B';
    sayavc=1;
  }
  
  while(olcum>=1&&yklhm==1)
  {
    int f=(olcum%10);
    tt[sayavc]=48+f;
    sayavc--;
    olcum=olcum/10;
    }
   while(olcum>=1&&yklhm==2)
  {
    int f=(olcum%10);
    tt2[sayavc]=48+f;
    sayavc--;
    olcum=olcum/10;
    }

//int pl=strlen(tt);  
//Serial.println(tt);
/*int i;
BTSerial.write("h");
for(i=0;i<=4;i++)
{c=tt[i];
  BTSerial.write(c);
}*/
//BTSerial.write("\n");
  
  
  
  
    
  
  }

void loop() {

 veri1=mesafe();
 veri2=mesafe2();
 int sayak=1;
  arranger(veri1, sayak);
  sayak++;
 // tt[4]='A';
 // tt2[4]='A';
  arranger(veri2, sayak);
  BTSerial.write("h");
  BTSerial.write(tt);
  BTSerial.write("k");
  BTSerial.write(tt2);
  BTSerial.println("  ");
//  BTSerial.write("g");
 // int fuck=strlen(tt2);
 // Serial.print(tt);
  //Serial.print("  ");
  //Serial.println(tt2);
 // for(int i=0;i<=4;i++)
//  {Serial.print(tt[i]);}
 // Serial.print("  ");
 //Serial.println(fuck);
 /*sayac++;
 if(timmes>=8000)
 Serial.println(sayac);*/

}

The issue is, serial port between two arduinos stopped working. Nothing on computer COM screen, the rx LEDs on both Arduinos does not light up, nothing... That happened after a.. 30-40 minutes I guess..

I uploaded the codes again, did not help. I unplugged and plugged arduinos, did not help :confused:

At the end, I uploaded another code first, then upload these codes and then ıt works :slight_smile:

I need this code works at least a daytime without any human calibration. That means I have to prevent this happen again.

I checked some similar topics, but the ones I found did not include any SoftwareSerial.

I think this might be about, but I am not sure

Why doesn't my sketch start when I power up or reset the Arduino board?

Most likely because you are sending serial data to the board when it firsts turns on. During the first few seconds, the bootloader (a program pre-burned onto the chip on the board) listens for the computer to send it a new sketch to be uploaded to the board. After a few seconds without communication, the bootloader will time out and start the sketch that's already on the board. If you continue to send data to the bootloader, it will never time out and your sketch will never start. You'll either need to find a way to stop serial data from arriving for the first few seconds when the board powers (e.g. by enabling the chip that sends the data from within your setup() function) or burn your sketch onto the board with an external programmer, replacing the bootloader.

It might be also the code which is thee problem.

As a beginner level programmer and arduino user, I have no idea which might be the reason and I don't know how to find it out. :frowning:

Thanks...

"(I have 2 cables connects arduinos serial on digital pins 5 and 6)"

Do you also have Ground connected between the two Arduinos? It is necessary.

Aaa no.. I didn't know that :confused: