Commit d296022b authored by user2684's avatar user2684
Browse files

Add option to define which state means for relay open/close #77

parent 9a485cdc
......@@ -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) {
......
......@@ -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;
};
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment