I have a terrarium code that works and I want to add a water pump to it. I will control the water pump using a relay. Given the size of my plant I want the water pump to only turn on once a day for 5 seconds. All the other actions on my terrarium work using if statements.
I am using a MEGA 2560. My circuit is as follows:
- A0 = light sensor (not currently used for anything; it's too finicky)
- A1 = soil humidity sensor
- Pin 5, 6, 7 = RTC (RST, Dat, CLK , respectively)
- Pin 9, 10, 11 = RGB led (displays the soil humidity using light color)
- Pin 22 = DHT
- Pin 31, 33, 35, 37 = 4 relay module
- Pin 41, 43, 45, 47, 49, 51 = LCD connections
Part numbers are as follows:
- RTC = DS1302
- DHT = DHT11
- Relays = 4 channel module
- Soil humidity sensor = I actually don't have this one, but it's a standard one (?)
- Fans = CPU fans connected to a 24V converter and plugged into relay
- LCD is a 2x16
I am trying to add a section of code so that relay1 runs power to the water pump and it turns on for 5 seconds at 8 am. To do this I wrote a water pump code as it's own sketch (sort of a mini version of my working terrarium sketch). However, the relay condition doesn't change when running the code:
#include <virtuabotixRTC.h> //real clock
int relay1 = 31;
virtuabotixRTC myRTC(7, 6, 5);
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000; //will be 10 mins = 600000 ms
unsigned long waterMillis;
unsigned long currentwaterMillis;
const unsigned long waterperiod = 5000;
bool wateredToday = false;
void setup() {
Serial.begin(9600); // Initializes Serial Monitor
pinMode(relay1,OUTPUT);
digitalWrite(relay1,LOW);
}
void loop() {
currentMillis = millis();
if (currentMillis - startMillis >= period){
myRTC.updateTime();
/* Turn on relay1 for 5 seconds. Also, when the waterpump turns on change the boolean condition for the next hour such that it doesn't turn on again */
if ((myRTC.hours == 8) && (wateredToday = false)){
digitalWrite(relay1,HIGH);
wateredToday == true;
currentwaterMillis = millis();
if (currentwaterMillis - waterMillis >= waterperiod){
digitalWrite(relay1,LOW);
}
}
else {
digitalWrite(relay1,LOW);
}
if (myRTC.hours > 9){
wateredToday = false;}
else {wateredToday = true;}
// Print to serial monitor
Serial.print("\nCurrent Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
Serial.print(wateredToday, DEC); //prints the condition of the bool
startMillis = currentMillis; //This the overall code refresh period
}
}
Can anyone tell what I'm doing wrong? Does anyone have better suggestions on how to do this?
Thanks