Post A7E9PdLbSFwaEcAVc0 by urusan@fosstodon.org
(DIR) More posts by urusan@fosstodon.org
(DIR) Post #A7E2VWzvHBoGbFtPrk by cyberfarmer@fosstodon.org
2021-05-14T01:11:04Z
1 likes, 0 repeats
Now I'm no professional programmer but even I know that this code I wrote is rough. But it works so ¯\_(ツ)_/¯
(DIR) Post #A7E2VXRZcNvZz0pUxs by urusan@fosstodon.org
2021-05-14T02:01:30Z
0 likes, 0 repeats
@cyberfarmer What language is this? C++?
(DIR) Post #A7E2e5s9Us6Z4vLCyW by meowski@fluf.club
2021-05-14T02:03:05.557261Z
0 likes, 0 repeats
@cyberfarmer why? is wuttwe babby scawed of whiwe twue statements uwu. this wooks funky wunky if i had more time i would bother to see what it's actually doing
(DIR) Post #A7E3MRycftHaNLWCrA by preflex@fosstodon.org
2021-05-14T02:08:30Z
1 likes, 0 repeats
@meowski @cyberfarmer It's not bad because "while(true)". It's bad because "delay".
(DIR) Post #A7E3MSPv2P7Jk0I0P2 by meowski@fluf.club
2021-05-14T02:11:06.033843Z
0 likes, 0 repeats
@preflex @cyberfarmer ok well what's the delay for?
(DIR) Post #A7E3OklRnIiBDtSCfo by preflex@fosstodon.org
2021-05-14T02:11:29Z
0 likes, 0 repeats
@urusan @cyberfarmer It's embedded C++. Like for an arduino or some other microcontroller.
(DIR) Post #A7E3softorpW2TC5oW by preflex@fosstodon.org
2021-05-14T02:14:23Z
0 likes, 0 repeats
@meowski @cyberfarmer Why is he using it? I dunno. Because he didn't want to write proper timers and state tracking.Unless he set some interrupt (also a bad idea), the system is non-responsive during delay(). He has to be holding down the button when the delay ends in order to get to the next step.
(DIR) Post #A7E3sp8G7QVzSQSk1A by meowski@fluf.club
2021-05-14T02:16:56.494634Z
0 likes, 0 repeats
@preflex @cyberfarmer i don't think we have enough information to tell just how bad this code is. it's out of context
(DIR) Post #A7E5W8BHKVzTmfBLFI by preflex@fosstodon.org
2021-05-14T02:34:15Z
1 likes, 0 repeats
@meowski @cyberfarmer No. I think we do have enough information. No external context is necessary. There isn't any context where this is good, but there are some contexts where it can work but be finicky and waste lots of time.
(DIR) Post #A7E5W8a5qFq91cn9vM by meowski@fluf.club
2021-05-14T02:35:16.000570Z
0 likes, 0 repeats
@preflex @cyberfarmer u gay
(DIR) Post #A7E8DI8oNE7n2FjiHg by urusan@fosstodon.org
2021-05-14T03:05:26Z
0 likes, 0 repeats
@cyberfarmer @preflexSo, 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
(DIR) Post #A7E8dpKjaW5lVaxxke by urusan@fosstodon.org
2021-05-14T03:10:14Z
0 likes, 0 repeats
@cyberfarmer @preflex The biggest change though would be to add another variable to track your page number, instead of using nested while loops to track which page you're on.So it'd be something like:page = 1;while(true){ display_page(page); //check each button //either update minutes or change the page}
(DIR) Post #A7E8zkELF3TM3JQXxo by preflex@fosstodon.org
2021-05-14T03:14:12Z
0 likes, 0 repeats
@urusan @cyberfarmer What Urusan said. Also track the state of your buttons and your depth in the code.The OneButton library is handy for this if you want to be lazy while having better code.https://github.com/mathertel/OneButton
(DIR) Post #A7E9PdLbSFwaEcAVc0 by urusan@fosstodon.org
2021-05-14T03:18:52Z
0 likes, 0 repeats
@cyberfarmer @preflex As preflex pointed out elsewhere, delay is a bad idea.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.
(DIR) Post #A7EAHLcPaQ16t3dx8C by preflex@fosstodon.org
2021-05-14T03:28:35Z
0 likes, 0 repeats
@urusan @cyberfarmer To keep CPU usage down, you can keep shorter delays, but attach an interrupt before the delay and detach it either on the click or at the end of the delay.
(DIR) Post #A7EAKtNSWomCSOL7Mu by preflex@fosstodon.org
2021-05-14T03:29:14Z
0 likes, 0 repeats
@urusan @cyberfarmer Do _not_ attach interrupts to clicks in setup() unless you want everything to go haywire.
(DIR) Post #A7EAuZkIzlNnzo2b56 by preflex@fosstodon.org
2021-05-14T03:35:41Z
0 likes, 0 repeats
@urusan @cyberfarmer See:https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/in short:attachInterrupt(BUTTON_A, ClickedA, HIGH);delay(100);detachInterrupt(BUTTON_A);if(AButtonClicked){//do stuff}...void ClickedA(){AButtonClicked = true;}
(DIR) Post #A7EBIgiYj3HyZJioPw by urusan@fosstodon.org
2021-05-14T03:40:01Z
0 likes, 0 repeats
@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.