Hello,
My current project is a scoreboard using some LED matrix displays. Once a team has won the scoreboard is put into a loop to display the "game score" for the 3 seconds and the "winning team" name for 3 seconds by using delays. I currently have an interrupt button to stop the loop and start a new game.
However, is there any other way I can stop the loop without using an interrupt? Maybe I should avoid using delays to cycle between "game score" & "winning team"?
I am very new to programming and would appreciate some advice to push me in the right direction.
Thanks!
kwalking:
Maybe I should avoid using delays to cycle between "game score" & "winning team"?
Yes.
You should always avoid delay, there's always a better way.
Take a look at the blink without delay example.
Pieter
PieterP:
Yes.
You should always avoid delay, there's always a better way.
Take a look at the blink without delay example.
Pieter
Thanks for the tip! I think I know what I have to do now.
Even if you use an interrupt, your blocking code has to finish before the main code can react to it.
Only instant interrupts for that way are the reset button and power off.
Here is a tutorial on not using delay: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
After discussing Blink by delay....
But what if you want to blink the two LEDs at different rates? Like, once a second for LED 1 and twice a second for LED 2?
This is where the delay function doesn't really help.
Let's look at an analogy. Say you want to cook breakfast. You need to cook:
- Coffee - takes 1 minute
- Bacon - takes 2 minutes
- Eggs - takes 3 minutes
Now a seasoned cook would NOT do this:
- Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
- Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
- Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.
The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).
In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.
What you are likely to do is this:
- Start frying eggs. Look at watch and note the time.
- Glance at watch from time to time. When one minute is up then ...
- Start cooking bacon. Look at watch and note the time.
- Glance at watch from time to time. When another minute is up then ...
- Put coffee on. Look at watch and note the time.
- When 3 minutes are up, everything is cooked. Serve it all up.
In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.
Then follows the code and explanation.