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.
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.
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.
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.
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");
}
}