Now I'm no professional programmer but even I know that this code I wrote is rough. But it works so ¯\_(ツ)_/¯
So, here's some suggestions, starting with little things that just tidy things up:
* You should create some static constant strings for your menus. You can join the multiple print statements together by just adding more newlines to your longer string. In the code you can then print the whole thing with display.println(MENU1);
* Similarly, 60*1000UL is a constant you can reuse, MILISECONDS_IN_MINUTE perhaps?
* Put the repetitive display code in a separate function
So it'd be something like:
page = 1;
//check each button
//either update minutes or change the page
I mean, you have the right instinct, because without any delay it would be incredibly CPU intensive to loop as fast as possible.
The lazy way to do this is to have the lowest delay that doesn't cause resource usage problems. The problems won't be as severe if any delay would be too fast to perceive. Humans can easily perceive anything longer than 100 ms.
The right way to do it is outside my area of expertise.
Here's a fairly simple program that uses the OneButton library and state tracking to handle a multi-level menu.
It's not worried about CPU usage though.
attachInterrupt(BUTTON_A, ClickedA, HIGH);
AButtonClicked = true;
@cyberfarmer @preflex One more habit you should form is that if you write the same code twice (and especially if you write it 3+ times), then you should break that code off into a function. It's essential for readability.
If you have reservations about the performance of functions, remember that the compiler will often optimize them out of existence, creating the hard to read code you would have written to maximize performance from the clean, easy to understand code you actually wrote.
re: Bad Code
re: Bad Code
Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.