Timer not working

I’m using code similar to that of this thread, and I have it set so that whenever 4 seconds pass, my money counter gains 1000. But for some reason, it isn’t working. The code described is at the very bottom of the main ino.

That’s because you’ve got the code that sets up the timer muddled in with the code that checks whether the timer has elapsed. So basically you’re resetting the timer at the same time that you’re checking if it’s elapsed, meaning it will never elapse (because it keeps resetting). (That’s assuming isPropertyBought stays true at least.)

It also looks like you’re calling PropertyTimer from within an if that checks if the A button is pressed, so the check that tests if the time has elapsed will only be checked when arduboy.justPressed(A_BUTTON) && money >= 120000 is true, since you aren’t calling it from anywhere outside the if as far as I can tell.

1 Like

Thanks, It worked! Now that the timer is functioning, is there a way I can reset the timer? Currently, after the timer elapses, the code inside the if statement just continues and keeps on adding money. So is there a way I can reset it? Also, a way that it can be active in any gamestate, so the money always comes?

How would you do it?

if (propertyTimerCurrent >= propertyTimer) {
money += 50;
}

Maybe make it so that the condition is invalid after the first trigger?

1 Like

Ok, I made it so that both timer codes (the starting 4 sec timer) and the counting code both have their own function, and in the counting code, after the 4 seconds in the if statement, I put the 4 sec timer function, so it should start again. Then I put the counting function in all of the menus and screens. This way it works just how I want it to.

Yes, but that depends on how you plan to use it.
E.g. if you want money to keep being added periodically, or if you want the timer to only fire once.

The line

is what sets the target time to be 1 second ahead.

The lines:

Are what check if that target time has been reached.

If you wanted to have it happening periodically then you could do:

uint32_t propertyTimerCurrent = millis();

if (propertyTimerCurrent >= propertyTimer)
{
	money += 50;
	propertyTimer = (millis() + (1 * 1000));
}

But if you wanted to be able to stop the timer then your best bet would be to introduce another boolean variable that determines if the timer should be running.

If you’re going to put it in all of them, then you’d be better off checking the timer outside of your main state machine (i.e. the switch that checks the game state and calls the appropriate function), then you won’t have to remember to call it from every state, and you won’t waste progmem calling the function from multiple places.

There are other alternatives too, like changing the code that checks if the timer has elapsed to find out how many seconds have passed since that appointed time in case you need to add multiple amounts of money. It really depends on what kind of a game you’re actually trying to make.

That all said, if you’re going to need more timers in future then there’s a much better way to go about it than having to create however many new functions every time you need to create a new timer.


For the record: ‘code’ in the sense of source code or software code is uncountable, like information - you cannot have ‘a code’ or ‘many codes’ in the same way you can’t have ‘an information’ or ‘many informations’.