esp32-garden-basics/02-water-pump/sketch/sketch.ino
2024-06-01 12:46:15 +02:00

80 lines
1.5 KiB
C++

/*
C1 : humidité du sol
C2 : remplissage du réservoir
I1 : Circuit alimenté si On
I2 : Substrat sec si On
I3 : Réservoir vide si On
P1 : Pompe réservoir -> substrat
*/
#define AHUMITY A0
#define AWATER_LEVEL A1
#define LED_POWER 12
#define LED_DRY 10
#define LED_ALERT 8
#define PUMP 6
#define HUMIDITY_THRESHOLD 530
#define WATER_LEVEL_THRESHOLD 530
// Durations in seconds
#define DURATION_PUMP 30
#define DURATION_DELAY 1800
#define SECOND 3 // OR 1000
void setup() {
Serial.begin(9600);
}
void loop() {
int humity = analogRead(AHUMITY);
int water_level = analogRead(AWATER_LEVEL);
digitalWrite(LED_POWER, HIGH);
bool is_dry = humity > HUMIDITY_THRESHOLD;
bool is_empty = water_level > WATER_LEVEL_THRESHOLD;
if (is_dry){
Serial.print("The soil is DRY (");
digitalWrite(LED_DRY, HIGH);
}else{
Serial.print("The soil is WET (");
digitalWrite(LED_DRY, LOW);
}
Serial.print(humity);
Serial.println(")");
// if the water is too low, never pump
if( is_empty ){
Serial.println("The water level is LOW ");
digitalWrite(LED_ALERT, HIGH);
}else{
Serial.println("The water level is HIGH");
digitalWrite(LED_ALERT, LOW);
if( is_dry){
Serial.println("Starting the pump");
digitalWrite(PUMP, HIGH);
delay(DURATION_PUMP * SECOND);
digitalWrite(PUMP, LOW);
Serial.println("Stopped the pump");
}
}
Serial.println("Sleeeping");
delay(DURATION_DELAY * SECOND);
Serial.println("Slept");
}