Go Down

Topic: Countdown timer (Read 608 times) previous topic - next topic

kwhunter

Hi there.
I need to build a timer that counts to 55 minutes, then turns a switch on for 5 minutes and then repeats the cycle until reset.
I believe it is more of a microcontroller than a timer, but let's just call it timer.
I have an Arduino UNO and a MEGA 2560 and I am not very good at it; I am a newbie and all help I can get will be greatly appreciated.
Thank you.

larryd

#1
Jun 15, 2019, 12:56 am Last Edit: Jun 15, 2019, 12:57 am by larryd
Have you Googled?
Arduino countdown timer

"until reset"
What does this mean?

Why are you wanting to do this?



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

kwhunter

Have you Googled?
Arduino countdown timer

"until reset"
What does this mean?

Why are you wanting to do this?

Probably my message is confusing, and it is...
I need a timer: which is off 55 minutes, then turns on a relay or whatever for 5 minutes, then repeats the cycle; I do not need the time displayed.
I googled and I found nothing close.
Thanks

larryd

#3
Jun 15, 2019, 01:14 am Last Edit: Jun 15, 2019, 01:15 am by larryd
"I am a newbie", but what software/hardware experience do you have?

Why are you wanting to do this?

Do you know what the BWD (blink without delay) technique is all about?

Show us what you have done so far.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

AdamDerbent

Actualy, the task is simply. You can do it easy. Loot @ Countimer library, it's great!

moShellShocker

Do you know what the BWD (blink without delay) technique is all about?
If your code doesn't need to do anything other than control the 55/5 minute timing, you don't even need blink without delay, using delay() is fine under those conditions. It's a forum knee jerk to say that whenever timing is involved that it must be delay()-less. (That said, if your code does need to do more than just the described timing, blink without delay will very likely be a requirement.)

If the code doesn't need to do anything else, it's a simple matter of using blink with two different delay() values.

You will need to consider what means on and what means off though. You talk of a "relay or whatever": many relay modules are so-called "active low" which means a low is on and a high is off.


GoForSmoke

How exactly 55 minutes and 5 minutes? Arduinos that have resonator clocks (most) can drift > 3 minutes a day. If real time is required you should get a Real Time Clock Module (about $2 or less) as your time source.

We were all beginners once. It takes practice at your own level to let you reach the next one.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

dave-in-nj

#7
Jun 15, 2019, 12:42 pm Last Edit: Jun 15, 2019, 12:50 pm by dave-in-nj
Countimer on GitHub

this may be what Adam DerBent was referring to.

but the idea is simple.

Going for Smoke adds a very valid point.

when you use a stopwatch there is an extra time needed to re-set the watch.  over many-many iterations this can add up.


the simple  BlinkWithoutDelay concept is rather easy and has many variants.
here is one of the many variants.

this is NOT workable, but shows the basic concept.
you have to work out your timing of seconds and minutes
for simplicity, I show 55 and 60, but that is milliseconds, not minutes.

Code: [Select]


timing = (millis() - then) ;

if (timing >= 55 ) {    // this is just to show the concept,  55 here is 55 milliseconds
action = LOW ;
}

if (timing >=60){
action = HIGH ;
then = millis() ;  // resets the counter
}

digitalWrite(motor, action)

GoForSmoke

Going for Smoke adds a very valid point.

when you use a stopwatch there is an extra time needed to re-set the watch.  over many-many iterations this can add up.

Oh no, we can keep from that. Add interval to start time, not millis() and you never gain a step.

The resonator is just inaccurate and does vary with temperature. A crystal is far more accurate. An RTC will have circuits with temperature effects that cancel each other and a battery of its own all for like $2.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

dougp

Build a free-running one-second millis() timer.  When this timer reaches preset, increment a counter and reset the timer.

As long as the counter is <= 54 the output is off.  When counter goes over 54, output turns on.  When counter > 59 reset counter.
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

kwhunter

If your code doesn't need to do anything other than control the 55/5 minute timing, you don't even need blink without delay, using delay() is fine under those conditions. It's a forum knee jerk to say that whenever timing is involved that it must be delay()-less. (That said, if your code does need to do more than just the described timing, blink without delay will very likely be a requirement.)

If the code doesn't need to do anything else, it's a simple matter of using blink with two different delay() values.

You will need to consider what means on and what means off though. You talk of a "relay or whatever": many relay modules are so-called "active low" which means a low is on and a high is off.


Thanks @moShellShocker, this should work; I need to turn a small (computer) fan on and off and I will use a Reed relay for that.

larryd

"It's a forum knee jerk to say that whenever timing is involved that it must be delay()-less. (That said, if your code does need to do more than just the described timing, blink without delay will very likely be a requirement.)"

"I am a newbie" means nothing other than they are new to the forum.

The OP gave no information on their skill set with writing code or assembling hardware.

Unfortunately, we often have to drag information out of questioners as they do not read or follow even the basics of forum rules.

No knee jerk here on the forum at all.

As for delay(), it can be used to write timing code, but hopefully we can present techniques to new people that they will use over and over to make them better programmers.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

JCA79B

#12
Jun 15, 2019, 08:31 pm Last Edit: Jun 15, 2019, 09:26 pm by JCA79B
Quote
As for delay(), it can be used to write timing code, but hopefully we can present techniques to new people that they will use over and over to make them better programmers.
Agreed++! I believe we should see more of the advanced features like direct port manipulation and hardware timer operation so beginners won't always be beginners and more easily move on to advanced timing and higher speed.
Yet another variant, runs on Arduino Nano:
Code: [Select]

unsigned long timeKeeper,
              offTime = 1000UL * 60 * 55, // 55 minutes
              onTime = 1000UL * 60 * 5; // 5 minutes
void setup()
{
  bitSet(DDRB,5); // set pin 13 as OUTPUT
}
void loop() {
  if(millis() - timeKeeper >= (onTime + offTime))
  {
    timeKeeper += (onTime + offTime); // reset timeKeeper to current millis()
  }
    //switch pin 13
  bitWrite(PORTB,5,millis() - timeKeeper > offTime);
}

larryd

Now you are just being mean to the noob. ;)



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

JCA79B

Now you are just being mean to the noob. ;)
Nah, just trying to help him / her become an ex noob.   :)

Go Up