How to - DS1307 used to countdown and trigger alarms?

I am trying to build a simple device using the Arduino Uno where the user sets two delays, say 30 mins and 130 mins - normal and extended mode, when a limit switch is closed the device will start counting down (30 mins or 130 based on what mode is selected by the user), at the end of the countdown something will triggered (alarm, etc).
Only one countdown will be active at any given time, a switch from one mode to the other will reset the countdown.

What do you recommend I do? I have very little programming experience so please keep that in mind when you answer.
It is the countdown part that I can't wrap my head around, the limit switch and getting the DS1307 to work is not a problem.
I've managed to get a DS1307 to work with my Uno using various examples I found on the net.
I would like to use the DS1307 for this to get the current time when the limit switch is closed but if you think I should use millis I'm open to suggestion.

Also, am I looking at this the wrong way, should I do a count up?
If using the DS1307, would the countdown (count up) be affected when the clock rolls over at midnight?

Precision is not extremely important, a few seconds +/- per countdown is not a big deal. The countdown intervals will be minutes only (no seconds).
If you can provide examples or links that would be great.

Thanks

D.

As you are talking relative time you don't need a DS1307 . It can be done by the millis() function

You need something like this (not tested, is not a complete prog but it should give you a start)

void loop()
{

  if (millis() >= endtime)
  {
    // do you alarmthing
  }

  if (digitalRead(switchPin) == HIGH)
  {
    endtime = millis() + 130 * 60 * 1000L;  // current time + 130 minutes
  }
}

When you start debugging better use 10 and 20 seconds iso 30 and 130 minutes or you will drink a lot of coffee :slight_smile:

I wouldn't use Ds1307 (reason explained by robtillaart), unless I want to catch an event at a specific time of a day, like a camera linked to an alarm clock :slight_smile:

That makes sense. My thinking was that if I have the DS1307 giving me the time I might as well use it but now I see the simplicity and beauty of doing it with millis.

My device will have an LCD and temp sensor + the DS1307 but now that I understand the logic of using millis I see no point in using the DS for my countdown.

Thanks a lot.

D.

Be aware that the millis() returns an unsigned long so it will overflow after 49.7 days or so

To be protected against this overflow one need a subtraction in the comparison with millis() iso addition

#define LEDPIN 13
unsigned long startTime;
const int switchPin = 4;

void setup()
{
  startTime = millis();
  pinMode(LEDPIN, OUTPUT)
  digitalWrite(LEDPIN, LOW);
}

void loop()
{
  if (millis() - 130 * 60 * 1000L >= startTime)
  {
    // do you alarmthing
    digitalWrite(LEDPIN, HIGH);
  }

  if (digitalRead(switchPin) == HIGH)
  {
    startTime = millis();
  }
}

That shouldn't be a problem as the longest countdown will be around 3 hrs.

Any idea how I can read the remaining minutes (and eventually show them on the LCD)? I'm only interested in the conversion not the LCD part.
It would be great if I could do something like Time left: 27:00 then Time left: 26:59 and so forth. I guess the question is how do I get 26:59 for example out of millis? If that is too complicated I could settle for 27 -> 26.... Not as elegant but it will do, I guess.

Thanks for your help.

D.

something like

unsigned long m = millis() - 13060100 - starttime; // m = millis left

int min = m / 60000L;
int sec = m % 60000L; // dont forget the L

display min and sec on the display and put a : in between

And now its your turn to start to programming,

Sweeeeet!!!!
Thanks a lot.
I really appreciate your help.

D.