Countdown game

I'm trying to make a game with a keyboard (keypad) and a timer (7 segment displays)

The game would compare with other input data while the counter is countdown, if it reaches 0, you lose.

The problem comes when do the counter, do not know how to do it, I mean:

Time program works well, and also the game´s program works well, but only separately. But, when I put together the program,
the counter stop for each time it expects user data.
Since the main program using loops, which is what stopped the counter.

My question is, if you know any alternative to the counter without delays and loops affect it .... and multitasking is possible? a library??

Thank you very much.

Possible??:

What kind of input is user entering? Button pushes or ???

the inputs are from a matrix keypad, and the library already have a event listener, but I cant do whit it.

Really, I want a countdown timer, isolated from the main program.
And in the main program can put loops, delays, etc...

And in the main program can put loops, delays

You really need to get over the idea that you can use a countdown timer and delay() at the same time.

Think about how you would approach the problem if the delay() function did not exist. You have access to millis() and micros().

in fact, i dont wanna use delays, only loops.

I need to put a loop on the subprogram of the keypad :

do
{
getkey(); // loops the sub program, until key is pressed.
}while(key is pressed)

Becouse, if i dont put like this. the program dont wait to the pressed key and pass to the next line.

Thank u

You need to invert the logic. Check for a valid key press. Do something when that happens. Do nothing if no key was pressed, or if a meaningless key is pressed.

Why do you want to wait for a key press?

what do you mean "invert the logic"??

I call the subprogram, (i.e. getkey():wink: but the subprogram only returns anything if a key is pressed in the moment of the call.
if no key is pressed the subprogram dont return anything
thats my problem, I put the subprogram in a loop thats break in the moment of a key is pressed, but if i dont press a key the countdown timer stops

I call the subprogram, (i.e. getkey(); ) but the subprogram only returns anything if a key is pressed in the moment of the call.

If the key press function needs to be a blocking function (i.e. does not return until a key is pressed), it will need to have some code added to it to update the "timer" (which really isn't a timer in the true meaning of the word) during the "while no key is pressed, do nothing" loop.

Thanks PaulS,
Do you think, the better option is:
Put an another mills() function ( similar to the timer program ) inside the subprogram of the getkey?????

thinking.... I have to try that.

a mills() function inside the getkey() that break the loop before the real mills() function need to work

How about this- wire up your keypad so that an interrupt is created when a key is pressed and a flag is set.

Then your main loop just counts down the time in say 1mS intervals, every time thru you check the flag and if set you call the keypad library to read the key (or, read the key as part of the interrupt)
(link might be a little off, can't tell from work, access is blocked - but you can search for 'rf remote crossroads' and find the link and discussion of it)

this is the code of keypad:

http://arduino.cc/forum/index.php/topic,59469.0.html

Okay, I was suggesting something like:
loop()

=1mS elapsed?
no, wait for next interval
yes:
interrupt flag set?
no, do nothing
yes, call get Key
case 1:
case 2:
etc thru case X:
elapsed time done?
yes - stop game
no - update time display

I essentially have this running at 100uS interval (0.1mS) for a fencing scoring system, every pass thru it checks for a touch, checks if an RF command came in from the remote control, if time is still running at the 1s mark the display is updated.
The only RF command that is acted upon while time is running is "stop", the other 15 are only acted on if time is not running (start, score change, time reset, etc).

I could desribe it better if I had the listing in front of me.
But most of the time, nothing is happening in the uC, it is just waiting for the interrrupt, or for the next time interval to be met.

As other posters have alluded to, it is once again time for everyone's favorite blink without delay example. http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Exactly.

Thank you CrossRoads, I will try that.
:cold_sweat: