Double- and tripple clicking with buttom

Hi!

I havn´t been working with Arduino for that loog and have some trouble with the push-buttoms.

I´m trying to create a code in wich a buttom is pressed a number of times to turn on or turn off an LED.

By clicking tree times on the buttom the LED is turned on and clicking another two times shoud turn it off.

My question is how to do this in an easy way, I checked several Youtube clips and other webpages without fiding the information that is relevant and understandable according to the knowledge I have.

So far, my code looks like this:

int LED = 13;
int Button = 7;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(Button, INPUT);
}

void loop()
{
ButtomValue = digitalRead(Button);

if ( ?? )
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}

}

Google for "debounce", in the examples you'll find you'll see how the millis() function is used to time events. Once you have the button debounced, just store the time of the last push. If the last push is within the double-click time you have an additional push (so increase the push-counter), otherwise you can activate the action you stored for the current value of the push-counter and reset it.

I searched for "debounce" and found this webpage:

I checked the video and they used a code from the example list in the Arduino program which I copied.

Now I can turn the LED on and off by clicking at the buttom one time. How can I change the number of clickes needed in this code?

const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledState);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);

// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}

Please use code tags!

This code part:

// only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }

currently toggles the LED. Now change that to not toggle the LED but simple store the time:

// only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        lastButtonPush = millis();
        pushCount++;
      }

and later on, just above the "set the LED" comment:

    if (pushCount && millis() - lastButtonPush > doubleClickTimeout) {
      if (pushCount == 2) {
        ledState = HIGH;
      } else if (pushCount == 3) {
        ledState = LOW;
      }
      pushCount = 0;
    }

In the global variables part you need to include this code:

uint8_t pushCount = 0;
uint32_t lastButtonPush = 0;
uint32_t doubleClickTimeout = 200; // or whatever you find appropriate

This Thread about different button clicks may be of interest.

...R

I tried to understand what you wanted me to change in the code and this is what it turned out as. It was a little bit confusingbut I tried my best, unfortunately there is an error at the "if (reading != lastButtonState)"

int ledState = HIGH;          
int buttonState;             
int lastButtonState = LOW; 

uint8_t pushCount = 0;
uint32_t lastButtonPush = 0;
uint32_t doubleClickTimeout = 200;  


void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, ledState);
}


void loop() {

  int reading = digitalRead(buttonPin);
  
  if (reading != lastButtonState) {  
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
      lastButtonPush = millis();
        pushCount++;
      }
    }
  }

  if (pushCount && millis() - lastButtonPush > doubleClickTimeout) {
      if (pushCount == 2) {
        ledState = HIGH;
      } else if (pushCount == 3) {
        ledState = LOW;
      }
      pushCount = 0;
    }
  
  digitalWrite(ledPin, ledState);

  lastButtonState = reading;
}

thegreenfrog:
what you wanted me to change

Who is the "you" that you refer to?

...R

Well, that is probaly the user named "Pylon" who helped me a couple of hours ago...

thegreenfrog:
Well, that is probaly the user named "Pylon" who helped me a couple of hours ago...

Aren't you sure ? :slight_smile:

More seriously, it is always useful to make it clear which Reply (or which person's Reply) you are responding to.

...R

Original Post:

By clicking tree times on the buttom the LED is turned on and clicking another two times shoud turn it off.

Try this:

Dowload this library

The sketch:

#define KEY_PIN 7
#define LED_PIN 13
#define REACTION_TIME 500

#include "SwitchPack.h"
DoubleClick key(KEY_PIN, PULLDOWN, REACTION_TIME);


//setup==========
void setup() {
  key.begin();
  key.setMaxClicks(3);
  pinMode(LED_PIN, OUTPUT);
}

//loop=======================
void loop(){
  if (key.clickCount() == 3)  digitalWrite(LED_PIN, HIGH);
  if (key.clickCount() == 2)  digitalWrite(LED_PIN, LOW);
}

P.S. Yes, the switch is debounced.

You forgot some code from your previous sketch:

const int buttonPin = 7;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

With this addition it compiles.