From d296022b15fd5d56b0afadd8c6299714c078c50d Mon Sep 17 00:00:00 2001
From: user2684 <you@example.com>
Date: Mon, 8 May 2017 17:58:20 +0200
Subject: [PATCH] Add option to define which state means for relay open/close
 #77

---
 NodeManager.cpp | 15 ++++++++++++---
 NodeManager.h   |  3 +++
 README.md       |  2 ++
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/NodeManager.cpp b/NodeManager.cpp
index 2481ded..604eb27 100644
--- a/NodeManager.cpp
+++ b/NodeManager.cpp
@@ -790,6 +790,9 @@ void SensorDigitalOutput::setInitialValue(int value) {
 void SensorDigitalOutput::setPulseWidth(int value) {
   _pulse_width = value;
 }
+void SensorDigitalOutput::setOnValue(int value) {
+  _on_value = value;
+}
 
 // main task
 void SensorDigitalOutput::onLoop() {
@@ -812,15 +815,21 @@ void SensorDigitalOutput::onReceive(const MyMessage & message) {
       Serial.print(F(" P="));
       Serial.println(_pulse_width);
     #endif
+    // reverse the value if needed
+    int value_to_write = value;
+    if (_on_value == LOW) {
+      if (value == HIGH) value_to_write = LOW;
+      if (value == LOW) value_to_write = HIGH;
+    }
     // set the value
-    digitalWrite(_pin, value);
-    _state = value;
+    digitalWrite(_pin, value_to_write);
     if (_pulse_width > 0) {
       // if this is a pulse output, restore the value to the original value after the pulse
       wait(_pulse_width);
-      digitalWrite(_pin, value == 0 ? HIGH: LOW);
+      digitalWrite(_pin, value_to_write == 0 ? HIGH: LOW);
     }
     // store the current value so it will be sent to the controller
+    _state = value;
     _value_int = value;
   }
   if (message.getCommand() == C_REQ) {
diff --git a/NodeManager.h b/NodeManager.h
index 040aa9f..34241de 100644
--- a/NodeManager.h
+++ b/NodeManager.h
@@ -578,6 +578,8 @@ class SensorDigitalOutput: public Sensor {
     void setInitialValue(int value);
     // if greater than 0, send a pulse of the given duration in ms and then restore the output back to the original value (default: 0)
     void setPulseWidth(int value);
+    // define which value to set to the output when set to on (default: HIGH)
+    void setOnValue(int value);
     // define what to do at each stage of the sketch
     void onBefore();
     void onSetup();
@@ -585,6 +587,7 @@ class SensorDigitalOutput: public Sensor {
     void onReceive(const MyMessage & message);
   protected:
     int _initial_value = LOW;
+    int _on_value = HIGH;
     int _state = 0;
     int _pulse_width = 0;
 };
diff --git a/README.md b/README.md
index 159515a..3f535e6 100644
--- a/README.md
+++ b/README.md
@@ -442,6 +442,8 @@ Each sensor class can expose additional methods.
     void setInitialValue(int value);
     // if greater than 0, send a pulse of the given duration in ms and then restore the output back to the original value (default: 0)
     void setPulseWidth(int value);
+    // define which value to set to the output when set to on (default: HIGH)
+    void setOnValue(int value);
 ~~~
 
 #### SensorSwitch / SensorDoor / SensorMotion
-- 
GitLab