Where to put a Counter

I want start a counter to count seconds and when it reaches one of the limits run for a minute. W by en it reaches the 3rd limit reset to zero and continue the new count.
Also monitor 2 temperature sensors DS18B20 that when they go below their limit trigger a relay and continue monitoring. Once above the limit turn off the relay.

Now my question is, can I do this? Also if I put this in a function then call it in the main loop, would it work? Right now I have it all in the main loop but it's not counting seconds it's more or less min or two.

Any tips or advice would help. Even experiments to try is welcome.
Thank you all.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Please post your code so we can advise you.
What is the application, what is your project for?

Thanks.. Tom.. :slight_smile:

If you want a variable to be used by more than one function, declare it outside the functions to make it 'global'.

If you want a value, used by a single function, to retain its value between calls to the function, use the 'static' keyword when you declare the local variable.

You can keep track of as many timers as you like by storing the unsigned long value returned by millis() in a global or static unsigned long variable. At any time you can calculate the elapsed time (in milliseconds) by subtracting the stored time from the current time.

There is a pinned topic at the top of this forum with beginner instructions for using mllis() for timing.

Thank you johnwasser for pointing me in the right direction. I assumed "time" was only if you had a RTC.
Now i have time in my program, how do i compare a set time and the timer? Sorry folk i don't have neat programming skill just trying to figure it out but here it is.

I need feed_relay1 and 2 to turn on and off at a certain time. Is there specific way of writing it that I'm missing?

#include <Time.h>
#include <TimeLib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS A3

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
int tempLimit = 70;      //define the lower threshold for the temperature
float temperature = 0;      //Variable to store the temperature in
float Celcius=0;
float Fahrenheit=0;
int relay = 2;
int feed_relay = 3;
unsigned long time;
const unsigned long feed1 = 9, 01, 01;
const unsigned long feed2 = 15, 01, 01;


void setup()
{
  pinMode(relay,OUTPUT);
  Serial.begin(9600);
  sensors.begin();
  digitalWrite(relay,HIGH );
  digitalWrite(feed_relay, HIGH);
  setTime (8,59,55,1, 01, 2018);

}


void loop()
{ 
 


  sensors.requestTemperatures(); //request temp
  Celcius=sensors.getTempCByIndex(0); //set to Celcius
  Fahrenheit=sensors.toFahrenheit(Celcius); //set to Fahrenheit
  temperature = Fahrenheit ; 
  /*Serial.print(" C  ");
  Serial.print(Celcius);
  Serial.print(" F  ");
  Serial.println(Fahrenheit);*/

  
  if(temperature <= tempLimit){
    /*Serial.println(", it's to cold!");*/
    digitalWrite(relay, LOW);
  }
  else if(temperature > tempLimit){
    /*Serial.println(", Temp is just right!");*/
    digitalWrite(relay, HIGH);}  


  if (time == 9,00,00 || time == 15,00,00);
    {
      digitalWrite(feed_relay, LOW);
        Serial.println("on");
     }
  if (time == 9,01,00 || time == 15,01,00);
    {
      digitalWrite(feed_relay, HIGH);
        Serial.println("OFF");
     }

  /*    
 Serial.print("Temp is  "); 
 Serial.print (temperature);
 Serial.println (" F" );
 Serial.print("I "); 
 Serial.println(i);
 Serial.print("Time: "); 
 //prints time since program started
 Serial.println(time);
  */

  Serial.print("Time now is: ");

  print2digits(hour());
  Serial.print(":");
  print2digits(minute());
  Serial.print(":");
  print2digits(second());

  Serial.print(" ");

  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year());

  Serial.println();

  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.print('0');
  }
  Serial.print(number);
}
  if (time == 9,00,00 || time == 15,00,00);

This will never work because even if the times matched the only code that would be executed as a result would be the semicolon on the end of the line. Remove it.

As to matching the times, when you have problems testing values of any kind it is helpful to print what you are testing. Have you tried that ?

sketch_dec06a:18:32: error: expected unqualified-id before numeric constant
 const unsigned long feed1 = 9, 01, 01;
                                ^

Sadly, you can't just put three values together with commas and expect the compiler to know that you intend those to be hour, minute, and second. The compiler doesn't know anything about time. That is what the library is for.

Look at the examples supplied with the time libraries. Find a function in the library that returns the date and time as an unsigned long.

Thank you all for your help i think i got it. time_t t =now()--- this store the current time in "t" then i can call aspects of it and compare it to the feed time.

time_t t = now();
  
  if (hour(t) == feedTime1 && minute(t) == 0 && second(t) == 0)
    {
        digitalWrite(feed_relay, LOW);
        Serial.println("on");
        delay(wait);
        digitalWrite(feed_relay, HIGH);
        Serial.println("off");

the new code is this.

#include <Time.h>
#include <TimeLib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS A3

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
int tempLimit = 70;      //define the lower threshold for the temperature
float temperature = 0;      //Variable to store the temperature in
float Celcius=0;
float Fahrenheit=0;
int relay = 2;
int feed_relay = 3;
unsigned long time;
unsigned long feedTime1 = 9; //feeding at 9 am
unsigned long feedTime2 = 15;//feeding at 3 pm
int wait = 30000; //wait 30 second to turn the feeder

void setup()
{
  pinMode(relay,OUTPUT);//setting the pin 2(relay) as an output
  Serial.begin(9600); // begins serial communication for debuging
  sensors.begin(); //sensor are turn on
  digitalWrite(relay,HIGH ); // verifying pin is set to high because of the type of relay.
  digitalWrite(feed_relay, HIGH);// verifying pin is set to high because of the type of relay.
  setTime (6,30,30,1, 01, 2018); // set a random time and date, its when i will plug it in(6:30 am).

}


void loop()
{ 
 
  time_t t = now(); //this store the current time  in "t" then i can call aspects

  sensors.requestTemperatures(); //request temp
  Celcius=sensors.getTempCByIndex(0); //set to Celcius
  Fahrenheit=sensors.toFahrenheit(Celcius); //set to Fahrenheit
  temperature = Fahrenheit ; 
  /*Serial.print(" C  ");
  Serial.print(Celcius);
  Serial.print(" F  ");
  Serial.println(Fahrenheit);*/

  
  if(temperature <= tempLimit){  
    /*Serial.println(", it's to cold!");*/
    digitalWrite(relay, LOW);                   // if temp is to cold it will trigger the relay on
  }
  if(temperature > tempLimit){
    /*Serial.println(", Temp is just right!");*/
    digitalWrite(relay, HIGH);                 // if temp is about the set temp, this will it will trigger the relay off
   }                                            

  
  if (hour(t) == feedTime1 && minute(t) == 0 && second(t) == 0){ // at set hour (9) and the min of "0" and second of "0", it will turn on the feed relay wait the set time then turn off
        digitalWrite(feed_relay, LOW);
        Serial.println("on");
        delay(wait);
        digitalWrite(feed_relay, HIGH);
        Serial.println("off");
     }
  if (hour(t) == feedTime2 && minute(t) == 0 && second(t) == 0){   // at set hour (15 or 3 pm) and the min of "0" and second of "0", it will turn on the feed relay wait the set time then turn off
        digitalWrite(feed_relay, LOW);
        Serial.println("on");
        delay(wait);
        digitalWrite(feed_relay, HIGH);
        Serial.println("off");
     }

//for debuging
  /*    
 Serial.print("Temp is  ");      
 Serial.print (temperature);
 Serial.println (" F" );
 Serial.print("Time now is: ");
  print2digits(hour());
  Serial.print(":");
  print2digits(minute());
  Serial.print(":");
  print2digits(second());

  Serial.print(" ");

  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year());

  Serial.println();

  delay(1000);
  */
}

void print2digits(int number) {      //this just check anything less then 10 after a colon it will place 0 in front of it
  if (number >= 0 && number < 10) {
    Serial.print('0');
  }
  Serial.print(number);
}