News for hackers. Stuff that matters.
Re:Show HN: I've built self-opening trash bin, I relax mysel... by ivanilves on Tuesday September 01, 2020 @05:59AM (#24340069)
Thanks for your expertise, you are very welcome to propose any real improvements, if you can ;)
Re:Show HN: I've built self-opening trash bin, I relax mysel... by shoo on Tuesday September 01, 2020 @08:14AM (#24340934)
ivanilves, thank you for writing this up and sharing the code & the video of the end result, it is a fun project!

As a thought experiment I've had a play to see what the state machine logic might look like if we try to replace the bool state variables with a set of explicit finite states, and to push the state transition logic into a pure function. Results are here: https://gist.github.com/fcostin/851c1b4d1e3cb75ba972408151f1...

It is more verbose, but one advantage of structuring it like this (that i've only partially succeeded at) is that state transitions only happen at most once per each call to advance_state. Might make it easier to read through and follow the logic. It also makes the state transition logic easy to unit test, since it is pure-functional instead of being mixed together with sensing and doing actions (unsure how much of a concern or possibility unit testing is with ino files, I've never done any arduino dev myself).

Not sure if this is anything like what SomeoneFromCA was thinking.

Re:Show HN: I've built self-opening trash bin, I relax mysel... by ivanilves on Wednesday September 02, 2020 @02:47AM (#24350661)
Thanks for your code, looks like this can help improving my state machine indeed. I'll try to apply that approach when I get my next slice of free time ;)
Re:Show HN: I've built self-opening trash bin, I relax mysel... by SomeoneFromCA on Tuesday September 01, 2020 @06:03AM (#24340087)
Sure, no problems, will give you feedback later, but here is one recommendation: combine group of booleans frequently used together in conditions into a new separate boolean variable. Say, for example, bool isMoving = (isOpening or isClosing).
Re:Show HN: I've built self-opening trash bin, I relax mysel... by ivanilves on Tuesday September 01, 2020 @07:07AM (#24340502)
boolean grouping sounds neat, thanks!