After quite a bit of reflection and contemplation, I think I've set myself up for failure because of a failure of understanding of one crucial element. I didn't really understand this, but the interrupt doesn't do quite what I thought it would do.
My impression was that the interrupt caused the program to jump out of its current "line of thinking" to run a different code. But the main thread actually continues to run while the second thread gets started. This means that I kind of need to think of the interrupts as being slightly separate programs. They're not quite different because they can call on the same variables that are used in the main thread (and so can interact with that code), but it means that I can't just use the interrupts to record button presses.
For example, I had intended to have a 5 second timer count down while it waits for the player to press a button. But when the button is pressed, it doesn't really do anything to or with the timer. It just causes some other program to run while the timer continues on and does its own thing. Several button pushes in a short period of time leads to several threads running, and that non-linearity of programs running is not really the best thing.
That being said, there may be a way to deal with it. It comes down to creating a couple variables. One variable acts like the busy variable, telling the interrupt whether to pay attention to the button press or ignore it. And the second one is a variable that simply carries the value of the pressed button. This will hopefully restore the linearity of the program and allow me to keep some of the things that have been brewing in my mind. Otherwise, I'm going to have to go back to a polling method. But now that I'm thinking about it, this two-variable solution is not that far away from polling, anyway. But I'm just going to go with it and see what happens.
No comments:
Post a Comment