Aquarium timer getting stuck when changing case

Hi. First post here thanks for having me! I just got back into arduino and programming since I felt the need to make a CO2 and Air timer for my aquarium. The idea seemed simple at first yet most things do until you truly get into it. The issue that ive been trying to narrow down is on the programming side i believe hence posting here. I have an arduino nano 33 iot, 2 relays 1 for air pump one for co2 solenoid, an external ds3231rtc since the nanos rtc is not very accurate, and a ds18b20 temp probe that sends relay status and temp readings through a local MQTT server on raspberrypi. Which is nested in an old ace hardware power strip... The issue is only with the cycling of relay states for its programmed schedule. Which is: 7 am CO2 on and airump off (plants like CO2 during the day and turn it to O2). 4pm both CO2 and airpump off (to allow plants in aqaurium to absorb excess co2). 5pm till 7 am next day air pump is on and off at 5 min intervals, and co2 is off.
~ The issue is that even though the program switches between the listed times properly through the day, when 7 am the next day comes around it seems the airpump state is stuck in an always on state(no ON, OFF) and does not switch back to running the CO2 solenoid. Though when i unplug the arduino to reset, it corrects the issue and runs the program till getting stuck at the 7am switch once again. So Ive been manually starting the cycle every morning:/

Any help would be appreciated at this point as i am at loss of what to try... Ive been learning more about C++ basics to help me understand this issue. I originally tried this with if statements and it was much too messy and buggy. I have a feeling im either overlooking something very simple, or im just un-aware of how the coding proceeds to run. sorry if this was too long, or in the wrong section as this is my first time, and i havn't been on a forum in ages.

#include <DS3231_Simple.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
char ssid[] = "";  // your network SSID (name)
char pass[] = "";

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DS3231_Simple Clock;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

//wifi and mqtt stuff
const char broker[] = "192.168.0.23";
int port = 1883;
const char topic[] = "airpump";
const char topic2[] = "co2";
const char topic3[] = "temp";
//timer stuff
unsigned long previousMillis = 0;
unsigned int minTime = 0;
unsigned int hourTime = 0;
unsigned int prevminTime = 0;
const int fivemin = 5;
//just the pins for the relays
const int airPin = 12;
const int coPin = 11;
//program state, would have shortened to pro-state but then id have to look at that every freaking time...
unsigned int state;
//air&co2 state bools... find a way to make and on/off string instead of binary 0/1 readout of states
bool airpump = false;
bool carbonDi = false;
// Curent time reset:
const long interval = 8000;
//state of airpump relay
int airrelayState = LOW;
int numberOfDevices;  // Number of temperature devices found
DeviceAddress tempDeviceAddress;
float tempC = 0.0;
// We'll use this variable to store a found device address

void setup() {

  pinMode(airPin, OUTPUT);
  pinMode(coPin, OUTPUT);

  Serial.begin(9600);
  Serial.println();
  sensors.begin();

  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1)
      ;
  }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();


  Clock.begin();
  // Get an initialized timestamp
  //DateTime MyTimestamp = Clock.read();
  for (int i = 0; i < numberOfDevices; i++) {
    // Search the wire for address
    if (sensors.getAddress(tempDeviceAddress, i)) {  // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
    } else {
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }
  }
}


void loop() {
  mqttClient.poll();
  DateTime MyTimestamp = Clock.read();
  sensors.requestTemperatures();
  minTime = MyTimestamp.Minute;
  hourTime = MyTimestamp.Hour;

  //Check if RTC is currently in hour range for the co2 or air pump
  if (hourTime >= 17 || hourTime <= 6)  // air pump relay flip flops at 5 min intervals for the night
  {
    state = 1;
  } else if (hourTime == 16)  //hour pause to use up excess co2
  {
    state = 2;
  } else {
    state = 3;  // co2 for the day time
  }


  switch (state) {
    case 1:
      
        //Based on blink without delay using DS3231 time to setup 5 min alternating air pump
        if (minTime - prevminTime >= fivemin) {
          // save the last time prevminTime was updated resetting 5 min time counter
          prevminTime = minTime;
          if (airrelayState == LOW) {
            airrelayState = HIGH;
          } else {
            airrelayState = LOW;
          }
          digitalWrite(airPin, airrelayState);
          airpump = true;
        }
        break;
      
    case 2:
      
        digitalWrite(coPin, LOW);
        digitalWrite(airPin, LOW);
        airpump = false;
        carbonDi = false;
        break;
      
    case 3:
      
        digitalWrite(coPin, HIGH);
        digitalWrite(airPin, LOW);
        airpump = false;
        carbonDi = true;
        break;
      
  }


  //Prints relay state and temp values to mqtt and serial
  unsigned long currentMillis = millis();
  //millis timer also based on blink without delay example but instead shows time stamps in 1 sec intervals
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Clock.printTimeTo_HMS(Serial);
    Serial.println();
    int Rvalue1 = airpump;
    int Rvalue2 = carbonDi;
    float Rvalue3 = sensors.getTempF(tempDeviceAddress);
    Serial.print("Sending message to topic: ");
    Serial.println(topic);
    Serial.println(Rvalue1);

    Serial.print("Sending message to topic: ");
    Serial.println(topic2);
    Serial.println(Rvalue2);

    Serial.print("Sending message to topic: ");
    Serial.println(topic3);
    Serial.println(Rvalue3);

    // send message, the Print interface can be used to set the message contents
    mqttClient.beginMessage(topic);
    mqttClient.print(Rvalue1);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic2);
    mqttClient.print(Rvalue2);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic3);
    mqttClient.print(Rvalue3);
    mqttClient.endMessage();
    Serial.println();
  }
}

the chunks in question are in the loop function, being the if with time constraints setting the state, and the switch case below it reacting to the state changes. I put the airpump and CO2 bools within the case statements just so i could tell if it switched to that case through MQTT explorer. I did this because i couldnt tell if it was hardware or not before, but when looking at it through mqtt the values show it hasnt switched from case 1 to case 3 (airpump stays true, co2 shows false instead). Which makes me think it must be the coding getting stuck... does that seem correct?

Hi, @ethanclark321
Welcome to the forum.

Sorry but had to spread your post out a bit.

Thanks for posting your code in code tags.. :+1: :+1: :+1:

Tom... :grinning: :+1: :coffee: :australia:

Thankyou! Spread meaning too long correct? sorry wasnt trying to write a highschool essay but i have seen people complain about not enough.

I don't think that prevminTime is being reset from when it last left the case.

Try setting prevminTime to minTime in case 3 so that it is correct when re-entering case 1

Thankyou for the quick response! I will try that and see how it runs. So basically your saying its getting stuck switching to case 3 from case 1 at 7am due to the prevminTime counter not being reset? I thought, (and correct me if im wrong please) that the if switching the cases above it would see 7 am is true and switch from case 1 to case 3 looping it till 4 pm then switching to case 2 then back to case 1 at 5pm till 7am again independent of the prevminTime. Though it obviously isnt lol.

I think it's switching the cases properly with the time, it's just not executing case 1 as you expect because of the value of prevminTime.

Hopefully you can test by setting the time rather than waiting 24 hours. :wink:

Gotcha! Thankyou for the explanation as well. Lol you read my mind, I was gonna say I should test it by altering the times. hopefully the problem is recreated. I will go and see thankyou!!!

Heyo. So I tried setting the prevmin to minTime in case 3 and it seemed to just run only on with no flipflopping and then gets stuck switching from case 1 to 3. I tested the switch/cases with the if statement removed and setting the pin to HIGH and it cycles through the cases fine...just without flipflopping.

One thing I found out was that the issue does not happen unless it runs through the cases meaning when I would go to reset the arduino at the time when case 1 should be running it runs normally and switches to case 3 just fine. Seems to only have an issue when all cases are used. I havnt figured out yet if its specifically case 1 switching to case 3. Im curious to see if I start it during case 1, will it get stuck switching from case 2 to 1. As well as starting at case 2, will it get stuck on case 3 to case2...

Either way, I ended up separating the if statement out of the case 1 and left case 1 to only deal with the relay pin control. Hopefuly it works. Any suggestions or thoughts on properly using switch and case? Maybe using deafult and 2 cases?

Please post your latest code.

If you want to create a rollover at 60, you can do:

if ((((60 + minTime) - prevminTime) % 60) >= fivemin) {

and then that will create your rollover at 60 minutes and then it will behave as you expect.

1 Like
unsigned int minTime = 0;
unsigned int prevminTime = 0;
 minTime = MyTimestamp.Minute;

I'm not clear that there is an issue with the code as written. minTime is limited to 0-60 by the clock and indeed rolls over. The unsigned int typing of the variables should work.

Yes. Started drinking early today. :wink:

4 Likes

Thankyou for the explanation! I was wondering if using seconds or min instead would have been better for functionality. Like you said I've been trying to write this program for me to easily understand since I get confused by simple things easily, such as the blink without delay. Even though I have mentally gone through it step by step 10 times already lol. Also the reason I went with switch case vs. a bunch of if statements. So to clarify, the roll over issue would mainly be with the variable prevminTime, not minTime, as minTime is equal to the RTC's minute counter correct? And prevminTime needs to rollover at 60 like minTime. So the way it currently is working is just continually adding up till the prevminTime hits the unsigned variable rollover point? Thankyou for the thorough explanation. I hope Im getting understanding this correctly and I appreciate the help!

Thankyou! I will try this out. So this is separate from the flipflopping if statement? Or it is the flip flop if? Sorry Im quite slow with coding...

This is my updated code. So by moving the if statement out of case1 and setting it to start only at when state is == to 1 it FINALLY works as intended and will now run a full cycle, though i think the problem with prevminTime will arise again. What's the opinion on setting prevminTime = 0 in case 3 since it proceeds to it after case 1? Any reason why that would be a bad idea? Though I haven't implemented what has been learned here yet, I definitely plan to do so to help me understand all of this just a little better. Thankyou everyone for the help, I truly appreciate the time you're taking out of your day and the knowledge being bestowed.

#include <DS3231_Simple.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
char ssid[] = "";        // your network SSID (name)
char pass[] = "";  

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DS3231_Simple Clock;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

//wifi and mqtt stuff
const char broker[] = "192.168.0.23";
int        port     = 1883;
const char topic[]  = "airpump";
const char topic2[]  = "co2";
const char topic3[]  = "temp";
const char topic4[] = "hour";
const char topic5[] = "state";
//timer stuff 
unsigned long previousMillis = 0;
unsigned int minTime=0;
unsigned int hourTime=0;
unsigned int prevminTime=0;
const int fivemin = 5;
//just the pins for the relays
const int airPin = 12;
const int coPin = 11;
//program state, would have shortened to pro-state but then id have to look at that every freaking time...
unsigned int state;
//air&co2 state bools... find a way to make and on/off string instead of binary 0/1 readout of states
bool airpump = false;
bool carbonDi = false;
// Curent time reset:
const long interval = 8000; 
//state of airpump relay
int airrelayState= LOW;
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;
float tempC =0.0;
 // We'll use this variable to store a found device address

void setup() {
  
  pinMode(airPin,OUTPUT);
  pinMode(coPin,OUTPUT);
  
  Serial.begin(9600);  
  Serial.println();
  sensors.begin();
  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  Serial.print("Attempting to connect to the MQTT broker: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
     }

  Serial.println("You're connected to the MQTT broker!");
  Serial.println();

  
  Clock.begin();
  // Get an initialized timestamp
  //DateTime MyTimestamp = Clock.read();               
for(int i=0;i<numberOfDevices; i++)
  {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
	{	// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
		sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);	
	}else{
		Serial.print("Found ghost device at ");
		Serial.print(i, DEC);
		Serial.print(" but could not detect address. Check power and cabling");
	}
  }



}


void loop() 
{
 mqttClient.poll();
  DateTime MyTimestamp = Clock.read();              
  sensors.requestTemperatures();
  minTime=MyTimestamp.Minute;
  hourTime=MyTimestamp.Hour;

//Check if RTC is currently in hour range for the co2 or air pump
if (hourTime >=17 || hourTime <= 6 ) // air pump relay flip flops at 5 min intervals for the night
{
  state = 1;
}
else if (hourTime == 16) //hour pause to use up excess co2 
{
  state = 2;
}
else 
{
  state = 3; // co2 for the day time
}

if (minTime - prevminTime >= fivemin && state == 1)
  {
   // save the last time prevminTime was updated resetting 5 min time counter
    prevminTime = minTime;
     if (airrelayState == LOW) 
   {
      airrelayState = HIGH;
    } 
else 
    {
      airrelayState = LOW;
    }
  
  }
switch(state){
case 1 : 
  digitalWrite(airPin,airrelayState);
  airpump=true;
  
break;
case 2 :
digitalWrite(coPin,LOW);
airpump=false;
carbonDi=false;
break;

case 3 :
digitalWrite(coPin,HIGH);
digitalWrite(airPin,LOW);
airpump=false;
carbonDi=true;
break;

  }


//Prints relay state and temp values to mqtt and serial
  unsigned long currentMillis = millis();
    //millis timer also based on blink without delay example but instead shows time stamps in 1 sec intervals
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
    Clock.printTimeTo_HMS(Serial);
    Serial.println();
    int Rvalue1 = airpump;
    int Rvalue2 = carbonDi;
    float Rvalue3 = sensors.getTempF(tempDeviceAddress);
    int Rvalue4 = hourTime;
    int Rvalue5 = state;
    Serial.print("Sending message to topic: ");
    Serial.println(topic);
    Serial.println(Rvalue1);

    Serial.print("Sending message to topic: ");
    Serial.println(topic2);
    Serial.println(Rvalue2);

    Serial.print("Sending message to topic: ");
    Serial.println(topic3);
    Serial.println(Rvalue3);

    // send message, the Print interface can be used to set the message contents
    mqttClient.beginMessage(topic);
    mqttClient.print(Rvalue1);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic2);
    mqttClient.print(Rvalue2);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic3);
    mqttClient.print(Rvalue3);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic4);
    mqttClient.print(Rvalue4);
    mqttClient.endMessage();

    mqttClient.beginMessage(topic5);
    mqttClient.print(Rvalue5);
    mqttClient.endMessage();
  } 
}


I see what you mean. I think my terminology is a bit spotty as well. I understand it wont "roll over" at 60 per se, since rollover is when the variable type hits its "capacity" and resets to 0 right? And thats why you said the unsigned and millis example works because they both roll over at 65535?

Do you think odometers solution would be an appropriate solution?

Do you understand odometer's solution?

If not, then try Delta_G's suggestion

So you'll need a function that says if the new minute is greater than the old minute then just subtract and if the new minute is less than the old minute then do some different math.

byte elapsedTime;

if(minTime>=previousTime) 
{elapsedTime = minTime-previousTime;}

else //rollover of minutes
{elapsedTime = minTime + (60-previousTime);}

if (elapsedTime >= fivemin && state == 1)


I thought I did, but now that Im checking it I don't believe so. I guess I shouldnt be coding with such a bad understanding of math in general... Also I guess just coding altogether... The example you provided seems to make more sense to me. I will try to implement with what little I know. Thanks

Okay I think I kinda am understanding what odometers code is doing. So the modulus % gives a remainder of the current time + 60 minus the prevminTime /60. It's basically checking until the remainder is >= to 5 to execute the code, sets prevminTime to current and then continues to do so till 60. Please correct me if im not understanding this properly. Does this look right? :confused:

if ((((60 + minTime) - prevminTime) % 60) >= fivemin) {

prevminTime=minTime;

 if (airrelayState == LOW) 
   {
      airrelayState = HIGH;
    } 
else 
    {
      airrelayState = LOW;
    }
}

I will try this after Delta_G's solution, thankyou for writing it out like that cattledog.

Hey everyone. I tried both coding methods and have still been running into the same issue but I believe I might be getting closer...

The coding works great out of state 1 so I decided to watch the RTC min and the prevminTime through MQTT to see if those values are proper as the program is okay without the flip flopping code. I noticed at some point randomly in state 1, hour goes = 0 and minTime as well as prevminTime = either 3 or 14. Im unsure of how its getting these values as I watch prevmin go up in 5's when state 1 is running and it does it fine for a few hours until it randomly gives those values and only those values until the program is reset. Im pretty sure the arduino is still seeing the rtc min though as the pump still goes on and off... This happened with both codes implemented.

Maybe the hour = 0 is why its getting stuck in state 1 yet its still using the minTime??? I'm thinking I should watch it through serial instead as it might be more reliable than mqtt topics...

I've been checking out some other projects with similar function on the forums to see their methods and have seen yours and odometers contributions so I feel I am in good hands here. I'm wondering if this method of using the RTC min and hour are conflicting and as Delta_G was saying it would probably be better to use a single measurement of time since 12am. Though I have seen a few posts using similar methods as I've been trying or being suggested to do so that way.

-Do you guys think using the hour and min from the rtc is conflicting in some way?
-Maybe a different rtc library would handle this type of method better?
-Did I even implement the code correctly?

I find the feeling of multiple defeats from just trying to implement something that seems so simple is somewhat funny as much as it is defeating. Just gonna keep on trying.

I'm going to work on a copy with the single time measurement as Delta G described. In the mean time, ill wait to hear back from you guys. Thankyou.

~I apologize Delta G if it seemed I was ignoring your solution, I assure you I wasn't. I literally just didn't understand the explanation due to my low level understanding of coding. Though the help you and the others have been providing has helped me through this, I feel like I'm finally starting to grasp what is being said. Sorry for my slow uptake :confused:

#include <DS3231_Simple.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 5
#define TEMPERATURE_PRECISION 9
char ssid[] = "";        // your network SSID (name)
char pass[] = "";  

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DS3231_Simple Clock;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

//wifi and mqtt stuff
const char broker[] = "192.168.0.23";
int        port     = 1883;
const char topic[]  = "airpump";
const char topic2[]  = "co2";
const char topic3[]  = "temp";
const char topic4[] = "hour";
const char topic5[] = "state";
const char topic6[] = "min";
const char topic7[] = "rtcmin";
//timer stuff 
unsigned long previousMillis = 0;
int minTime=0;
int hourTime=0;
int prevminTime=0;
const int fivemin = 5;
//just the pins for the relays
const int airPin = 12;
const int coPin = 11;
//program state, would have shortened to pro-state but then id have to look at that every freaking time...
unsigned int state;
//air&co2 state bools... find a way to make and on/off string instead of binary 0/1 readout of states
bool airpump = false;
bool carbonDi = false;
// Curent time reset:
const long interval = 10000; 
//state of airpump relay
int airrelayState= LOW;
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;
float tempC =0.0;
// We'll use this variable to store a found device address

void setup() {
 
 pinMode(airPin,OUTPUT);
 pinMode(coPin,OUTPUT);
 
 Serial.begin(9600);  
 Serial.println();
 sensors.begin();
 
 // Grab a count of devices on the wire
 numberOfDevices = sensors.getDeviceCount();
 // attempt to connect to Wifi network:
 Serial.print("Attempting to connect to WPA SSID: ");
 Serial.println(ssid);
 while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
   // failed, retry
   Serial.print(".");
   delay(5000);
 }

 Serial.println("You're connected to the network");
 Serial.println();

 Serial.print("Attempting to connect to the MQTT broker: ");
 Serial.println(broker);

 if (!mqttClient.connect(broker, port)) {
   Serial.print("MQTT connection failed! Error code = ");
   Serial.println(mqttClient.connectError());
    }

 Serial.println("You're connected to the MQTT broker!");
 Serial.println();

 
 Clock.begin();
 // Get an initialized timestamp
 //DateTime MyTimestamp = Clock.read();               
for(int i=0;i<numberOfDevices; i++)
 {
   // Search the wire for address
   if(sensors.getAddress(tempDeviceAddress, i))
   {	// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
   	sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);	
   }else{
   	Serial.print("Found ghost device at ");
   	Serial.print(i, DEC);
   	Serial.print(" but could not detect address. Check power and cabling");
   }
 }



}


void loop() 
{
mqttClient.poll();
 DateTime MyTimestamp = Clock.read();              
 sensors.requestTemperatures();
 minTime=MyTimestamp.Minute;
 hourTime=MyTimestamp.Hour;

byte elapsedTime;
   if(minTime>=prevminTime) 
{elapsedTime = minTime-prevminTime;}

else //rollover of minutes
{elapsedTime = minTime + (60-prevminTime);}

if (elapsedTime >= fivemin && state == 1) {
prevminTime=minTime;

if (airrelayState == LOW) 
  {
     airrelayState = HIGH;
   } 
else 
   {
     airrelayState = LOW;
   }
}
//Check if RTC is currently in hour range for the co2 or air pump
if (hourTime <=6 || hourTime >=17 ) // air pump relay flip flops at 5 min intervals for the night
{
 state = 1;
}
else if (hourTime == 16) //hour pause to use up excess co2 
{
 state = 2;
}
else 
{
 state = 3; // co2 for the day time
}


switch(state){
case 1 : 
 digitalWrite(airPin,airrelayState);
 airpump=true;
 
break;
case 2 :
digitalWrite(coPin,LOW);

carbonDi=false;
break;

case 3 :
digitalWrite(coPin,HIGH);
digitalWrite(airPin,LOW);
airpump=false;
carbonDi=true;
break;

 }


//Prints relay state and temp values to mqtt and serial
 unsigned long currentMillis = millis();
   //millis timer also based on blink without delay example but instead shows time stamps in 1 sec intervals
 if (currentMillis - previousMillis >= interval) {
   // save the last time you blinked the LED 
   previousMillis = currentMillis;
   Clock.printTimeTo_HMS(Serial);
   Serial.println();
   int Rvalue1 = airpump;
   int Rvalue2 = carbonDi;
   float Rvalue3 = sensors.getTempF(tempDeviceAddress);
   int Rvalue4 = hourTime;
   int Rvalue5 = state;
   int Rvalue6 = prevminTime;
   int Rvalue7 = minTime;
   // send message, the Print interface can be used to set the message contents
   mqttClient.beginMessage(topic);
   mqttClient.print(Rvalue1);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic2);
   mqttClient.print(Rvalue2);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic3);
   mqttClient.print(Rvalue3);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic4);
   mqttClient.print(Rvalue4);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic5);
   mqttClient.print(Rvalue5);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic6);
   mqttClient.print(Rvalue6);
   mqttClient.endMessage();

   mqttClient.beginMessage(topic7);
   mqttClient.print(Rvalue7);
   mqttClient.endMessage();
 } 
}