Perform a task DURING a certian time period/delay

Hi!!

Hope all of you are doing great!! :slight_smile:

Well I am really struggling with timings in Arduino code... Hope I can get some Idea... Here's my code:

#include <Wire.h>
int in1Pin = 10;
int in2Pin = 11;
double Temperature;
double Humidity;
#define HYT221_ADDR1 0x28
void setup(){
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  Serial.begin(9600);
  Wire.begin(); 
}
 void read_HumTemp(){
    // initiate measurement
  Wire.beginTransmission(HYT221_ADDR);
  Wire.write(0);
  Wire.available();
  int Ack1 = Wire.read(); // receive a byte
  Wire.endTransmission(); 
  delay(100); //This particular delay is the delay between the displayed values (in msec)...

// READ DATA from here on
  Wire.beginTransmission(HYT221_ADDR);
  Wire.requestFrom(HYT221_ADDR,4);  //Reade 1 byte
  Wire.available();
  int b11 = Wire.read(); // receive a byte
  int b21 = Wire.read(); // receive a byte
  int b31 = Wire.read(); // receive a byte
  int b41 = Wire.read(); // receive a byte
  Wire.write(0); //NACK

  // combine the bits
  int RawHumidBin = b11 << 8 | b21;
  // compound bitwise to get 14 bit measurement first two bits
  // are status/stall bit (see intro text)
  RawHumidBin =  (RawHumidBin &= 0x3FFF); 
  Humidity = 100.0/pow(2,14)*RawHumidBin;

  b41 = (b41 &= 0x3F); //Mask away 2 least sign. bits see HYT 221 doc
  int RawTempBin = b31 << 6 | b41;
  Temperature = 165.0/pow(2,14)*RawTempBin-40;

  Wire.endTransmission();
}
void loop(){
      digitalWrite(in1Pin, LOW);
      digitalWrite(in2Pin, HIGH);
      delay(30000);
for(int x=0; x<500; x++){        
        read_HumTemp();
        Serial.print(Humidity);
        Serial.print(",") ;
        Serial.println(Temperature);
        delay(10); 
   }
  }

So as a short description, I have an Arduino UNO, LD293D and HYT221 humidity sensor. A valve remains open through L293D chip for 30 seconds (delay). Then I need to close the valve for 5 sec (5000 msec) and DURING these 5 sec, read from the sensor every 10 miliseconds for 500 times (i.e- total is 5 seconds...)

This code is unfortunately not working... After the initial delay of 30 seconds, when the valve closes then it goes into infinite loop and never opens again... Whts wrong with this code...?? :frowning:

Maybe I am missing something silly or maybe there is something new I can try...

Some help would be greeatly appreciated!! :slight_smile:

Thank you so much!!

Pramit

There is no "infinite loop" in that code. But there is a honking long delay (500 * 100 * 10 mSec = 500 seconds, or about 8.3 minutes, after which everything repeats....

Regards,
Ray L.

Hey Ray!!

Thanks a ton for pointing out that out!! This has made the 5 sec almost exact... :slight_smile:

But somehow the 30 second delay is not so exact.. The valve closes not exactly after 30 sec.. but after 36-37 sec... Is using this delay() function a good idea?? Or one should use some other better library?

Regards

Pramit

Study the blink without delay sketch that comes with the IDE.
You want to use millis() to keep track of time and not use delay.

sood:
Then I need to close the valve for 5 sec (5000 msec) and DURING these 5 sec,

You CANNOT do anything during a delay().

If you need to do something while something else is happening you need to replace all the delay()s with millis() as illustrated in several things at a time

...R