how to program a button that is pressed once in the arduino environmet

 how do you make a press once button because i know how to use the if statement with toggle switches but what im asking is how do you tell arduino if this is pressed once do all of this?

if that doesnt make sence comment saying ?que?

unarat7 years ago
I answered wrong earlier so I removed my posts. This is the example code found in the Arduino examples.


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter

    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
    else {
      // if the current state is LOW then the button
      // wend from on to off:

    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;

leotcol7 years ago
Make a bool or boolean variable. I'm not very familiar with the arduino code but I know C.

Then you can initialise that variable to false. Then if somebody presses the button then assign the variable to true. Then it would stay true the entire time, as nothing would tell it to be false even though to button wouldn't be pressed constantly.

Then have your code you want executed inside an if statement checking whether the boolean variable is true.
Jayefuu7 years ago
Are you saying you want to tell if the button is pressed once, twice or three times etc and do a different thing depending on how many times it is pressed?

Or are you trying to debounce the button?
Set a boolean flag in the program.
If <button pressed> myflag=true

If nothing else clears the flag, the button effectively stays pressed.