Hi everyone
I have read a thousand and one posts, followed a bunch of youtubers and copied a bunch of tutorials. Seems like I just don't have the capacity any more to figure this out on my own even with the WWW full of tutorials and how to's.
I have an Arduino Nano, DHT11 sensor and a 16ch relay, low level Triger.
I need it to, well lets go with only the measuring of the temperature for now and if the temp. is above 25°C the relay must switch on for 20seconds and then stay closed for 20 minutes.
This is to spray the water curtain to cool a greenhouse. I can not let is keep the valve open as this will flood my greenhouse. same will be implemented for the humidity
The "HIGH" and "LOW" in the sketch is inverted as I am using an LED to simulate the relay before connecting everything to go to work.
Apparently I cant even figure out how to attach my sketch so Ill just past it in here.
This was the final attempt and the closest I could get to the result I am looking for. The "delay" will be removed and the Serial.prints this was only to try and make sense of what the heck I am trying to do.
Also a 16x2 I2C LCD will be added to display the current temperature and humidity.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht (DHTPIN, DHTTYPE);
int State = 0;
const int maxTemp (28);
const int mistV (3); // Pin for the misting valve relay
// This is supposed to control the time the valve will stay closed
unsigned long CurrentClose = 0;
unsigned long PreviousClose = 0;
const int StayClosed = 1000*10; // 1000*1 = 1 second
// This is to control the time the valve will stay open
unsigned long CurrnetOpen = 0;
unsigned long PreviousOpen = 0;
const int StayOpen = 1000*5; // 1000*1 = 1 second
void setup() // Start up the everything
{
dht.begin();
Serial.begin(9600);
pinMode (mistV, OUTPUT);
}
void loop() // Type of declarations of the State machine
{
delay (1000);
mistValve();
Serial.println(State);
}
// Open the mist valve for "X" and keep closed for "Y" when Max Temp was exceeded
void mistValve()
{
switch (State)
{
// Reset (0)
case 0:
{State = 1;}
break;
// (1) Take Temperature reading. If temp is over Max Temp chack time at (2) to see if it may open the valve
case 1:
{
float t= dht.readTemperature();
Serial.println (t,1);
if (t >= maxTemp)
{
State = 2;
}
}
break;
// (2) Check the Closed valve timer to see if the valve may open. if true open valve at (3)
case 2:
{
unsigned long CurrentClose = millis();
if (CurrentClose - PreviousClose > StayClosed)
{
PreviousClose = CurrentClose;
State = 3;
}
}
break;
// (3) Open the valve and check duration at (4)
case 3:
{
digitalWrite (mistV, HIGH);
Serial.println( "Valve Open");
State = 4;
}
break;
// (4) Check the Open valve timer and close when time runs out
case 4:
{
unsigned long CurrnetOpen = millis();
if (CurrnetOpen - PreviousOpen > StayOpen)
{
PreviousOpen = CurrnetOpen;
State = 5;
}
}
break;
// (5) The time the valve will stay closed (5)
case 5:
{
digitalWrite (mistV, LOW);
Serial.println( "Valve Closed");
State = 0;
}
break;
}
}