Skip to content

Instantly share code, notes, and snippets.

@ppazos
Last active November 22, 2020 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppazos/eafaec4dde29f80e61e0255906c3f679 to your computer and use it in GitHub Desktop.
Save ppazos/eafaec4dde29f80e61e0255906c3f679 to your computer and use it in GitHub Desktop.
Automatic Irrigation System for Arduino
// IN/OUT PINS
// button pressed to run the water pump for on_ms milliseconds
const int button_in = 5; // D05 / PIN 8
// control pin that turns on the relay for the water pump
const int control_out = 2; // D03 / PIN 5
const int buzzer_out = 6; // D06 / PIN 9
// time the pump will be turned on
const int on_ms = 30000;
const int moisture_analog_in = A4;
int moisture_read = 0;
// current pump state (HIGH/LOW of the control_out)
int pump_state = LOW;
// current reading from the button_in pin
int button_reading = LOW;
// the time when the pump should be set to low (millis()+on_ms when the pump was set to HIGH)
unsigned long off_time = 0;
void setup()
{
// initial state for pump
// set before the pintMode to avoid having the pump_state floating when it is set to output
digitalWrite(control_out, pump_state);
pinMode(button_in, INPUT);
pinMode(control_out, OUTPUT);
pinMode(buzzer_out, OUTPUT);
pinMode(moisture_analog_in, INPUT);
Serial.begin(9600);
}
void loop()
{
// we could read the input and add a delay but that blocks the whole program,
// this will manage the pump status turning ON when it's OFF and the button
// is pressed, maintaining the ON for the program duration and turning it OFF
// after the program duration.
// check if the program should terminate
if (pump_state == HIGH)
{
Serial.println("PUMP STATE IS HIGH");
Serial.println(off_time - millis());
// program complete?
if (millis() > off_time)
{
program_completed();
}
}
// check if the pump should turn on
else
{
Serial.println("PUMP STATE IS LOW");
// this reading can 'bounce'
button_reading = digitalRead(button_in);
// turn on the program
if (button_reading == HIGH)
{
program_start();
}
}
// automatic program start if moisture is low
if (pump_state == LOW && read_moisture_percentage() < 60.0)
{
Serial.println("MOISTURE IS LOW");
program_start();
}
// debug slows down serial console
delay(500);
}
void buzzer_pip_pip_pip()
{
// buzzer notifies program is complete
digitalWrite(buzzer_out, HIGH);
delay(800);
digitalWrite(buzzer_out, LOW);
delay(400);
digitalWrite(buzzer_out, HIGH);
delay(800);
digitalWrite(buzzer_out, LOW);
delay(400);
digitalWrite(buzzer_out, HIGH);
delay(1000);
digitalWrite(buzzer_out, LOW);
}
void program_start()
{
Serial.println("PROGRAM STARTS");
pump_state = HIGH;
digitalWrite(control_out, pump_state);
off_time = millis() + on_ms; // should be turned off in on_ms
}
void program_completed()
{
Serial.println("PROGRAM FINALIZED");
// turn off
pump_state = LOW;
digitalWrite(control_out, pump_state);
off_time = 0;
buzzer_pip_pip_pip();
}
float read_moisture_percentage()
{
moisture_read = analogRead(moisture_analog_in);
//Serial.println(map(moisture_read, 0, 1023, 100, 0));
moisture_read = map(moisture_read, 0, 1023, 1024, 1);
//Serial.println(moisture_read);
//Serial.println(log(moisture_read)/log(2));
//Serial.println(10 * (log(moisture_read)/log(2)));
return 10 * (log(moisture_read)/log(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment