Go Down

Topic: Arduino push button Double Click function (Read 173 times) previous topic - next topic

The_Little_Cousin

Hello Folks,
It took me some time to do the following script for a simple Double Click function that suits my needs and I would like to share the following codes with you.
Am using one button to turn ON/OFF, 2 LED.


Code: [Select]
// set PushButton pin number
const int buttonPin = 2;

// set LED pin numbers
const int LED1 = 8;
const int LED2 = 9;

// set LED pin 13
const int LEDP13 = 13;

//initialising led
int LED1Status = LOW;
int LED2Status = LOW;

int buttonState;

unsigned long timePress = 0;
unsigned long timePressLimit = 0;
int clicks = 0;

void setup() {
  // put your setup code here, to run once:
 
  // initialize the LED pin as an output:
  pinMode(buttonPin ,INPUT);
 
  // initialize the pushbutton pin as an input:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
 
  Serial.begin(9600);
}

void loop() {
 
  // put your main code here, to run repeatedly:
 
  buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH){
    delay(200);
    //Serial.println("Button Pressed");
   
    if (clicks == 0) {
    timePress = millis();
    timePressLimit = timePress + 1000;   
    clicks = 1;
    }

    else if (clicks == 1 && millis() < timePressLimit){
      Serial.println("Button Pressed twice");
     
     //Double Press Action
     if (LED2Status == LOW){
      digitalWrite(LED2, HIGH);
      LED2Status = HIGH;
     }

     else if (LED2Status == HIGH){
      digitalWrite(LED2, LOW);
      LED2Status = LOW;
     }   

     //set variables back to 0
      timePress = 0;
      timePressLimit = 0;
      clicks = 0;     
    }   
  }

  if (clicks == 1 && timePressLimit != 0 && millis() > timePressLimit){
     Serial.println("Button Pressed Once");
     timePress = 0;
     timePressLimit = 0;
     clicks = 0;

     //Double Press Action
     if (LED1Status == LOW){
      digitalWrite(LED1, HIGH);
      LED1Status = HIGH;
     }

     else if (LED1Status == HIGH){
      digitalWrite(LED1, LOW);
      LED1Status = LOW;
     }
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy