Go Down

Topic: Flip Flop (on-off) button (Read 141 times) previous topic - next topic

Joprp05

I'll go straight to the point to make it easier : I want to make a button that can act like a light switch; press once, one thing happen/starts to happen, press another time the said thing stops to happen.

You may say that there are LOADS of sites saying how to do this, and you aren't wrong at all, but they all majorly cover only a LED or two.

I want to create one so called "switch button" that can work OUTSIDE of that spectrum (for isntance, selecting if i'm controlling the rotation of a servo or controlling the brightness of a led through a poteitiometer) . The problem is, as you may already have figured it out, i don't know how to do it.

I would love to someone to at least give me an insight as to how to do this, because I'm clearly not able to do this myself.

Thanks for taking your time reading this,
Bye bye.

wvmarle

Well, instead of connecting an LED to that pin, connect your servo or whatever (with appropriate power driver, of course). For the code it doesn't matter what's connected to the pin.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

vinceherman

You are using a push button to select between 2 states.

For the LED example, the 2 states are
- LED on
- LED off

For a servo example, the 2 states may be
- rotate left and stop
- rotate right and stop

State machines are a frequent construct for arduino projects.  It would be good reading for you.

Joprp05

You are using a push button to select between 2 states.

For the LED example, the 2 states are
- LED on
- LED off

For a servo example, the 2 states may be
- rotate left and stop
- rotate right and stop

State machines are a frequent construct for arduino projects.  It would be good reading for you.
Oh thank you dude. I'm going to do that right now.
And yes, selecting the states is what i'm trying to do, thanks for summarizing it for me :)

saildude

It looks like there are a couple of solutions here and a link to a tutorial (not sure if the tutorial link works sorry forgot to double check)

http://forum.arduino.cc/index.php?topic=160474.0

Doing a simple push on / push off here sucks - I used to do industrial PLC's and it was literally one rung (line) of logic - it has been a sore spot with me for some time on this platform, maybe someone will have a better solution for you

Good luck

MorganS

I'll go straight to the point to make it easier
What was the point? You want a light switch or you want a dimmer knob or you want a servo? Pick one or more but let us know which ones are important and which ones are hypothetical examples.

A pushbutton is a good example of a state machine. When it's in the ON state, any push of the button changes to the OFF state. In the OFF state, the button does something different: it turns it on. So the action of the button depends on what has happened previously.

You can add more states. Holding the button down may slowly dim the light up or down in intensity. The direction it's going will certainly depend on what happened before.

However a "simple" button is not that simple from the Arduino's point of view. There's some hidden states which the Arduino must use. The Arduino can react to a button so unbelievably fast that it doesn't remember by itself that the button wasn't pushed before and now it is - you have to add that memory in your program. Then there's the problem of contact "bounce" where a single push of the button may appear like 2 or 5 cycles of pushed-released-pushed-released in just a millisecond.
"The problem is in the code you didn't post."

Joprp05

Ok so I didn't clarify this very well, but I found the words to do so in another topic (therefore making this a useless topic); what I wanted was a button that made a state change, in other words, each time you press the button the state goes from 1 to 0, or  0 to 1.

I got my answer with this post : https://forum.arduino.cc/index.php?topic=410217.0

Basically, you create a boolean and invert that boolean each time the button is pressed (true to false, false to true), and make "if" statements to make whatever you want in determined state.

thanks for the help from everyone, and sorry for not being able to explain myself, as morganS pointed out.


that's about it. bye.

Go Up