I’m writing a pong-type game in C, which will run on an ARM board over an LCD screen. Part of the requirements for the game is something called ‘magic time’.
A “magic time” period occurs at random intervals between 5 and 10 seconds – i.e, between 5 and 10 seconds after the last “magic time” period, and lasts for a random duration of 2 to 10 seconds.
I don’t really understand your question (do you execute this code every second via timer interrupt, or?), but there are some errors that I see on the first sight:
Last line (
magicTime == magicTimeLength;) don’t do anything – it simply evaluates ifmagicTimeis equal to themagicTimeLength, so you will enter dead-loop.I think that you want to do this:
magicTimeOccurencewith random value within 5 and 10.magicTimeLengthwith random value within 2 and 10.magicTimeOccurenceis greater than 0, decreaseits value by one.
magicTimeOccurencehits 0, decreasemagicTimeLengthvalueby one.
magicTimeLengthis greater than 0. If it is, it is magictime period (so, set the
magicTimeflag totrue). DecrementmagicTimeLength.magicTimeLength, setmagicTimetofalseand go to step 1.You should initialize your
timer0interrupt with period of 1s. I think that you accomplished it withbut make sure that is triggered every second.
Here is sample code, it should show you what I mean.