We always leave our bathroom fan on for too long, and in the winter months it sucks in cold air from outside in all the rooms of the apartment.
I had an idea to create a mechanical wall switch flipper which would allow us to select three runtime intervals for the fan. Obviously I could do this electronically, but I love the clickety-clack of old-school mechanical devices, hence my desire to flip the switch mechanically. I already have all the mechanical stuff figured out.
My Mission:
Have three different momentary-contact pushbuttons to control how much time to allow the fan to run.
Green button: Servo flips the switch on and shuts it off after 10 minutes
Yellow button: Servo flips the switch on and flips it back off after 20 minutes
Red button: For those "Code Red" trips to the bathroom after dinner at the local Mexican restaurant. Servo flips the switch on and flips it back off after 30 minutes.
Just seems like a fun project, and practical so we're not leaving the fan on for hours.
Is it feasible to use these three pushbuttons to actuate the servo for three different time intervals? I was thinking of a simplistic algorithm of 3 "if" statements followed by a simple pause depending on which button was pressed, but I have a feeling that this is the least elegant way to do it.
Any thoughts would be appreciated. It's pretty obvious I'm a rank beginner, and I'm doing my best to describe what I want to accomplish. Thanks!
You might get more satisfaction out of complicating it, for elegance or some other measurement.
And doing would leave you off better able to tackle the next project, with elegance, where a simple approach might just not be so good an idea.
It does mean you'd have trouble enhancing this device, but if it works, is deployed and becomes part of the background, again, who cares?
I have some really dodgy projects that would benefit from a visit or two, but they work, have for years and I'd prolly just start over if upgrades are considered.
Depends what you meant by "a simple pause". The most obvious way to get that pause is to use the delay() function. But what if someone presses a button while the pause/delay() is still running? Let's say 29 minutes after someone presses red, someone else, maybe the same person, presses red again. The press will be ignored (because that's what happens when you use the delay() function) and the fan will switch off one minute later.
Better to use millis() function instead of delay()!
And other good points to settle during the design phase.
Perhaps one button and a stack of LEDs, 10 minutes each out to an hour (dunno what kind of lame Mexican restaurants are in your neighborhood!), press the button N times for N * 10 minutes run time.
Wow, thank you for the code. I will certainly try it and experiment, and will make it my business to understand enums and constexpr.
Is your code utilizing the the internal pullup resistor, or must I use external ones? From my meager knowledge it looks like you're using the internal pullup for all three switches.
I wonder if there is a way to incorporate the user changing their mind? Let's say they enter the bathroom and push red, and then realize it was a false alarm and they want to change it to green or yellow. I'm no asking you to do all my work for me, but I'm wondering if there's a method to accommodate a change of mind.
Thanks. I was actually thinking of a reset button and eliminating the initial servo actuation and instead put in a little out-of-sight test button for a quick flip on and off to verify operation.