/*
* Wather level and pomp controler ESP32
* Manual and automatic level control
*
* Narcyz Wojcik 2024
*
* more infos you can find on https://esp32io.com/
*/
#include <ezButton.h> // Biblioteka ezButton do kontroli bouncingu
const int TRIG_PIN = 23; // ESP32 pin GPIO23 connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 22; // ESP32 pin GPIO22 connected to Ultrasonic Sensor's ECHO pin
const int BUTTON1_PIN = 34; // Przycisk ręcznego załączenia pompy
const int BUTTON2_PIN = 35; // Przycisk ręcznego wyłączenia pompy
ezButton button1(BUTTON1_PIN);
ezButton button2(BUTTON2_PIN);
// PIN 33 - zależnie od stanu, wybór pracy manualnej bądź automatycznej
const int PIN_tryb_pracy = 33;
const int RELAY_PIN = 21;
// zmienna tryby pracy
int tryb_pracy = 0;
int manualny = 0;
float duration_us, distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
button1.setDebounceTime(100);
button2.setDebounceTime(100);
// configure the trigger pin to output mode
pinMode(TRIG_PIN, OUTPUT);
// configure the echo pin to input mode
pinMode(ECHO_PIN, INPUT);
// configure the relay_pin to output mode
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
pinMode(PIN_tryb_pracy, INPUT);
delay(500);
}
void loop() {
button1.loop();
button2.loop();
int button1_state = button1.getState();
int button2_state = button2.getState();
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
// print the value to Serial Monitor
//Serial.print("distance: ");
//Serial.print(distance_cm);
//Serial.println(" cm");
tryb_pracy = digitalRead(PIN_tryb_pracy);
if (tryb_pracy == HIGH){ // Tryb manualny
//Serial.println("Manualny");
// Przycisk włączenia pompy wciśnięty pierwszy raz
if (button1.isPressed() && manualny == 0) {
digitalWrite(RELAY_PIN, HIGH);
//Serial.println("The button 1 is pressed");
manualny = 1;
}
// Przycisk wyłączenia pompy wciśnięty pierwszy raz
if (button2.isPressed() && manualny == 1){
digitalWrite(RELAY_PIN, LOW);
//Serial.println("The button 2 is pressed");
manualny = 0;
}
}
// Tryb automatyczny, oraz lustro wody poniżej 100cm
if (tryb_pracy == LOW && distance_cm < 100 ){
digitalWrite(RELAY_PIN, HIGH);
//Serial.println("Pompa włączona");
}
// Tryb automatyczny, oraz lustro wody powyżej > 250cm
if (tryb_pracy == LOW && distance_cm > 250 ){
digitalWrite(RELAY_PIN, LOW);
//Serial.println("Pompa wyłączona");
}
delay(500);
}