I've built a small project that uses an Arduino Mega2560, a Freetronics 2004 shield, an Arduino Ethernet Shield 2, and a Weatherproof Ultrasonic Sensor w/ Separate Probe (RB-Dfr-720) to monitor a 2,600 gallon water tank, determine the water level, and then update a MySQL DB table with the data it gathered. This thing will run 24x7x365 and I need it to be stable. I live off grid and the tank, which serves my small house, is 1,300 feet away, so I don't want to be hiking there all time to check on things or reset the set up.
I've been working thru various issues with this project (LCD conflicting with ethernet, sensor stability issues (now using NewPing), my learning curve, etc) but I think I've now got all devices working and am in the code refactoring phase for stability and efficiency.
Here's the deal... I've set the system up in my house and just let it run for hours/days to check on stability. But, I get random hangs where the system LCD displays "Reading Sensor" (my display logic) and just stalls there. I assumed it was still stability issues with the sensor, even though NewPing is supposed to address that issue. Anyway, in trouble shooting I added a slew of other LCD display writes to indicate where I am in the code, and have found that the sensor seems to hang fairly consistently on a new debug display statement I added "Writing to DB". I'm now looking at the code and am wondering if my program is overflowing memory and creating more or less random fails.
I'd assumed that the Arduino IDE wraps my code in a loader that did some memory management, but obviously that's not the case. So after that long preamble here's the question...can anyone help me understand if lots of LCD display statements and/or my MySQL cursor create and insert logic is blowing thru the Flash memory.
Here's the MySQL insert logic. I assumed that my Delete Cursor statement was clearing Flash but maybe it's not. And I also understand that strings take up a fair amount of SRAM and maybe because of all my LCD writes I'm blowing that out. Any memory management tips or tutorials you can refer me to would be great. Or, if you see a stupid problem in my DB write code hit me with your feedback. I have thick skin so will take all responses as helpful. Thanks...
if (priorgallons != gallons) {
lcd.setCursor( 0, 0 );
lcd.print("Writing To DB ");
lcd.setCursor( 0, 1 );
lcd.print(" ");
char INSERT_SQL[] = "INSERT INTO welldb.well_tank (inches, gallons) VALUES (%d, %d);";
char query[255];
sprintf(query, INSERT_SQL, waterheight, gallons);
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Delete the cursor to free up memory
delete cur_mem;
priorgallons = gallons;
lcd.setCursor( 0, 0 );
lcd.print("DB Write Done ");
lcd.setCursor( 0, 1 );
lcd.print(" ");