Great job @TD22057
Could you share your sketch???
I´m working in something similar, but without the water sensor.
The valve is similar as well, buy it has 3 way, but still with 5 wires.
Do you remender, or could check, how did you configured the valve sensor? Debouncering or not?
I want select water from two different places, so the 3 way...
My project has a button to local valve operation, two leds to indicate from where water is comming, H-bridge to control the valve motor, and I use Home Assistant receiving and controlling valve status, and receiving the signal from valve´s status sensor.
Here is my code:
#define MY_DEBUG
#define MY_RADIO_NRF24
#define MY_REPEATER_FEATURE
#define MY_NODE_ID 10
#include <SPI.h>
#include <MySensors.h>
#include <DHT.h>
// #include <Bounce2.h>
// PINAGEM
#define int1 6 // ATUADOR PRA H BRIDGE
#define int2 7 // ATUADOR PRA H BRIDGE
#define button 4 // BOTAO LOCAL PARA MUDANÇA
#define info_rua 11 // SENSOR RETORNO DA VALVULA QUANDO RUA SELECIONADO
#define info_cist 12 // SENSOR RETORNO DA VALVULA QUANDO CISTERNA SELECIONADO
#define led_rua 9 // LED ACENDE PRA ÁGUA DA RUA
#define led_cist 10 // LED ACENDE PRA ÁGUA DA CISTERNA
#define CHILD_ID_ACT 0
#define CHILD_ID_ESTADO 1
// MOTOR
boolean buttonState = LOW;
int rotDirection = 0; // 0 PRA CISTERNA E 1 PRA RUA
int pressed = false;
bool initialValueSent = false;
bool state;
//sensores de fim de curso
int valor_rua = 0;
int valor_cist = 0;
MyMessage msgSwitch(CHILD_ID_ACT, V_STATUS); // SWITCH PARA MUDAR O ESTADO
MyMessage msgState(CHILD_ID_ESTADO, V_TRIPPED); //SENSOR DO ESTADO
void presentation()
{
sendSketchInfo("Seletor Agua", "1.0");
wait(200);
present(CHILD_ID_ACT, S_SPRINKLER);
wait(200);
present(CHILD_ID_ESTADO, S_SPRINKLER);
}
void setup() {
pinMode(int1, INPUT);
pinMode(int2, INPUT);
pinMode(button, INPUT);
pinMode(info_rua, INPUT);
pinMode(info_cist, INPUT);
pinMode(led_rua, OUTPUT);
pinMode(led_cist, OUTPUT);
//definir estado inicial da valvula
digitalWrite(int1, LOW);
digitalWrite(int2, HIGH);
digitalWrite(led_rua, LOW);
digitalWrite(led_cist, HIGH);
send(msgSwitch.set(0));
// setup do botao e dos sensores de fim de curso
digitalWrite(button, HIGH);
digitalWrite(info_rua, HIGH);
digitalWrite(info_cist, HIGH);
}
void loop() {
valor_rua = digitalRead(info_rua);
valor_cist = digitalRead(info_cist);
// valores iniciais
if (!initialValueSent) {
Serial.println("Sending initial value");
send(msgSwitch.set(0));
send(msgState.set(0));
Serial.println("Requesting initial value from controller");
request(CHILD_ID_ACT, V_STATUS);
wait(2000, C_SET, V_STATUS);
}
// ler estado do botão
if (digitalRead(button) == true) {
pressed = !pressed;
}
while (digitalRead(button) == true);
wait(20);
// se botao pressionado
if (pressed == true & rotDirection == 0) {
digitalWrite(int1, HIGH);
digitalWrite(int2, LOW);
digitalWrite(led_cist, LOW);
rotDirection = 1;
send(msgSwitch.set(1));
wait(20);
}
if (pressed == true & rotDirection == 1) {
digitalWrite(int1, LOW);
digitalWrite(int2, HIGH);
digitalWrite(led_rua, LOW);
digitalWrite(led_cist, HIGH);
rotDirection = 0;
send(msgSwitch.set(0));
wait(20);
}
if (valor_rua == 1) {
digitalWrite(led_rua, HIGH);
}
if (valor_rua == 0) {
digitalWrite(led_rua, LOW);
}
if (valor_cist == 1) {
digitalWrite(led_cist, HIGH);
}
if (valor_cist == 0) {
digitalWrite(led_cist, LOW);
}
}
void receive(const MyMessage &message) {
if (message.isAck()) {
Serial.println("This is an ack from gateway");
}
if (message.type == V_STATUS) {
if (!initialValueSent) {
Serial.println("Receiving initial value from controller");
initialValueSent = true;
}
state = message.getBool();
if (state == 0) {
digitalWrite(int1, LOW);
digitalWrite(int2, HIGH);
digitalWrite(led_rua, LOW);
digitalWrite(led_cist, HIGH);
rotDirection = 0;
send(msgSwitch.set(0));
}
if (state == 1) {
digitalWrite(int1, HIGH);
digitalWrite(int2, LOW);
digitalWrite(led_rua, HIGH);
digitalWrite(led_cist, LOW);
rotDirection = 1;
send(msgSwitch.set(1));
}
}
}