I use an Arduino Mega to measure, control and log dissolved oxygen values in fish tanks. To measure dissolved oxygen, I use two FireStingO2 optical oxygen sensors that are connected to the Arduino via Serial.
I’m a biology student and this is my first big project that involves coding and microprocessors, so my learning curve was very steep and the sketch looks - even to me - very convoluted and probably, there’s a lot of clutter in it.
The system has now been operating for over 8 weeks continuously and more or less successfully. However, there are several issues with this system that I would like to ask anybody interested to help me with.
Several sections of the code are hardcoded to fit my application (measurement with two sensors, using all 4 channels each), because I desperately had to get it to work. If anybody would like to give it a look and comment on how to make the code more adaptive to e.g. a different number of measurement channels , I’d be very grateful. Also, I simply copied the functions that use serial communication to switch between Serial1 and Serial2 on the Arduino Mega. Is there a better way to do this?
The program is designed to run 24h non-stop for 8 weeks. And it does work for the better part of the time but every now and then, there is a communication error between the Arduino and the sensor and I can’t wrap my head around why it happens. The basic principle is simple:
- Arduino sends measurement command “SEQ 1\r”
- Sensor echoes measurement command if received correctly - so far it has always worked
- Arduino sends readout command “REA 1 3 4\r”
- Sensor echoes readout command + value “REA 1 3 4 95682”
I included a communication check using strncmp() to compare outgoing and incoming data. And every now and then - at completely random timepoints - the sensor gets stuck echoeing the measurement command “SEQ 1” when it should report the values “REA 1 3 4 95682”
I tried everything I can think of to resolve this issue: clearing the char arrays for incoming serial data, sending only carriage returns “\r” to elicit an error message from the sensor and rebooting the Arduino using
void(* resetFunc) (void) = 0;
So far, only the reboot has solved the problem. And this is how I have been running this system for the last 8 weeks. However, I worry that these frequent reboots lead to problems (e.g. memory fragmentation) and quite frankly, I would really like to know what’s going on here.
For anybody who’s interested, I attached the arduino sketch, a schematic drawing of the whole system and a puTTY logfile in which the errors occurred.
arduino_sketch_multichannel.ino (36.7 KB)
logfile_abbreviated.txt (1.4 KB)