0
\$\begingroup\$

So I have an Arduino Uno and I've made a device to humidify my the enclosure for my Tortoise. The device consists of a temperature and humidity sensor, an LCD screen to display temperature and humidity and a water pump, which comes on if the humidity is below a certain point.

I set the program to loop every ten minutes, so as to not flood the enclosure with too much water but of course that means the LCD screen with only update every ten minutes too. I want the LCD screen to update every few seconds but the pump to only turn on 10 minutes after it's already been on if the humidity is still too low.

How can I do this?

Here is my code...

#include <dht.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;

#define DHT11_PIN 7
int pump = 13;

void setup(){
  pinMode(pump, OUTPUT);
  lcd.begin(16, 2);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  int hum = DHT.humidity;
  lcd.setCursor(0,0); 
  lcd.print("Temp: ");
  lcd.print(tmp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(hum);
  lcd.print("%");

  if(hum <= 70) {
    digitalWrite(pump, HIGH);
    delay(2000);
    digitalWrite(pump, LOW);
  }
  else{
    digitalWrite(pump, LOW);
  }

  delay(600000);
}

Thanks for your help! =]

\$\endgroup\$

closed as off-topic by brhans, Elliot Alderson, Dmitry Grigoryev, Bort, Finbarr Oct 4 '18 at 14:49

  • This question does not appear to be about electronics design within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Try the Arduino sites \$\endgroup\$ – Sunnyskyguy EE75 Sep 28 '18 at 13:33
  • 4
    \$\begingroup\$ thats more a programming question than an electrical. \$\endgroup\$ – PlasmaHH Sep 28 '18 at 13:38
  • 3
    \$\begingroup\$ Have a shorter delay and a counter. When the counter gets to a suitable value you run the code that runs the pump then reset the counter. \$\endgroup\$ – HandyHowie Sep 28 '18 at 13:39
  • 2
    \$\begingroup\$ I'm voting to close this question as off-topic because it's a general programming question. \$\endgroup\$ – Dmitry Grigoryev Oct 1 '18 at 6:52
  • \$\begingroup\$ Lookup "Blink without delay". It's an example code that should have came included with the Arduino IDE. The gist is that you should stop using delay() and instead have the micro check each loop if it's time to do something yet. \$\endgroup\$ – Bort Oct 1 '18 at 18:58
1
\$\begingroup\$

Instead of a long delay use a timestamp of the last time you ran the pump and compare it with millis():

// add to globals under pump

unsigned long lastRun = 0;


if(millis() - lastRun > 60*1000){
  if(hum <= 70) {
    digitalWrite(pump, HIGH);
    delay(2000);
    digitalWrite(pump, LOW);
    lastRun = millis();
  }
  else{
    digitalWrite(pump, LOW);
  }
}

This can be used in a lot of ways to avoid using delay() at all.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.