Timer 2 ?

Hi I am still very new with Arduino and need help with my project

I need to build a clock that will count down , in seconds from 5mins to 0 mins and then imediately count up again from 0 mins to about 90 minutes, It will output the time on to a 4 X 7 segment display . At intervals during the count I need an output to a warning buzzer,

I believe I can manage the code for the output to the buzzer and can code to count and display the time on the display but up till now ( on pic chips) any counting I did I always did with pauses such as

Let X=0 // Start with empty variable Loop: // Start of loop pause (1000) // 1 second pause increment X // Variable increases by 1 goto loop // Repeat the loop

But of course that ties up the proccesor all the time.

I am guessing my best bet is an interupt that will count away un-noticed in the back ground while the rest of the program continues outputting to the buzzer and 7 seg displays. Then once per second the interrupt trips to let the code increment or decrement the clock.

I have tried to find out for myself how to do it by reading all I could find in tutorials , and I think that Timer2 is what I need to use but I just dont know how.

Could some one help me out please

P.S. I have a duemilanove328 so need to code for that

Thank you Don

You don't need a separate timer at all. The millis() function returns the time since the Arduino was reset. Call it, and record the time. Periodically, call it again, and compare the current time against the stored time. If the interval is greater than one second (1000 milliseconds), do something, like increment a counter. Otherwise do nothing.

Hi and thanks for the reply

However , wont that be un inaccurate ?

if I call the millis() function at less than 1 second intervals I am calling on the proccessor more often than I need to which is wasting proccessing time . If I call it at greater than 1 second intervals then the clock will update late .

Lets suppose I called millis() at around every 250ms then for the first 3 calls I need nothing to happen and on the fourth my clock increments so I have wasted 75% of the proccesing time .

if I call millis() every 1.25secs then my clock only updates 4 times in every 5 seconds

If I set my call to be at exactly 1 second then I dont need the call as I would already have my accurate 1 second interval

The idea is to save proccesing time by not needlessly polling for the right result but rather having the interupt run as a background function so the other tasks I need to do can still continue untill they are interrupted I am sure I read timer2 is the way to go . but being a newbie I just could not understand how to do it

Regards Don

DonRecardo:
However , wont that be un inaccurate ?

Impossible to say. You have not defined what “inaccurate” means in your application.

if I call the millis() function at less than 1 second intervals I am calling on the proccessor more often than I need to
which is wasting proccessing time .

I doubt the processor is going to be too upset that its time has been wasted. If it does get upset, get it something nice like an I2C CANBUS controller. :smiley:

Lets suppose I called millis() at around every 250ms then for the first 3 calls I need nothing to happen and on the fourth my clock increments
so I have wasted 75% of the proccesing time .

Your math is seriously flawed. In that scenario, the “wasted time” is CONSIDERABLY less than 75%.

The idea is to save proccesing time

In which case PaulS has given you good advice.

but being a newbie

As a newbie, you may want to seriously consider the advice offered by a person who is not a newbie.

High Coding Badly, Thanks for the reply,

Impossible to say. You have not defined what “inaccurate” means in your application.
You are quite right , I didnt, but as I said it was for a clock I had assumed people would guess I didnt
want to make it any more inacurate that I need to ( within reason),but I can see from your answer that it was my mistake and I need to be more carefull how I word it next time

I doubt the processor is going to be too upset that its time has been wasted. If it does get upset, get it something nice like an I2C CANBUS controller. :smiley:
No, I also doubt the processor will be upset, but depending how many tasks I need to do between updates of the clock , I might be upset if I run out of time to do them , because I wasted it making unneccesary calls to millis()

Your math is seriously flawed. In that scenario, the “wasted time” is CONSIDERABLY less than 75%.
No not my math .Once again my description was not accurate enough for you . I had not meant the total processing time but the time spent processing the millis() calls .If i called it 4 times when really only once was needed thats 3/4 of the calls were wasted time , and when I went to school 3/4 was 75%

In which case PaulS has given you good advice.
Yes I have no doubt he had but was it the “best” advice ? If I can do it without any calls to millis() then surely that has to save even more time . I had read that Timer2 would allow this, and thats why I asked for help on the Timer2 method

As a newbie, you may want to seriously consider the advice offered by a person who is not a newbie.

I always seriously consider advice who ever gives it to me , whether they be more experienced than me or even if they are newbies, I am never too old to learn , and often even newbies have some usefull tips ,but if you are saying because I am a newbie then I should take without question the advice offered to me by those with more experience , then I am afraid you are a fool, what if the experienced person has made a mistake ? or has missunderstood my meaning ?
It was because I considered Pauls advice that I thanked him for the trouble of replying

Just to let you know I have now read and re read the info I found on Timer2 and by using and altering the examples I have managed to get a good enough grasp on how to do it by myself. My clock now counts in seconds , it makes no calls to millis() at all and has so much time between updates I can make all my calculations and could display the time 30 times over without running out of time if I so chose .

It uses the Library MsTimer2 which I found on the Arduino page

You might like to read through your post again Coding Badly, you will find that every one of your lines are nothing
but sarcasm. Are you such a little man that you need to insult newbies to make you feel bigger?. Was your post
any way to encourage a newbie to ask for help? I am old enough to have come across many arse holes like you in my life so it doesnt really phase me , but others might not be so resilient and be put off

My post to which you took umbridge had been to Pauls reply and not one that you had written. I am sure if Paul had been offended by it he is big enough to say so himself without your help .You might consider that if you have nothing usefull to say to a post that didnt concern you that you might be better saying nothing , and keep your nose
out . Perhaps if you tried thinking up usefull answers to posts rather than insulting the writer , you might one day be able to change the user name from Coding Badly to Coding a Bit Better, but some how , I doubt it

Don

DonRecardo: High Coding Badly, Thanks for the reply,

And thank you for that wonderful reply! I'm certain you and I will become life long friends!

Ok, I did get a pretty good chuckle here, thanks for that CB :slight_smile:

Anyway, just so you know, millis() doesn’t waste much processor time. Here’s the entire function, posted below. In fact, spinning up another timer and incrementing another variable, when you already have one going is actually the thing that will ‘waste’ processor time.

unsigned long millis()
{
	unsigned long m;
	uint8_t oldSREG = SREG;

	// disable interrupts while we read timer0_millis or we might get an
	// inconsistent value (e.g. in the middle of a write to timer0_millis)
	cli();
	m = timer0_millis;
	SREG = oldSREG;

	return m;
}

Anyway, this is one of the few times that we do need moderators here to go clean up that last post. Let’s focus on coding conversations not personal attacks.

moderators here to go clean up that last post

Yeah, I was a little out of control when I wrote that. If you (or anyone else) wants I will remove it.

[quote author=Coding Badly link=topic=57632.msg414657#msg414657 date=1302050792]

moderators here to go clean up that last post

Yeah, I was a little out of control when I wrote that. If you (or anyone else) wants I will remove it.

[/quote]

But, but, but, hold on to the "Coding a Bit Better" idea. That could have legs. ;)

Lefty

[quote author=Coding Badly link=topic=57632.msg414657#msg414657 date=1302050792] Yeah, I was a little out of control when I wrote that. If you (or anyone else) wants I will remove it. [/quote]

Oh no, I wasn't talking about you, sorry, I was talking about the OP. Yours was a comical and mature response, IMO!

Oh no, I wasn't talking about you, sorry, I was talking about the OP

I know. ;)

Yours was a comical and mature response, IMO!

Glad to have entertained! And I certainly appreciate the sentiment. However, I have to disagree about "mature". The mature stance would have been with the other cheek turned.

Yikes! After writing that I had a flashback of a good friend who used to turn out two bare cheeks as an act of defiance. That is one sight I would gladly forget!

Coding a Bit Better

Hmm ... I feel a new moniker coming my way ... Or, how about "Coding a Bit Badder"? That's certainly better than "Coding a Bit Bladder". :D

... how about "Codeing Bitterly"? :P

Hi Don

Useing delay(1000) is not the right way to time 1 sec. You should use millis().

Every time you go though the loop() you should update a vaiable like newTime = millis() and compare newTime with and oldTime to see if more than 1000 milliseconds has elapsed.

The processer is not gonna go "sauer" on you just becurse you ask: "Hey what time is it?" more than more than 500 times a second. You will never get the "um-um um-um I allready told you a kazilion times so new answer is: banana" :fearful:

-Fletcher