theautomationguy:
I am trying to end up with a buzzer which sounds for 1 second then is silent for 5 minutes after receiving an input from the relay output of the Photo electric beam, that can't be done with the blink without delay. I can write that sketch, but I don't know how to apply that sketch to what I am trying to achieve. It is very simple using PLC ladder logic.
It can be done using the principle that BWD teaches, how to use time.
The process below will do what you want and let you add many other "simultaneous" functions.
You could do something simpler that would block other code but I'm guessing that you don't want that.
You have a buzzer that should only sound under certain conditions:
- waiting for the button to be pushed, no sound ------- call this state 10, the default state
- when the button is pushed, sound for a time period -- call this state 20
- after that time period the buzzer is not to sound for another time period -- call this state 30
So you have an input, the button, and an output, the buzzer.
-- The button code should include debouncing and set or clear a button pushed flag after debounce. It should not change the state, the button may be pressed at any time. My favorite debounce is simply checking for change every time through loop and if it stays the same for 5 ms and is different than the button flag, change button flag but not until then. Yes, that means keeping track of the last pin read for the button.
-- The buzzer code should keep track of buzzer on/off and start the buzzer when (the state is 20 and the buzzer is off) else stop the buzzer when (the state is not 20 and the buzzer is on).
-- There should be a switch-case on state with time checks inside
case 10, if button pressed; set state to 20, set start time to millis(), set interval to 1000UL (1 second).
break
case 20, if the interval is up; set state to 30, set start time to millis(), set interval to 5 mins.
break
case 30, if the interval is up; set state to 10.
That's it. Each piece should be short, not interleave the logic of other pieces and easy to debug.
You could even tie leds to the states as process status indicators but give that a separate code block.