Question about button press stuff and counters

So first off. Im horrible at explaining myself...

So what i want to do is have a button control a relay.

The button would be 3 presses. On, off and auto.

Where on is always on. Off is always off. And auto would be a different code.

So what would i use to control this?

Like ifs or state machine? I dont know what term im trying to figure out, if that makes sense.

Look into the state change detection example. The buttonPushCounter could be used to select the mode. Like 0 for deactivate the relay (off), 1 for activate the relay (on) and 2 for the other function (auto). If the count increases to 3, reset the count to 0. So the count starts at 0 (off), the next push increments to 1 (on), the next push increments to 2 (auto function) and on the next push the counter is reset to 0 (off). Use switch case, using the buttonPushCounter to select which function to do.

Millis and switch case are my weakness so this will be an adventure.

Thanks :slight_smile:

Remember to debounce that button, too (either in software or in hardware). Or you'll read a dozen presses when you try to press just once.

Hi,
You will also need some way of showing what mode the relay is in if you are only going to use one button.
Just one LED would do it.

LED, OFF for relay OFF.
LED ON constantly for Relay ON.
LED FLASHING for relay in AUTO.

Tom.. :slight_smile:

Take a look at this example:

PushButton-debounce.ino

Pieter

jokefox:
The button would be 3 presses. On, off and auto.

It is perfectly possible to write a program to act like that but I suspect a human user - including you, but especially someone who is not familiar with your "machine" will find it very confusing in practice.

For example I could interpret what you have written to mean a cycle of ON OFF AUTO ON OFF AUTO ON OFF etc. And that would mean that there is no means to go from AUTO to OFF.

And what happens if you want to go from ON to OFF and accidentally give an extra press and go to AUTO?

A three-position switch with OFF in the centre seems to me much more sensible. Or three buttons.

...R

Robin2:
It is perfectly possible to write a program to act like that but I suspect a human user - including you, but especially someone who is not familiar with your "machine" will find it very confusing in practice.

For example I could interpret what you have written to mean a cycle of ON OFF AUTO ON OFF AUTO ON OFF etc. And that would mean that there is no means to go from AUTO to OFF.

And what happens if you want to go from ON to OFF and accidentally give an extra press and go to AUTO?

A three-position switch with OFF in the centre seems to me much more sensible. Or three buttons.

...R

i have a power bar that has a "mode" button that just cycles threw the on, off, auto , so i figured it would be a good idea, but a 3 state button sounds alot easier for my dumb ass,

i also wanted to release this code once finished, so i figured the less wierd items people had to buy the better

jokefox:
i also wanted to release this code once finished, so i figured the less wierd items people had to buy the better

A Single Pole Double Throw Centre OFF switch is not that wierd.
ON - OFF - ON toggle switch.
google spdt centre off switch
SPDT-single-pole-centre-off.jpg
Tom.... :slight_smile:

jokefox:
i also wanted to release this code once finished, so i figured the less wierd items people had to buy the better

That sounds like a great reason to make the user-interface simple - with a 3 position switch. As @TomGeorge has said centre off switches are very easily obtained.

...R