Hi Friends,
This is the code I've been working on for an irrigation controller. I would like a soil moisture sensor to be able to trigger the solenoid valves if it gets too dry in the garden bed, but also have the option of just pressing the button to trigger the relay. Right now, the buttons work and the soil moisture sensor kind of does... basically I start the program with the sensor in a glass of water, pull it out (nothing happens), put it back in and it triggers the relay for the stated interval... so, I feel like its close... but in practical terms I would like the relay to trigger when the sensor detects the dry soil, not the wet.
I am still very new to this and hope I am not wasting anyone's time. Thank you for your feedback and for the many hours of helpful tweaking this forum has already provided.
PS: This code is set up for 2 valves, though I have only tried adding the sensor to the first as I'm testing.
//CONSTANTS
const int BUTTON_PIN2 = 2; // Button2
const int BUTTON_PIN3 = 3; // Button3
const int relay1 = 7;
const int relay2 = 6;
const int greenLED2 = 8;
const int redLED2 = 9;
const int greenLED3 = 10;
const int redLED3 = 11;
const int sensor_pin = 5;
//VARIABLES
//int buttonPushCounter = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int buttonState2 = 0; // current state of the button
int soilMoisture = 0;
int lastButtonState = 0; // previous state of the button
bool relayOn1 = false;
bool relayOn2 = false;
//MILLIS
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
const unsigned long interval = 7000;
const unsigned long redLedInterval = 2000;
//const unsigned long redLedInterval2 = 5000;
void setup()
{
pinMode(BUTTON_PIN2, INPUT);
digitalWrite(BUTTON_PIN2, HIGH); // pull-up
pinMode(sensor_pin, INPUT);
pinMode(BUTTON_PIN3, INPUT);
digitalWrite(BUTTON_PIN3, HIGH); // pull-up
Serial.begin(9600);
pinMode(greenLED2, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(greenLED3, OUTPUT);
pinMode(redLED3, OUTPUT);
//SWITCH HANDLERS
pinMode(relay1, OUTPUT);
digitalWrite(relay1, HIGH);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, HIGH);
}
void loop(){
// read the pushbutton input pin:
buttonState1 = digitalRead(BUTTON_PIN2);
buttonState2 = digitalRead(BUTTON_PIN3);
soilMoisture = digitalRead(sensor_pin);
unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
// if button is pressed, turn relay on (if it wasn't already on), and reset the timer
if( buttonState1==HIGH || soilMoisture==LOW) // no need to check for previous state, in this specific case
{
previousMillis = currentMillis1;
digitalWrite(relay1, LOW);
digitalWrite(greenLED2, HIGH);
digitalWrite(redLED2, LOW);
relayOn1 = true;
}
// if relay is currently on...
if( relayOn1 )
{
// turn red led on, if close to turning off the relay
if (currentMillis1 - previousMillis >= interval-redLedInterval )
digitalWrite(redLED2, (millis()/100)%2);//blink red led; 300ms on; 300ms off
// if enough time has elapsed, turn of the relay
if (currentMillis1 - previousMillis >= interval)
{
// .. turn off relay
digitalWrite(relay1, HIGH);
digitalWrite(greenLED2, LOW);
digitalWrite(redLED2, LOW);
relayOn1 = false;
}
Serial.write("Garden Bed 1 is being Watered");
Serial.println(relayOn1);
}
if( buttonState2==HIGH ) // no need to check for previous state, in this specific case
{
previousMillis2 = currentMillis2;
digitalWrite(relay2, LOW);
digitalWrite(greenLED3, HIGH);
digitalWrite(redLED3, LOW);
relayOn2 = true;
}
// if relay is currently on...
if( relayOn2 )
{
// turn red led on, if close to turning off the relay
if (currentMillis2 - previousMillis2 >= interval-redLedInterval )
digitalWrite(redLED3, (millis()/100)%2);//blink red led; 300ms on; 300ms off
// if enough time has elapsed, turn of the relay
if (currentMillis2 - previousMillis2 >= interval)
{
// .. turn off relay
digitalWrite(relay2, HIGH);
digitalWrite(greenLED3, LOW);
digitalWrite(redLED3, LOW);
relayOn2 = false;
}
Serial.write("Garden Bed 2 is being Watered");
Serial.println(relayOn2);
}
}