Never ending while loop

Hi!

I'm new to the community but not to programming. So, greetings to everybody!

I'm using Arduino IDE 1.8.9

Recently, I worked on an implementation of the game Snake on an Arduino Nano. Everything is working fine, the only problem I couldn't solve is a while loop which is executed as long as the start button was not pressed and should be left after the button was pressed.

As soon as the device gets powered on,the game starts, which is completely as expected to do.
When the game is over, the LCD shows "Game over", and then a while loop is executed.
If I comment it out, the game restarts immediately. So the empty while loop was not omitted by the compiler.

This is the while loop:

while(!tasten.getButtonCycle(buttonStart)) {  //will be never left
}

If I declare a volatile variable before the loop and change it inside, e.g. var1++;, then nothing changes.

But, if I manipulate a PORT pin, then it works as expected?!

while(!tasten.getButtonCycle(buttonStart)) {  //will be left as expected
      digitalWrite(14, HIGH);
}

The buttons are read during a Timer1 interrupt which happens every 1ms. My lib, which reads the buttons using shift registers and using only one pin as input,does work as expected in every other case.
If the button is pressed, true is returned by getButtonCycle(), else it's false.

I have no clue, why it shows this behaviour, but maybe someone is able to explain this to me?

NanoGame.zip (17.5 KB)

You’ve fallen for the trap of thinking that a timer interrupt is going to save you from learning to write code that doesn’t block. And now it’s gotten convoluted. And now this is harder than just learning to use millis. The “easy way” has turned out to be more difficult.

Post your code. You’ve obviously made a mistake somewhere. How do you think anyone can find it if you keep the code secret?

Impossible to answer without the full code. You probably forgot to make "tasten" or some of its fields volatile.

That being said, using interrupts to read user input is an unusual approach, and often makes things much harder (both reasoning about the control flow and writing bug-free code).

Pieter

Everything needed inside the zip file.

Delta_G:
You’ve fallen for the trap of thinking that a timer interrupt is going to save you from learning to write code that doesn’t block. And now it’s gotten convoluted. And now this is harder than just learning to use millis. The “easy way” has turned out to be more difficult.

Post your code. You’ve obviously made a mistake somewhere. How do you think anyone can find it if you keep the code secret?

What's millis? Honestly, I don't use Arduino libraries that much. They're slow and need often a lot more space than a selfmade solution. Actually, I want to know, what my code is doing, I don't want to copy & paste some shit together and try, if it will run somehow.

NanoGame.zip (17.5 KB)

thor4115:
What's millis? Honestly, I don't use Arduino libraries that much. They're slow and need often a lot more space than a selfmade solution.

Selfmade solution that doesn't appear to be working.

millis() is a fundamental function in the Arduino ecosystem (not a library) and is key to the accepted method for writing non-block code that emulates multi-tasking.

What's millis?

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

gfvalvo:
Selfmade solution that doesn't appear to be working.

millis() is a fundamental function in the Arduino ecosystem (not a library) and is key to the accepted method for writing non-block code that emulates multi-tasking.

So your solution to everything is, to only rely on the solutions of other people? I want to learn doing stuff by myself. In my opinion, this is the better approach.
A code which is not working needs to be fixed. Trying that is more worth, in meanings of learning something, than getting pointed to some instant stuff.
Thank you.

Ok, now I found the issue.
It was really simple. The button flags inside the shiftRegButton class were not volatile. But why was there no problem with this in the runningGame() Method?

thor4115:
So your solution to everything is, to only rely on the solutions of other people? I want to learn doing stuff by myself. In my opinion, this is the better approach.
A code which is not working needs to be fixed. Trying that is more worth, in meanings of learning something, than getting pointed to some instant stuff.
Thank you.

What do you mean? I think you’ve misunderstood. You should read the language reference. The millis function isn’t some user contributed thing. It’s a fundamental part of Arduino. All it dose is tell you the length of time the board has been up.

If you throw it away because it is written by others then I assume you’re also not using digitalRead or digitalWrite or any of the Serial functions either. Will you be rewriting those for yourself?

Also please note that due to security concerns a zip file is the absolute worst way to post code. Not many people will risk unzipping something from a total stranger.

Delta_G:
Also please note that due to security concerns a zip file is the absolute worst way to post code. Not many people will risk unzipping something from a total stranger.

And what exactly could happen, if you extract a compressed file?
As long as you don't execute some script or executable what is supposed to happen? You sound like some sort of hysterical Windows user :smiley:

Delta_G:
What do you mean? I think you’ve misunderstood. You should read the language reference. The millis function isn’t some user contributed thing. It’s a fundamental part of Arduino. All it dose is tell you the length of time the board has been up.

If you throw it away because it is written by others then I assume you’re also not using digitalRead or digitalWrite or any of the Serial functions either. Will you be rewriting those for yourself?

That's not the point. Of course it makes no sense, reinventing the wheel. But why is my solution wrong, just because it differs from another one?
Very open minded community...

That's not the point. Of course it makes no sense, reinventing the wheel. But why is my solution wrong, just because it differs from another one?
Very open minded community...

Hey man, relax a little. We are trying to help here. If you don’t want free help then by all means go hire someone.

All I was saying is that I’ve been down that road before. If you’re not interested in my experience then that’s fine. When it bites you it will just be that much more code to rewrite. No skin off my back.

But you need to relax a little. This isn’t a war. It isn’t a fight. It’s just someone who has been down the road ahead of you trying to tell you what they learned. But you apparently already know it all so why bother posting here?

And what exactly could happen, if you extract a compressed file?

Apparently an exploit you aren’t familiar with.

Again man, cool off a little. We’re all trying to be friends here. Just take a deep breath and approach this a little less aggressively and things might go better for you. That’s good advice for all of life.

Jeeeeeez @thor4115, who peed in your Cornflakes? Get the chip off your shoulder.