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
}
}