Stumped!

Hi all

So I`m very new to Arduino, I love the idea behind this thing as I thought it would be a great use to me, as I grow insect eating plants which require regular misting to keep the air moist, but not to soak the plants.

So I had a look around at a few sketches, a humidity controller with LCD display and will trigger a relay to operate whatever at below the set thresh hold!, great and that worked well.

At least I thought it did, my pump would be on for a very long time and everything was getting drenched, way too much water.

So I then decided to buy a garden timer which would turn on the water for say 2 mins every hour between 9am and 6pm, this worked, but I need a feed from this to turn on my pump at the same time!, so it was getting more complicated and rather messy.

So I then decided wait, I have a micro-controller the Arduino, it must be able to provide timed outputs as well?

So I have been experimenting and getting nowhere fast, I decided on a real time clock and was just getting too frustrated with not even the examples working, I must have a duff board.

So I set my self a not too complicated task, keep the original humidity controller, but when the output goes on, not to directly turn on a relay but to start the cyclic timer which will then control the relay, till its completed say 10 cycles or the humidity is back above the thresh hold level.

So my problem is, once I have an output turn on, how can it start the second piece of code?

I have a feeling rather than the direct relay option the code needs to go here somehow, but I could be wrong!

if (h < humLowTrigger) //if the humidity lower than the trigger value
digitalWrite(relay2, LOW); //start humidification

All thoughts appreciated along with my two items of code.

Cheers Alan

#include <Timer.h>
#include <LiquidCrystal_I2C.h>
#include <DFR_Key.h>
#include "DHT.h"



#define humLowTrigger 80 //Setting the trigger value for the humidity, once the humidity lower than this value, start misting
#define DHTPIN 7    
#define DHTTYPE DHT22  
#define LED 13  


const int relay2 =  A2;      // humidity control relay pin

DHT dht(DHTPIN, DHTTYPE);


//Pin assignments for DFRobot LCD Keypad Shield
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//---------------------------------------------

DFR_Key keypad;

int localKey = 0;
String keyString = "";

void setup()
{

   

   pinMode(relay2, OUTPUT);

   pinMode(LED, OUTPUT);

   
   lcd.begin(16, 2);
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("initializing...");
   lcd.setCursor(0, 1);
   delay(1000);

   Serial.begin(9600);
   dht.begin();
   delay(1000);
   lcd.clear();

   /*
     OPTIONAL
     keypad.setRate(x);
     Sets the sample rate at once every x milliseconds.
     Default: 10ms
   */
   keypad.setRate(10);

   digitalWrite(relay2, HIGH);

}

void loop()
{



   // Reading temperature or humidity takes about 250 milliseconds!
   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
   float h = dht.readHumidity();
   float t = dht.readTemperature();

   // check if returns are valid, if they are NaN (not a number) then something went wrong!
   if (isnan(t) || isnan(h)) {
       Serial.println("Failed to read from DHT");
     
  } else {

       //Display
       lcd.setCursor(0, 0);
       lcd.print("Hum: ");
       lcd.print(h);
       lcd.print(" %");

       lcd.setCursor(0, 1);
       lcd.print("Tem: ");
       lcd.print(t);
       lcd.print(" *C");

       if (h < humLowTrigger) //if the humidity lower than the trigger value
           digitalWrite(relay2, LOW); //start humidification
       else
           digitalWrite(relay2, HIGH);

       
       
       
  }
   
     

     
}


 
----------------------------------------------------------------------------------------------------------------------------------------------------------------



const int relayPin =  11;
unsigned int relayState = LOW;
long offTime = 10000; //Pump off time
long onTime = 100;  // Pump on time
unsigned long previousMillis = 0;
void setup() {
   // put your setup code here, to run once:
   pinMode(relayPin, OUTPUT);
   relayState = LOW;
   digitalWrite(relayPin, LOW);  
}
void loop() {
   // put your main code here, to run repeatedly:
   long currentMillis = millis();
   if ((relayState == HIGH) && (currentMillis - previousMillis >= offTime))
      {
       relayState = LOW;// turn it on
       previousMillis = currentMillis;   // Remember the time
       digitalWrite(relayPin, LOW);    // Update the actual relay
  }
  else if ((relayState == LOW) && (currentMillis - previousMillis >= onTime))
      {
       //digitalWrite(RELAY2, LOW);
       relayState = HIGH;   // Turn it off
       previousMillis = currentMillis;  // Remember the time
       digitalWrite(relayPin, HIGH);  // Update the actual relay
     
  }
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading,

...R

Now that I can study the code in your Original Post it seems to contain two separate programs. If you are hoping that will work, then it won't.

You need to include the extra functionality in the original program.

It will be much easier to conceptualize the problem if you break the program into a number of functions - for example the code in loop() might be

void loop() {
 checkHumidity();
 checkIfSprayOnTooLong();
 operateSpray();
}

Have a look at how millis() is used to manage timing in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

Also look at Planning and Implementing a Program

...R