Pages: [1]   Go Down
Author Topic: Arduino Delay not working properly  (Read 425 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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; }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24293
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3451
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3451
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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");
}
}
Logged

Pages: [1]   Go Up
Jump to: