br3ttb / Arduino-PID-Library

1.96k stars 1.11k forks source link

Set pinMode(RELAY_PIN,OUTPUT) & add RELAY_ON /RELAY_OFF per https://github.com/br3ttb/Arduino-PID-Lib… #137

Open drf5n opened 1 year ago

drf5n commented 1 year ago

Flesh out the logic of https://github.com/br3ttb/Arduino-PID-Library/blob/master/examples/PID_RelayOutput/PID_RelayOutput.ino to clearly accommodate both active-LOW and active-HIGH relays per https://github.com/br3ttb/Arduino-PID-Library/issues/136

diff --git a/examples/PID_RelayOutput/PID_RelayOutput.ino b/examples/PID_RelayOutput/PID_RelayOutput.ino
index 17fbe1a..7c19203 100644
--- a/examples/PID_RelayOutput/PID_RelayOutput.ino
+++ b/examples/PID_RelayOutput/PID_RelayOutput.ino
@@ -18,6 +18,8 @@

 #define PIN_INPUT 0
 #define RELAY_PIN 6
+#define RELAY_ON LOW
+#define RELAY_OFF HIGH

 //Define Variables we'll be connecting to
 double Setpoint, Input, Output;
@@ -55,8 +57,14 @@ void loop()
   { //time to shift the Relay Window
     windowStartTime += WindowSize;
   }
-  if (Output < millis() - windowStartTime) digitalWrite(RELAY_PIN, HIGH);
-  else digitalWrite(RELAY_PIN, LOW);
+  if (Output > millis() - windowStartTime)
+  {
+    digitalWrite(RELAY_PIN, RELAY_ON);
+  }
+  else
+  {
+    digitalWrite(RELAY_PIN, RELAY_OFF);
+  }

 }