#include <Arduino.h>
// Define DRV8833 Control Pins mapped in Wokwi
const int VALVE_PIN_1 = 18;
const int VALVE_PIN_2 = 19;
// Solenoid pulse duration in milliseconds
const int PULSE_DURATION_MS = 500; // Extended slightly for visual clarity in simulation
// Function to control the latching valve polarity
void triggerValve(bool open) {
if (open) {
digitalWrite(VALVE_PIN_1, HIGH);
digitalWrite(VALVE_PIN_2, LOW);
Serial.println("-> Polarity: OPEN pulse sent (Pin 1 HIGH, Pin 2 LOW)");
} else {
digitalWrite(VALVE_PIN_1, LOW);
digitalWrite(VALVE_PIN_2, HIGH);
Serial.println("-> Polarity: CLOSE pulse sent (Pin 1 LOW, Pin 2 HIGH)");
}
// Keep pulse short to protect coil
delay(PULSE_DURATION_MS);
// Cut all power (High-Impedance state to save battery)
digitalWrite(VALVE_PIN_1, LOW);
digitalWrite(VALVE_PIN_2, LOW);
Serial.println("-> Power cut: Driver in standby (0V across valve)\n");
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("=== Low-Battery Irrigation Controller Initialized ===");
pinMode(VALVE_PIN_1, OUTPUT);
pinMode(VALVE_PIN_2, OUTPUT);
digitalWrite(VALVE_PIN_1, LOW);
digitalWrite(VALVE_PIN_2, LOW);
}
void loop() {
// 1. Simulate a trigger event (e.g., from weather/schedule)
Serial.println("Opening valve for watering cycle...");
triggerValve(true);
// Simulated watering window (shortened to 5 seconds for simulation)
delay(5000);
// 2. Close the valve
Serial.println("Closing valve to save water...");
triggerValve(false);
// Sleep interval before next cycle (shortened to 10 seconds for simulation)
Serial.println("Entering deep sleep / idle state for 10 seconds...\n");
delay(10000);
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4