I am having a weird behavior on a sketch that I have coded for a multi zone heat system. Everything works as it should until I enable the mqtt to upload temps for the zones.
I have function called reconnect() that checks that the mqtt server is connected and reconnect if not. What happens is that the 8th relay clicks on and off every time that function runs. There is nothing in that function that controls a relay so I am not sure why it is doing it. I have tried changing the pin the relay uses but that does not help.
I am pretty new to coding and arduino and this project it one of my first big ones, so I know the codding could be neater but for now I am just trying to figure out why the strange relay triggering is happening. the relay basically clicks every 10 sec. If is comment out the reconnect() near the top of the loop, it stops. The code is as follows:
#include <avr/wdt.h>//watch dog - resets arduino if freezes up
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
//Temp Sensor library
#include <dht.h>
dht DHT;
//8 Channel Relay is backwards for on and off
#define ON LOW //Active low
#define OFF HIGH
#define TransInd_Pin 53
#define MainPump 2 //Output relay for Main Pump
//Used for external key pad
#define key1 29 //Cycle through 4 zones
#define key2 31 //edit selected zone
#define key3 33 //Raise Set Point Temp
#define key4 35 //Lower Set Point
//set loop count to 0 -
int loopCount = 0;
int DHT11_PIN[] = {A7, A8, A9, A10}; //DHT11 pin for sensors for 4 zones
int thermPin_1 = 25; //Therm pins used for thermostate in shop and dog school
int thermPin_2 = 27;
// Se points for zones 0 only needed for zones with DHT11 sensors. Unit heaters run on standard thermostate
float zone1SetPoints[] = {5, 0.5};
float zone2SetPoints[] = {10, 0.5};
float zone3SetPoints[] = {15, 0.5};
float zone4SetPoints[] = {20, 0.5};
float zone5SetPoints[] = {25, 1};
float zone6SetPoints[] = {30, 1};
float newSetPoint;//holds new temp setpoint untill saved
float previousSetpoint;
bool CallForHeat = false;//Is there a current call for heat? True = yes
bool MainOn = false;//Main circulation pump true is on, false is off
bool delayStart = false;
//setup the heat zone outputs
int HeatZone_1 = 3; // Dog School Floor
int HeatZone_2 = 4; // Shop Floor
int HeatZone_3 = 5; // Tex
int HeatZone_4 = 6; // Costco
int HeatZone_5 = 7; //Unit Heat Dog School
int HeatZone_6 = 8; //Unit Heat Shop
int blowerPin_1 = 9; //Dog School Unit Heat Blower Relay;
int blowerPin_2 = 10; //Shop Unit Heat Blower Relay;
int zone_state[6] = {0,0,0,0,0,0}; // set all zones to idle - idle = 0 - heating = 1
bool zoneHeatActive[6] = {false, false, false, false, false, false};
bool selectDisplay[4] = {false, false, false, false}; //used for determaining what zone to display on disply
int screenTimeOut = 0;
int currentScreen = 0;
String SavedState;//returned string to display on display to show saved
struct ZONES {
String zoneName;
int zoneNumber;
int zoneRelayPin;
int zoneHeatLED;
int zoneTempType;
int DHT11_PIN;
float zoneDesiredTemp;
float zoneHeatOffset;
};
//for testing only loading the first zone
ZONES zone[6] = { {"Dog School Floor Heat", 1, HeatZone_1, 22, 1, DHT11_PIN[0], zone1SetPoints[0], zone1SetPoints[1]},
{"Shop Floor Heat", 2,HeatZone_2, 24, 1, DHT11_PIN[1], zone2SetPoints[0], zone2SetPoints[1]},
{"Tex Heat", 3, HeatZone_3, 26, 1, DHT11_PIN[2], zone3SetPoints[0], zone3SetPoints[1]},
{"Cosco Heat", 4, HeatZone_4, 28, 1, DHT11_PIN[3], zone4SetPoints[0], zone4SetPoints[1]},
{"Dog School Unit Heat", 5, HeatZone_5, 30, 2, thermPin_1, zone5SetPoints[0], zone5SetPoints[1]},
{"Shop Unit Heat", 6, HeatZone_6, 32, 2,thermPin_2, zone6SetPoints[0], zone6SetPoints[1]}}; //Zone Name, zone number; zone heat, LED pin; zone type(fixed(1) or Thermostat(2), DHT11 temp sensor Pin, desired Temp)
int totalZones = 6; //total heat zones. Used in For Loops
unsigned long timer = 180000;
boolean delayPump = true; //pump delayed start by 3 minutes to allow valve time to open before turning main pump on to avoid damage to pump
unsigned long previousMillis = 0;
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Set your MAC address and IP address here
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFE };
IPAddress ip(192, 168, 2, 163);
const char* server = "192.168.2.51"; //address of MQTT server
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect("NikkiPublisher")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more outputs)
mqttClient.subscribe("BarnHeat");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
#include <LiquidCrystal_I2C.h> // Driver Library for the LCD Module
// Wiring: Connect SDA pin to 20 and SCL pin to 21
// Connects to LCD via I2C, at address 0x027
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4); // Adjust to (0x27,20,4) for 20x4 LCD
void setup() {
Serial.begin(9600);
String MqttStatus;
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "myClientID"
if (mqttClient.connect("NikkiPublisher"))
{
Serial.println("Connection has been established, well done");
MqttStatus = "MQTT Connected";
// Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
} else {
Serial.println("Looks like the server connection failed...");
}
// Initiate the LCD and turn on the backlight
lcd.init(); // Initiate the LCD module
lcd.backlight(); // Turn on the
// Print 'Hello World!' on the first line of the LCD
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.print("*Forever Homestead*");
lcd.setCursor(1, 1); //Set cursor to 2nd column and 2nd row (counting starts at 0)
lcd.print("Heat System Start");
lcd.setCursor(3, 3); //Set cursor to 3nd column and 3nd row
lcd.print("Initializing...");
delay(2000);//delay screen for readability
//clear screen and show I.P address and MQTT startup status
//I.P Address
lcd.clear(); //Clear the LCD
lcd.setCursor(0,0); // Set the cursor on the LCD to Col 1 Row 1
lcd.print("My IP address: "); // Print on text on the LCD
Serial.print("My IP address is: "); // Print text on the serial monitor
lcd.setCursor(0,1); //set the cursor on the LCD to Col 0 Row 2
Serial.println(Ethernet.localIP()); // Print the IP address on the Serial monitor
lcd.print(Ethernet.localIP()); // Print the IP address on the LCD
delay(1000);
//MQTT Status
lcd.setCursor(0,3); //set the cursor on the LCD to Col 0 Row 2
lcd.print(MqttStatus); // Print the IP address on the LCD
delay(3000);
pinMode(HeatZone_1, OUTPUT); // Define the HeatZone_1 as output pin - arduino relay board pin 1
pinMode(HeatZone_2, OUTPUT); // Define the HeatZone_2 as output pin - arduino relay board pin 2
pinMode(HeatZone_3, OUTPUT); // Define the HeatZone_3 as output pin - arduino relay board pin 3
pinMode(HeatZone_4, OUTPUT); // Define the HeatZone_4 as output pin - arduino relay board pin 4
pinMode(HeatZone_5, OUTPUT); // Define the HeatZone_5 as output pin - arduino relay board pin 5
pinMode(HeatZone_6, OUTPUT); // Define the HeatZone_6 as output pin - arduino relay board pin 6
pinMode(blowerPin_1, OUTPUT); // Define the blower Pin as output pin - arduino relay board pin 7
pinMode(blowerPin_2, OUTPUT); // Define the blower Pin as output pin - arduino relay board pin 8
pinMode(MainPump, OUTPUT); //Define the Main Pump as output - arduino relay stand alone
pinMode(TransInd_Pin, OUTPUT);//shows if system in transition waiting for valve to open
// initialize the Thermostate pin as an input:
pinMode(thermPin_1, INPUT);
pinMode(thermPin_2, INPUT);
//initialize Key Pad
pinMode(key1, INPUT_PULLUP);
pinMode(key2, INPUT_PULLUP);
pinMode(key3, INPUT_PULLUP);
pinMode(key4, INPUT_PULLUP);
Serial.println("Test Relay");
//test relays
for (int i = 3; i <= 10; i++) {
Serial.print("Relay ");
Serial.println(i-2);
pinMode(i, OUTPUT);
digitalWrite(i, ON);
delay(1000);
digitalWrite(i, OFF);
}
digitalWrite(MainPump, LOW); //set main pump to off to start
Serial.println("Test LED");
for (int r = 22; r <=32; r = r + 2) {
Serial.print("LED ");
Serial.println(r);
pinMode(r, OUTPUT);
digitalWrite(r, HIGH);
delay(1000);
digitalWrite(r, LOW);
}
digitalWrite(TransInd_Pin, LOW);//set LED to Off to start
delay(2000);
// this delay help to complete all initial tasks
wdt_enable(WDTO_4S); // Watchdog: reset board after one second, if no "pat the dog" received
}
void loop() {
int key1s = digitalRead(key1);
int key2s = digitalRead(key2);
int key3s = digitalRead(key3);
int key4s = digitalRead(key4);
if(!key1s){
//check screen count. reset if at 4
if(currentScreen == 4){
currentScreen = 0;
}
currentScreen = currentScreen + 1;
Serial.print("Current screen is ");
Serial.println(currentScreen);
changeScreen(currentScreen);
selectDisplay[currentScreen-1] = true;
//set previous screen to false
switch (currentScreen-1){
case 0:
selectDisplay[3] = false;
break;
case 1:
selectDisplay[0] = false;
break;
case 2:
selectDisplay[1] = false;
break;
case 3:
selectDisplay[2] = false;
break;
default:
Serial.println("unknown issue if switch for select display");
break;
}
//reset screen timer to zero with button press
screenTimeOut = 0;
}
if(!key2s){
Serial.println("Button 2 pressed");
if( newSetPoint == 0 ){
newSetPoint = zone[currentScreen-1].zoneDesiredTemp;
}else{
//newSetPoint set so save to global variable for current screen
SavedState = saveNewSetPoint(currentScreen);
editScreen(currentScreen);
newSetPoint = 0;
currentScreen = 0;
}
//this enters edit zone.
if(currentScreen >0){
editScreen(currentScreen);
}
screenTimeOut = 0;
}
if(!key3s){
Serial.println("Button 3 pressed");
//incress setpoint edit
newSetPoint = newSetPoint+1;
editScreen(currentScreen);
screenTimeOut = 0;
}
if(!key4s){
Serial.println("Button 4 pressed");
//incress setpoint edit
newSetPoint = newSetPoint-1;
editScreen(currentScreen);
screenTimeOut = 0;
}
//only check heat status every 5 loop
if(loopCount == 5){
if (!mqttClient.connected()) {
//reconnect(); //commented out for testing
}
// This is needed at the top of the loop!
mqttClient.loop();
// Ensure that we are subscribed to the topic
mqttClient.subscribe("BarnHeat");
//find status of Zone heat call - if any calling set flag to true - used for delay start
callingForHeat();
if( CheckKeys() == false){ //no keys pressed
int chk = DHT.read11(DHT11_PIN[0]);
float schoolTemp = DHT.temperature;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Scl: ");
lcd.print(round(schoolTemp));
lcd.print((char)223);
lcd.print("C");
int chk1 = DHT.read11(DHT11_PIN[1]);
float shopTemp = DHT.temperature;
lcd.setCursor(10,0);
lcd.print("Shop: ");
lcd.print(round(shopTemp));
lcd.print((char)223);
lcd.print("C");
int chk2 = DHT.read11(DHT11_PIN[2]);
float texTemp = DHT.temperature;
lcd.setCursor(0,2);
lcd.print("Tex: ");
lcd.print(round(texTemp));
lcd.print((char)223);
lcd.print("C");
int chk3 = DHT.read11(DHT11_PIN[3]);
float coscoTemp = DHT.temperature;
lcd.setCursor(10,2);
lcd.print("Cosc: ");
lcd.print(round(coscoTemp));
lcd.print((char)223);
lcd.print("C");
}else{
//key was pressed, count down timer
screenTimeOut++;
Serial.print("Screen Time out is ");
Serial.println(screenTimeOut);
if(screenTimeOut >= 5){
//screen timedout, reset all screens to false
for(int d=0; d<=3; d++){
selectDisplay[d] = false;
}
//reset current screen variable to default of 0
currentScreen = 0;
}
}
//Check Zone Status and compare with last loop
for( int i=0; i<= totalZones-1; i++){
//call proper zone array
int state = zoneStatusCheck(zone[i].zoneName, zone[i].zoneNumber, zone[i].zoneHeatLED, zone[i].zoneTempType, zone[i].DHT11_PIN, zone[i].zoneDesiredTemp, zone[i].zoneHeatOffset, zone_state[i]);
if(state ==1) {
Serial.print(zone[i].zoneName);
Serial.print(" calling for heat - ");
Serial.println(zone[i].zoneNumber);
if( checkPrevious(zone[i].zoneRelayPin) == false){ //not running
int sendOpen[] = {zone[i].zoneRelayPin, zone[i].zoneHeatLED};
OpenValve(sendOpen);
if(IsRunning() == true){ //pump running so just reset the call for heat flag
CallForHeat = false;
//check if that call for heat needs blower fans as well
if(zone[i].zoneNumber == 5){
digitalWrite(blowerPin_1, ON);
}
//check if that call for heat needs blower fans as well
if(zone[i].zoneNumber == 6){
Serial.print("In statement to tunr on zone ");
Serial.println(zone[i].zoneNumber);
digitalWrite(blowerPin_2, ON);
}
}else{
//main pump not running. delay pump start.
delayStart = true;
digitalWrite(TransInd_Pin, HIGH); // Turn on transition LED
Serial.println("Pump not running...");
unsigned long currentMillis = millis();
previousMillis = currentMillis;//reset millis so timer works properly
}
}
}else if( state == 2){
//turn off valve as heat no longer required
int sendClosed[] = {zone[i].zoneRelayPin, zone[i].zoneHeatLED};
CloseValve(sendClosed);
if(zone[i].zoneNumber == 5){
Serial.print("School Blower Fan turned off after close valve sent");
digitalWrite(blowerPin_1, OFF);
}
if(zone[i].zoneNumber == 6){
Serial.print("Shop Blower Fan turned off after close valve sent");
digitalWrite(blowerPin_2, OFF);
}
Serial.print("closing valve for ");
Serial.println(zone[i].zoneName);
//check if all valves closed and turn off pump if they are
if(!IsHeating()){
Serial.println("All zones closed. Turning off main pump");
digitalWrite(MainPump, 0); // 3 minute delay passed, turn on main pump
MainOn = false;//set bool to false
//publish that pump is off
if(mqttClient.publish("Barn Circulator", "off")){
Serial.println("Barn circulation pump MSG Published");
}else{
Serial.println("Barn circulation pump MSG FAILED");
}
}
}
}
//call for heat needs 3 minutes to activate main pump so that the requested zone is open
Serial.print("Call for heat is = ");
Serial.println(CallForHeat);
Serial.print("Is main Pump On = ");
Serial.println(IsRunning());
Serial.print("Delayed Start = ");
Serial.println(delayStart);
if( (CallForHeat == true) and (IsRunning() == false) and (delayStart == true) ){
unsigned long currentMillis = millis();
Serial.print("Delayed start requested ");
Serial.println(currentMillis);
if (currentMillis - previousMillis > timer){
previousMillis = currentMillis;
digitalWrite(MainPump, 1); // 3 minute delay passed, turn on main pump
MainOn = true;//set bool to true
delayStart = false;
digitalWrite(TransInd_Pin, LOW); // Turn off transition LED
//publish main pump on
if(mqttClient.publish("Barn Circulator", "On")){
Serial.println("Barn Circulation pump on MSG Published");
}else{
Serial.println("Barn Circulation pump on MSG FAILED");
}
//check if that call for heat needs blower fans as well
if ( (zoneHeatActive[4] == true) and (digitalRead(blowerPin_1) == OFF) ) {
Serial.println("In Timer, blower needed so turn on zone 5 blower");
digitalWrite(blowerPin_1, ON);
}
if ( (zoneHeatActive[5] == true) and (digitalRead(blowerPin_2) == OFF) ){
Serial.println("In Timer, blower needed so turn on zone 6 blower");
digitalWrite(blowerPin_2, ON);
}
}
}
Serial.println("");
//reset loopCount to 0;
loopCount = 0;
}// end loop every 5
loopCount++;
delay(1000);//Wait 1 seconds before accessing sensor again.
// Reset the WatchDog timer (pat the dog)
wdt_reset();
}//end loop()
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
String saveNewSetPoint( int zoneEdit ){
Serial.print("Saving set point for variable ");
Serial.println(zoneEdit);
switch (zoneEdit) {
case 1:
//set global variable
zone1SetPoints[0] = newSetPoint;
//set struct zone variable
zone[0].zoneDesiredTemp = newSetPoint;
return "Zone 1 Saved";
break;
case 2:
//set global variable
zone2SetPoints[0] = newSetPoint;
//set struct zone variable
zone[1].zoneDesiredTemp = newSetPoint;
return "Zone 2 Saved";
break;
case 3:
//set global variable
zone3SetPoints[0] = newSetPoint;
//set struct zone variable
zone[2].zoneDesiredTemp = newSetPoint;
return "Zone 3 Saved";
break;
case 4:
//set global variable
zone4SetPoints[0] = newSetPoint;
//set struct zone variable
zone[3].zoneDesiredTemp = newSetPoint;
return "Zone 4 Saved";
break;
default:
Serial.print("Undefined zone value in save setpoint");
break;
}
}
void editScreen(int screen){
Serial.print("Edit zone ");
Serial.println(screen);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(zone[screen-1].zoneName);
lcd.setCursor(0,1);
lcd.print("Desired: ");
lcd.setCursor(12,1);
lcd.print(zone[screen-1].zoneDesiredTemp);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,2);
lcd.print("New: ");
lcd.setCursor(12,2);
lcd.print(newSetPoint);
lcd.print((char)223);
lcd.print("C");
if(SavedState != ""){
lcd.setCursor(0,3);
lcd.print(SavedState);
//empty saved msg variable
SavedState = "";
}
}
void changeScreen(int screen){
Serial.print("Changing display to Zone ");
Serial.println(screen);
int chk = DHT.read11(DHT11_PIN[screen-1]);
//print to LCD
lcd.clear(); //Clear the LCD
lcd.setCursor(0,0);
lcd.print(zone[screen-1].zoneName);
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.setCursor(12,1);
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,2);
lcd.print("Humidity: ");
lcd.setCursor(12,2);
lcd.print(DHT.humidity);
lcd.print((char)223);
lcd.print("%");
lcd.setCursor(0,3);
lcd.print("Mode: ");
lcd.setCursor(12,3);
if(CallForHeat == true){
//check status of Main Pump
if (IsRunning()){
//main pump not on. delay heat zone start untill a valve a open (3 min delay)
lcd.print("Heating");
}else{
lcd.print("Delay St");
}
}else{
lcd.print("Stdby");
}
}
//check if keys pressed
bool CheckKeys() {
for( int d=0; d<=3; d++){
if (selectDisplay[d] == true) {
return true;
}
}
return false;
}
//check if a Main Pump is running and report true or false
bool IsRunning(){
if(digitalRead(MainPump) == 1){
return true;
}else{
return false;
}
}
//check if any zone is heating
bool IsHeating(){
static const uint8_t heat_pins[] = {3,4,5,6,7,8};
// Setup pins for input
for (int i = 0; i <= totalZones-1; i++) {
if (digitalRead(heat_pins[i]) == ON){
return true;
}
}
return false;
}
//check if any zone is calling for heat
void callingForHeat(){
CallForHeat = false;
// Setup pins for input
for (int i = 0; i <= totalZones-1; i++) {
Serial.print("checking calls for heat ");
Serial.print(". Zone ");
Serial.print(i+1);
Serial.print(" is ");
Serial.println(zoneHeatActive[i]);
if (zoneHeatActive[i] == true){
CallForHeat = true;
}
}
}
void OpenValve(int ZONE[]){
pinMode(ZONE[0], OUTPUT); //valve relay Pin
pinMode(ZONE[1], OUTPUT); //Status LED Pin
digitalWrite(ZONE[0], ON); //open valve calling sub
digitalWrite(ZONE[1], HIGH); //set LED state to heat
}
void CloseValve(int ZONE[]){
pinMode(ZONE[0], OUTPUT); //Valve relay pin
pinMode(ZONE[1], OUTPUT); //Status LED pin
digitalWrite(ZONE[0], OFF); //Close valve calling sub
digitalWrite(ZONE[1], LOW); //set LED state to off
}
int zoneStatusCheck(String zoneName, int zoneNumber, int zoneHeatLED, int zoneTempType, int DHT11_PIN, float zoneDesiredTemp, float zoneHeatOffset, int prevState){
int state;
bool heatActive = zoneHeatActive[zoneNumber-1];
//first Determain Type of Heat Monitor - Set temp = 1; Thermostat = 2
if( zoneTempType == 1 ){
//set temp so get current and previous temp
int chk = DHT.read11(DHT11_PIN);
float CurrentTemp = DHT.temperature;
Serial.print("Current zone ");
Serial.print(zoneName);
Serial.print(" Temperature = ");
Serial.print(DHT.temperature);
Serial.println(" C");
Serial.print("Target temp is ");
Serial.println(zoneDesiredTemp);
Serial.print("Humidity = ");
Serial.print(DHT.humidity);
Serial.println(" %");
if (DHT.temperature-zoneHeatOffset <= zoneDesiredTemp) {
//Calling for heat so change bool
heatActive = true;
}else{
heatActive = false;
}
}else{
Serial.print("Current zone ");
Serial.print(zoneName);
Serial.print(" - ");
Serial.print(zoneNumber-1);
//thermostat - check the pin
int thermState_1 = 0; // variable for reading the pushbutton status
int thermState_2 = 0;
// read the state of the pushbutton value:
thermState_1 = digitalRead(thermPin_1);
thermState_2 = digitalRead(thermPin_2);
// check if the thermostat calling for heat and set variable
if ((thermState_1 == HIGH) and (zoneNumber == 5) ) {
//Calling for heat so change bool
heatActive = true;
Serial.println(" calling for heat.");
} else if ((thermState_1 == LOW) and (zoneNumber == 5) ) {
heatActive = false;
Serial.println(" NOT calling for heat.");
}
// check if the thermostat calling for heat and set variable
if ((thermState_2 == HIGH) and (zoneNumber == 6) ) {
//Calling for heat so change bool
heatActive = true;
Serial.println(" calling for heat.");
} else if ((thermState_2 == LOW) and (zoneNumber == 6) ) {
heatActive = false;
Serial.println(" NOT calling for heat.");
}
}
if(heatActive == true){
state = 1;
zoneHeatActive[zoneNumber-1] = true;
} else {
state = 0;
zoneHeatActive[zoneNumber-1] = false;
}
//compare previous state with current and return changes to calling function
if( (state == 1) & (prevState == 1)){
return 0; //no change return 0
} else if ( (state == 1) & (prevState == 0) ){
zone_state[zoneNumber-1] = 1;
return 1; //changed to call for heat
}else if ( (state == 0) & (prevState == 0)) {
return 0; //no heat call and prev was same - no change
}else if ( (state == 0) & (prevState == 1) ) {
zone_state[zoneNumber-1] = 0;
return 2; //calling for no heat - turn off
}else{
Serial.print("error in status function. Undefined state change");
return 3; //missing somethng. print error
}
}
bool checkPrevious(int checkZone){
if(digitalReadOutputPin(checkZone) == HIGH){
return false;
}else{
return true;
}
}
int digitalReadOutputPin(uint8_t pin)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
if (port == NOT_A_PIN)
return LOW;
return (*portOutputRegister(port) & bit) ? HIGH : LOW;
}