Arduino Delay not working properly

Hello, I am currently programming a sensor which will take measurements over a long period of time and I need to delay the measurements. Whenever I add a delay the code will either stop working, or it will delay the writing of measurements to the serial monitor. I am working with an Atlas Scientific Dissolved Oxygen sensor with an Arduino Duemilanove AT mega 328.

My current code :

#include <SoftwareSerial.h> 
#define rxpin 2 
#define txpin 3 
SoftwareSerial myserial(rxpin, txpin);
String inputstring = "";
String sensorstring = "";
boolean input_stringcomplete = false;
boolean sensor_stringcomplete = false;
void setup(){ 
 Serial.begin(38400); 
 myserial.begin(38400); 
 inputstring.reserve(5); 
 sensorstring.reserve(30); 
 }
void serialEvent() { 
 char inchar = (char)Serial.read(); 
 inputstring += inchar; 
 if(inchar == '\r') {input_stringcomplete = true;} 
 } 
void loop(){ 
 if (input_stringcomplete){ 
 myserial.print(inputstring); 
 inputstring = ""; 
 input_stringcomplete = false;
 }
while (myserial.available()) {
 char inchar = (char)myserial.read(); 
 sensorstring += inchar; 
 if (inchar == '\r') {sensor_stringcomplete = true;} 
 }
if (sensor_stringcomplete){ 
 Serial.println(sensorstring); 
 sensorstring = ""; 
 sensor_stringcomplete = false; }
}

The simple answer is "don't use delay" but from the look of your sketch, you've already arrived at that conclusion.

I removed the delay since I literally put it in between every line of code individually to see if it worked and it didn't. I was hoping someone would reply with a suggestion of where to put it/ what form to be in. I used:

delay (5000);

I don't necessarily need a delay, but I do need to limit the amount of data points are obtained. I thought the delay would be easiest, but it won't work properly. I have limited storage and I will later add a time stamp for when a measurement is taken, so I can't have it taken every second.

I expect that delay() is working but not doing what you want. How long is the 'delay' that is needed between readings ?

For testing it will be 5 seconds, but in practice it will need to be around one hour. Half the time we use a delay, it will take all the measurements, but not print them until 5 seconds has passed. Then it prints 5 measurements at once. The other times no measurements are taken.

There are no delay()s in the code you posted. We can't guess where you put them, so we can't explain why they are causing the issues you see.

What do you want to have 5 seconds/minutes/hours/days apart? Reading from the sensor? Writing to the serial port?

Sorry for the ambiguity, but we tried placing a delay in many parts of the code, but more specifically after the serial.print because it makes logical sense to print the value every five seconds, but these numbers are compounding and printing simultaneously in every five second interval.

if (sensor_stringcomplete){ 
 Serial.println(sensorstring); 
 delay(xxxx);
 sensorstring = ""; 
 sensor_stringcomplete = false; }
}

We wanted to take measurements from the sensor every five seconds(not write to serial port) as that would save energy, since this entire assembly will function as a data logger.

I don't see why you are reading from serial in 2 different places in the code. What is the purpose of the sensorstring and inputstring variables ?

I would have thought that you would have done something like this

start of loop
  if serial data available
    read input and add to string until '\r'
    print the data
  end of if
  wait for 5 seconds
end of loop

This would loop until data is available, read it, print it, wait 5 seconds then do it all again. If you need to do anything else in the loop function then you will need to use millis() to do your timing.

You could put in a delay on just the output routine and let the rest of the code run normally.

#define DELAY  5000	// 5000 mS or 5 second for delay
unsigned long _last_sent;

void setup() {
	_last_sent = millis();
}

void loop() {
	// normal sensor read code
	
	if (millis() - _last_sent >= DELAY) {
		_last_sent = millis();
		Serial.print("output data on 5 second delay");
	}
}