Arduino's Serial feed times out ?

Okay here's the scenario.

I have 3 devices that sends CSV data on Serial1 , Serial2 and Serial3 of my Arduino Mega.

also have 4 current sensors on my AnalogRead A0,A1,A2,A3

and Lastly, I have 2, AC Rectified to DC inputs on A4 and A6 (My A5 Pin is burnt). A4 and A6 is energized with a Selector Switch(When A4 is High , A6 is low and V/V )

When energized, I open the Serial monitor to check the data and everything seems to be fine.

Then I set A4 to high which makes A6 low.

Serial Data updates.

Now I set A4 and A6 both to low

Serial Data updates

Now I set A6 high which makes A4 low

Serial Data updates

Now I set both to low again

Arduino Times out, Data stops.

This always happens when I set both to low. Somehow, something is disconnecting or disrupting the arduino but I have no idea on what is. I am hoping someone in the community has encountered this kind of problem.

My first thought is that the A4 and A6 inputs are causing the error. A4 and A6 are both from the same AC source but they are from a different rectifying circuit. (220VAC step down to 4.5VAC then rectified to 4.7VDC)

Any ideas guys?

Hi eraldino

Please post your code. Use the code tags (the '#' button above the row of smileys).

Also please post a schematic diagram of how all your hardware is connected. It can be a photo of a drawing, if that's easier.

Regards

Ray

Thank you for the fast reply
Here is the code:

boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
  String content;
  String content2;
  String content3;
    String phase1;
    String phase2;
    String phase3;
    String y;
    String d;
  int pc1;
  int pc1f=512;
  int pc2;
  int pc2f=512;
  int pc3;
  int pc3f=512;
  int wye;
  int deltz;
void setup() {
  // initiate lang ng data
  Serial.begin(9600); //baud rate 
  Serial1.begin(9600); //baud rate Serial1 
  Serial1.write(0x02); // Start transmission
  Serial1.print("M2"); // to send a setting of M2 = CSV
  Serial1.write(0x03); // End transmission
  
  Serial2.begin(9600); //baud rate  Serial2
  Serial2.write(0x02); // Start transmission
  Serial2.print("M2"); // to send a setting of M2 = CSV
  Serial2.write(0x03); // End  transmission
  
  Serial3.begin(9600); //baud rate Serial3 
  Serial3.write(0x02); // Start transmission
  Serial3.print("M2"); // to send a setting of M2 = CSV
  Serial3.write(0x03); // End  transmission
  
  // we initiated a csv setting   
}
#define C_STX 0x02
#define C_ETX 0x03


void loop() {
  //A0 = Phase AB
  //A1 = Phase BC
  //A2 = PHase CB
  //A3 = Neutral
  //A4 = Wye
  //A5 = Detlz
  //Line A = A0+A1
  //Line B = A2+A3
  //Line C = A0+A3
 
  pc1=analogRead(A0);
  if(pc1>pc1f)
  {pc1f=pc1;}
  
    pc2=analogRead(A1);
  if(pc2>pc2f)
  {pc2f=pc2;}
  
   pc3=analogRead(A2);
  if(pc3>pc3f)
  {pc3f=pc3;} 


  
  // put your main code here, to run repeatedly:
  if(flag1&&flag2&&flag3)
  
  {   wye=analogRead(A4);
  deltz = analogRead(A6);
    phase1 = String(pc1f);
    phase2 = String(pc2f);
    phase3 = String(pc3f);
    y = String(wye);
    d = String(deltz);
    Serial.print("A=");
    Serial.println(content+","+phase1+","+phase2+","+phase3+","+y+","+d);
    
    Serial.print("B=");
    Serial.println(content2);
      
    Serial.print("C=");
    Serial.println(content3);
       

  pc1=512;
  pc2=512;
  pc3=512;
  pc1f=512;
  pc2f=512;
  pc3f=513;
  y="";
  d="";
  content = "";
  content2 = "";
  content3 = "";
  phase1="";
  phase2="";
  phase3="";
  flag1=false;
  flag2=false;
  flag3=false;
  }
 
}


void serialEvent1()
{
  
 while(Serial1.available()) 
    { char c1 = Serial1.read();
    // ignore the STX
    if(c1 == C_STX)
    {//Serial.print("A=");
     continue;}
  
    if(c1 == C_ETX) {
      flag1= true;
      continue;}
    // print anything else
    content.concat(c1);
    }
}

void serialEvent2()
{
  
 while(Serial2.available()) 
    { char c2 = Serial2.read();
    // ignore the STX
    if(c2 == C_STX)
    {//Serial.print("B=");
    continue;}
  
    if(c2 == C_ETX) {
    flag2= true;
    continue;}
    // print anything else
    content2.concat(c2);
    }
}

void serialEvent3()
{
  
 while(Serial3.available()) 
    { char c3 = Serial3.read();
    // ignore the STX
    if(c3 == C_STX)
    {//Serial.print("C=");
    continue;}
  
    if(c3 == C_ETX) {
    flag3= true;
    continue;}
    // print anything else
    content3.concat(c3);
    }
}

I'll follow up the connection diagram.

Diagram

Thanks, eraldino.

Can you show how the other devices are connected to the serial ports?

And do the serial devices continuously send data to the Mega, or only when you operate the selector switch?

while(Serial1.available()) 
    { char c1 = Serial1.read();
    // ignore the STX
    if(c1 == C_STX)
    {//Serial.print("A=");
     continue;}
  
    if(c1 == C_ETX) {
      flag1= true;
      continue;}
    // print anything else
    content.concat(c1);
    }
}

For each serial port, the String content only gets reset to an empty String in loop() after ETX has been received on all serial ports. Is it possible that the external devices are sending at different rates, so one of the content strings is growing too large and running out of memory?

A possible fix would be to change each event handler so that the last part was like this (with the appropriate flag variable in each case)

    // print anything else
    if (!flag1)
    {
        content.concat(c1);
    }

This always happens when I set both to low.

This forum use English, in which language ALWAYS means every time NOT the second time or sometimes or after a while. Which is it!

This line of code

  if(flag1&&flag2&&flag3)

will cause your Arduinos stack to meet its heap and thus crash, if one or more of the serials stops!

Mark

Hackscribble:
Thanks, eraldino.

Can you show how the other devices are connected to the serial ports?

And do the serial devices continuously send data to the Mega, or only when you operate the selector switch?

The other devices are called "Power Analyzers" they are the ones mentioned in CSV they send a string of data every 1 second.
they operate continously independent of the selector switch

Hackscribble:

while(Serial1.available()) 

{ char c1 = Serial1.read();
    // ignore the STX
    if(c1 == C_STX)
    {//Serial.print("A=");
     continue;}
 
    if(c1 == C_ETX) {
      flag1= true;
      continue;}
    // print anything else
    content.concat(c1);
    }
}




For each serial port, the String `content` only gets reset to an empty String in `loop()` after ETX has been received on [u]all[/u] serial ports. Is it possible that the external devices are sending at different rates, so one of the `content` strings is growing too large and running out of memory?

A possible fix would be to change each event handler so that the last part was like this (with the appropriate flag variable in each case)



// print anything else
    if (!flag1)
    {
        content.concat(c1);
    }

Every serial port gets a new data every 1 second from each power analyzer there is a possibility that they might be out of sync but so far i have no problems with it.

BUT

The additional condition statement you said will help to stop overflow incase it happens and I will add it thank you for that.

The other devices are called "Power Analyzers" they are the ones mentioned in CSV they send a string of data every 1 second.

How big is the string sent every second?

The additional condition statement you said will help to stop overflow incase it happens and I will add it thank you for that.

As Mark @holmes4 said, it won't help if one of the power analysers stops sending. The program will get stuck waiting for the last power analyser to send an ETX.

But make the code change and see if it changes how the program behaves.