Go Down

Topic: Arduino Delay not working properly (Read 1 time) previous topic - next topic


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 :
Code: [Select]
#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(){
void serialEvent() {
char inchar = (char)Serial.read();
inputstring += inchar;
if(inchar == '\r') {input_stringcomplete = true;}
void loop(){
if (input_stringcomplete){
inputstring = "";
input_stringcomplete = false;
while (myserial.available()) {
char inchar = (char)myserial.read();
sensorstring += inchar;
if (inchar == '\r') {sensor_stringcomplete = true;}
if (sensor_stringcomplete){
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


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:
Code: [Select]
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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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?
The art of getting good answers lies in asking good questions.


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.
Code: [Select]
if (sensor_stringcomplete){
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
Code: [Select]

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


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

Code: [Select]
#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");

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131