Hello
I would like to build a water level sensor.
I have four reed contacts wired and connected.
it works so far.
but unfortunately it happens very often that he switches though.
but not back.
I think it's the software.
Unfortunately, my programming arts are not that great.
my english also not ....
can someone help me solve this problem?
I use the following sketch.
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* Example sketch for a "light switch" where you can control light or something
* else from both HA controller and a local physical button
* (connected between digital pin 3 and GND).
* This node also works as a repeader for other nodes
* http://www.mysensors.org/build/relay
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#define MY_NODE_ID 50
// Enabled repeater feature for this node
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
#define RELAY_PIN1 8 // Arduino Digital I/O pin number for relay
#define RELAY_PIN2 6 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN1 2 // Arduino Digital I/O pin number for button
#define BUTTON_PIN2 3 // Arduino Digital I/O pin number for button
#define BUTTON_PIN3 4 // Arduino Digital I/O pin number for button
#define BUTTON_PIN4 5 // Arduino Digital I/O pin number for button
#define BUTTON_PIN5 A3 // Arduino Digital I/O pin number for button
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();
int oldValue1=0;
int oldValue2=0;
int oldValue3=0;
int oldValue4=0;
int oldValue5=0;
bool state1;
bool state2;
bool state3;
bool state4;
bool state5;
MyMessage msg1(1,V_LIGHT);
MyMessage msg2(2,V_LIGHT);
MyMessage msg3(3,V_LIGHT);
MyMessage msg4(4,V_LIGHT);
MyMessage msg5(5,V_LIGHT);
void setup()
{
// Setup the button
pinMode(BUTTON_PIN1,INPUT);
pinMode(BUTTON_PIN2,INPUT);
pinMode(BUTTON_PIN3,INPUT);
pinMode(BUTTON_PIN4,INPUT);
pinMode(BUTTON_PIN5,INPUT);
// Activate internal pull-up
digitalWrite(BUTTON_PIN1,HIGH);
digitalWrite(BUTTON_PIN2,HIGH);
digitalWrite(BUTTON_PIN3,HIGH);
digitalWrite(BUTTON_PIN4,HIGH);
digitalWrite(BUTTON_PIN5,HIGH);
// After setting up the button, setup debouncer
debouncer1.attach(BUTTON_PIN1);
debouncer2.attach(BUTTON_PIN2);
debouncer3.attach(BUTTON_PIN3);
debouncer4.attach(BUTTON_PIN4);
debouncer5.attach(BUTTON_PIN5);
debouncer1.interval(30);
debouncer2.interval(30);
debouncer3.interval(30);
debouncer4.interval(30);
debouncer5.interval(30);
// Make sure relays are off when starting up
digitalWrite(RELAY_PIN1, RELAY_OFF);
digitalWrite(RELAY_PIN2, RELAY_OFF);
// Then set relay pins in output mode
pinMode(RELAY_PIN1, OUTPUT);
pinMode(RELAY_PIN2, OUTPUT);
// Set relay to last known state (using eeprom storage)
state1 = loadState(1);
digitalWrite(RELAY_PIN1, state1?RELAY_ON:RELAY_OFF);
state5 = loadState(5);
digitalWrite(RELAY_PIN2, state5?RELAY_ON:RELAY_OFF);
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay & Button", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(1, S_LIGHT);
present(2, S_LIGHT);
present(3, S_LIGHT);
present(4, S_LIGHT);
present(5, S_LIGHT);
}
/*
* Example on how to asynchronously check for new messages from gw
*/
void loop()
{
debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();
// Get the update value
int value1 = debouncer1.read();
if (value1 != oldValue1 && value1==0) {
send(msg1.set(state1?false:true), true); // Send new state and request ack back
}
oldValue1 = value1;
// Get the update value
int value2 = debouncer2.read();
if (value2 != oldValue2 && value2==0) {
send(msg2.set(state2?false:true), true); // Send new state and request ack back
}
oldValue2 = value2;
// Get the update value
int value3 = debouncer3.read();
if (value3 != oldValue3 && value3==0) {
send(msg3.set(state3?false:true), true); // Send new state and request ack back
}
oldValue3 = value3;
// Get the update value
int value4 = debouncer4.read();
if (value4 != oldValue4 && value4==0) {
send(msg4.set(state4?false:true), true); // Send new state and request ack back
}
oldValue4 = value4;
// Get the update value
int value5 = debouncer5.read();
if (value5 != oldValue5 && value5==0) {
send(msg5.set(state5?false:true), true); // Send new state and request ack back
}
oldValue5 = value5;
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println("This is an ack from gateway");
}
if (message.type == V_LIGHT) {
// Change relay state
if(message.sensor==1){
digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
state1=message.getBool();
}
// Change relay state
if(message.sensor==2){
digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
state2=message.getBool();
}
// Change relay state
if(message.sensor==3){
digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
state3=message.getBool();
}
// Change relay state
if(message.sensor==4){
digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
state4=message.getBool();
}
if(message.sensor==5){
digitalWrite(RELAY_PIN2, message.getBool()?RELAY_ON:RELAY_OFF);
state5=message.getBool();
}
// Store state in eeprom
saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Greetings
Michael