#include <DHT.h>
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
// hum = dht.readHumidity(); temp= dht.readTemperature();
unsigned long currentMillis;
unsigned long lightingMillis;//for timing grow lights
unsigned long soilMillis = (1000 * 60 * 60 * 6); //for timing soil sensor reads
unsigned long uvMillis;//for timing uv light
unsigned long solenoidMillis;//for timing water on duration
unsigned long tempMillis;
unsigned long lightCycle = (1000 * 60 * 60 * 12); //12on 12off
unsigned long soilCycle = (1000 * 60 * 60 * 12); //check soil every 12 hours
unsigned long tempCycle = (1000 * 10);
int soilMoistureSetpoint = 0;
int fanPin = 0; // for controlling fan relay
int lightingPin = 0; // for controlling lightinh relay
int solenoidPin = 0; // for controlling watering relay
int soilPinD = 0; // digital soil pin, for supplying vcc to soil sensor
int soilPinA = 0; // analog soil pin
int soilCapButPin = 0; // for a button to capture soil setpoint
int soilCapIndPin = 0; // for led to indicate soil setpoint capture
int uvPin = 0; // for controlling uv relay
void setup()
{
dht.begin();
pinMode (fanPin, OUTPUT);
pinMode (lightingPin, OUTPUT);
pinMode (solenoidPin, OUTPUT);
pinMode (soilPinD, OUTPUT);
pinMode (soilPinA, INPUT);
pinMode (soilCapButPin, INPUT);
pinMode (soilCapIndPin, OUTPUT);
pinMode (uvPin, OUTPUT);
}
void loop()
{
currentMillis = millis();
ControlGrowLights();//ready for test
ControlWateringSystem();//ready for test
ControlTemp();
}
void ControlGrowLights()
{
if (currentMillis - lightingMillis >= lightCycle)//if it has been 12 hours since the lights have been toggled
{
lightingMillis = currentMillis;//record current time for future light toggle
digitalWrite(lightingPin, !digitalRead(lightingPin));// toggle lights
if (digitalRead(lightingPin))
{
ControlUVLight();
}
}
}
void ControlWateringSystem()
{
CaptureSoilMoistureSetpoint();//checks for button press, if yes debounces and waters plant
if (currentMillis - soilMillis >= soilCycle) //if it has been 12 hours since the soil humidity has been measured
{
if (SoilMoisture() < soilMoistureSetpoint)//and if soil moisture is too low
{
WaterPlant();
}
soilMillis = currentMillis; //reset soil timer
}
}
int SoilMoisture()
{
digitalWrite(soilPinD, HIGH);
delay(25);
int soilReading = analogRead(soilPinA);
digitalWrite(soilPinD, LOW);
return soilReading;
}
void CaptureSoilMoistureSetpoint()
{
for (int x = 0; digitalRead(soilCapButPin); x++)
{
if (x = 50)
{
soilMoistureSetpoint = SoilMoisture();
digitalWrite(soilCapIndPin, HIGH);
delay(1000 * 3);
digitalWrite(soilCapIndPin, LOW);
WaterPlant();
}
}
}
void WaterPlant()
{
digitalWrite(solenoidPin, HIGH); //|
delay(1000 * 20); //// water the plant
digitalWrite(solenoidPin, LOW); ///
}
void ControlTemp()
{
int currentTemp = 0;
int tempHighSetpoint = 90;
int tempLowSetpoint = 75;
if (currentMillis - tempMillis >= tempCycle)
{
currentTemp = CheckTemp();
if (currentTemp >= tempHighSetpoint)
{
digitalWrite(fanPin, HIGH);
}
if (currentTemp <= tempLowSetpoint)
{
digitalWrite(fanPin, LOW);
}
tempMillis = currentMillis;
}
}
int CheckTemp()
{
int temp = dht.readTemperature();
temp = temp * 1.8 + 32; //converty to farenheit
return temp;
}
void ControlUVLight()
{
digitalWrite(uvPin, HIGH);
delay(1000 * 5);
digitalWrite(uvPin, LOW);
}
I am building an automated grow box. The code I have written is posted above. Just looking for someone to say "looks like that will work," or point out anything that looks like it won't work.
the main things I am trying to accomplish are:
provide a 12 on 12 off light cycle
turn on a uv light for 5 seconds everytime the light cycles switches to 12 on
monitor soil moisture with a simple two probe, DIY resistive soil moisture sensor
add water when needed
use a fan to ventilate the tent when temps get too high
set the soilMoistureSetpoint with a button press when plant needs watering for the first time
let me know if you want more info on the hardware end, I have written the code so I can assign the actual pin numbers as I wire everything together. I have a simple 4 chanel relay modual to control 120v power. I intend to hook those up to recepticals as I am using typical plug in household appliances. I will also use a threeway switch to bypass the lighting relay so i can have 24 hours on if needed.
thank you