Hello communitiy,
I am very new to the Arduino ecosystem. I recently started with a project for school to make an automated greenhouse with the following sensors:
2x water level sensor (I'm using two different reservoirs)
3x soil moisture
1x DHT 22 (Temp and Humidity)
1x LDR module (light intensity)
1x SD module to log temp, humidity and light intensity
1x 4 relay board of which I use 3 relays for pumps to turn on when the paired soil humidity sensor returns a low value
2x LED to indicate if the water levels are low
I really simplified timing because I find it quite difficult, so I just let my loop run every 30 minutes, in which the light intensity, humidity and temperature gets measured and logged, the water level gets checked and the soil moisture as well. That is where my problem lies. I want to turn the corresponding pump on for 10 seconds when one of the moisture sensors returns a low value. However I have no clue how to do this. I have attached my main file and a test file in which I try to get the timing right with leds to simplify the process.
I have been trying to make it work with the millis function but I don't think I get it. This is my test file:
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
int LedPin = 8;
int LedState = LOW;
int LedPin2 = 9;
int LedState2 = LOW;
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= 20000){
unsigned long SMillis = millis();
digitalWrite(9, HIGH);
int LightVal = analogRead(0);
Serial.println(LightVal);
if(LightVal > 800){
SMillis = currentMillis;
while(currentMillis - SMillis <= 10000){
digitalWrite(8,HIGH);
}
}
else{
digitalWrite(8,LOW);
}
previousMillis = currentMillis;
}
}
The led on pin 9 is meant as a test to make sure the pocess runs, but led 8 just turns on as well.
This is my full sketch:
//SD
#include <SD.h>
#include <SPI.h>
File sdcard_file;
const byte CS_pin = 10;
//DHT
#include <DHT.h>
#define DHTTYPE DHT22
const byte DHTpin = 3;
DHT dht(DHTpin, DHTTYPE);
//Water
const byte WledPin1 = 8;
const byte WledPin2 = 9;
int WledState1 = LOW;
int WledState2 = LOW;
//Soil
unsigned long previousMillis = 0;
const byte RelayPin1 = 4;
const byte RelayPin2 = 5;
const byte RelayPin3 = 6;
int RelayState1 = LOW;
int RelayState2 = LOW;
int RelayState3 = LOW;
//Variables
double LightVal;
double HumVal;
double TempVal;
void setup() {
Serial.begin(9600);
//DHT
dht.begin();
//SD
pinMode(CS_pin, OUTPUT);
if (SD.begin()){
Serial.println(F("SDReady"));
}
else{
Serial.println(F("SDError"));
return;
}
sdcard_file = SD.open(F("data.txt"), FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(F("Light"));
sdcard_file.print(F(" "));
sdcard_file.print(F("Temp"));
sdcard_file.print(F(" "));
sdcard_file.print(F("Hum"));
sdcard_file.println(F(" "));
sdcard_file.close();
}
//Water
pinMode(WledPin1, OUTPUT);
pinMode(WledPin2, OUTPUT);
//Soil
pinMode(RPin1, OUTPUT);
pinMode(RPin2, OUTPUT);
pinMode(RPin3, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= 60000){
//DHT
HumVal = dht.readHumidity();
TempVal= dht.readTemperature();
//Lichtsensor
LightVal = analogRead(A0);
//SD module
sdcard_file = SD.open(F("data.txt"), FILE_WRITE);
if (sdcard_file) {
sdcard_file.print(LightVal);
sdcard_file.print(F(" "));
sdcard_file.print(TempVal);
sdcard_file.print(F(" "));
sdcard_file.println(HumVal);
sdcard_file.close();
}
else {
Serial.println(F("ErrorSDOpen"));
}
//Water
int WaterValA1 = analogRead(A1);
int WaterValA2 = analogRead(A2);
Serial.println(WaterValA1);
Serial.println(WaterValA2);
if(WaterValA1 <=400){
digitalWrite(8,HIGH);
}
else{
digitalWrite(8,LOW);
}
if(WaterValA2 <=400){
digitalWrite(9,HIGH);
}
else{
digitalWrite(9,LOW);
}
//Soil
int SoilValA3 = analogRead(3);
int SoilValA4 = analogRead(4);
int SoilValA5 = analogRead(5);
Serial.println(SoilValA3);
Serial.println(SoilValA4);
Serial.println(SoilValA5);
if(SoilValA3 <=700){
digitalWrite(4,HIGH);
}
else{
digitalWrite(4,LOW);
}
if(SoilValA4 <=500){
digitalWrite(5,HIGH);
}
else{
digitalWrite(5,LOW);
}
if(SoilValA5 <=500){
digitalWrite(6,HIGH);
}
else{
digitalWrite(6,LOW);
}
previousMillis = currentMillis;
}
}
This does not include a timing feature to turn on the pumps.
I really hope I provided enough information. Thanks in advance.
Test.ino (628 Bytes)
FullSketch.ino (2.92 KB)