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.
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.
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
Joprp05:
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.
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.
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.