Hi! I've run into this problem while using an Arduino Mega, a pH reading stamp, and an LCD (all communicate via serial). Basically, the pH stamp gets its measurements, and by SerialEvent, it send it to the Arduino, which then prints the readings to the LCD. This works fine, but at random times, the Arduino restarts.
I've read a few pages about this auto-restart issue, and the two common fixes I've seen are to either disable DTR on your computer, or to physically break the connection to the reset pin (cutting a trace or removing a capacitor). The thing is that I'm running the Mega on an external 7V input, so I don't see the DTR-fix working for me. And as much as possible, I want to leave the second option as a last resort.
Is it possible that something in my code can force a restart of the Arduino?
This bit is outside the loop:
void serialEvent1() {
char inchar = (char)Serial1.read();
temppH2 += inchar;
if(inchar == '\r' && Menu=="0") {
armCond = true;
}
}
void serialEvent2() {
char inchar = (char)Serial2.read();
tempCo2 += inchar;
if(inchar == '\r' && Menu=="0") {
armCond = true;
}
}
void serialEvent3() {
char inchar = (char)Serial3.read();
tempCo1 += inchar;
if(inchar == '\r' && Menu=="0") {
armCond = true;
}
}
This one is within the loop (was using softwareSerial, so I couldn't use a Serial Event anymore):
if(armCond==true && Menu=="0" && Main==false) {
lcd.setCursor(12,1);
lcd.print(tempCo1.substring(0,tempCo1.indexOf(',')));
//lcd.print(" ");
lcd.setCursor(12,2);
lcd.print(tempCo2.substring(0,tempCo2.indexOf(',')));
//lcd.print(" ");
tempCo1="";
tempCo2="";
lcd.setCursor(12,5);
lcd.print(temppH1);
//lcd.print(" ");
lcd.setCursor(12,6);
lcd.print(temppH2);
//lcd.print(" ");
temppH1="";
temppH2="";
armCond=false;
}
while(Serial0.available()) {
char inchar0 = (char)Serial0.read();
temppH1 += inchar0;
if(inchar0 == '\r' && Menu=="0") {
armCond = true;
}
}
btw, '\r' is the carriage return the stamp sends once it has data ready to send
Thank you very much for your time