Author Options:

Is there a way to put have a button push in a set of time (arduino)? Answered

I want to make a treat disperser for my dog that he can just push a button and get one, but only allowed one within a set amount of time so he doesn't just sit there hitting the button all day.



Best Answer 5 years ago

Use the button sketch. When the button is pressed, instead of lighting just an LED activate the dispencer. Then set a long delay after that for the amount of time you want him to wait between treats.


5 years ago

If you want an absolute time frame (e.g. a certain time of day) I am not sure other than using an external clock (a real clock that will feed the arduino time info).

For a relative time limit, (e.g. only allow the dog to receive a treat X minutes after last requesting one) then I would use a a digitalRead function, and if the value returns 1 or HIGH, have a delay for several hundred seconds. (3,600,000ms = 3,600 seconds = 1 hour)


5 years ago

Personally, I am not a big fan of using delay. I prefer to set events, so the arduino continues to cycle instead of waiting for some input. Use the millis function to get your time intervals, and let the loop go. It is a little more work, but better in the long run if you will be adding any other code events or features later on.