Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ButtonAEvents
- long prevA = 0;
- long prevB = 0;
- int buttonStateA = 0; // 0 = not pressed --- 1 = long pressed --- 2 short pressed
- int buttonStateB = 0; // 0 = not pressed --- 1 = long pressed --- 2 short pressed
- int ButtonAPin = 2;
- int ButtonBPin = 3;
- int DURATION_A_IN_MILLIS = 1000;
- int DURATION_B_IN_MILLIS = 1000;
- //ONE TIME SETUP
- void setup(){
- pinMode(ButtonAPin, INPUT);
- pinMode(ButtonBPin, INPUT);
- Serial.begin(9600);
- }
- //LOOP BEGIN
- void loop(){
- //BUTTON A CHECK
- buttonStateA = 0;
- if(digitalRead(ButtonAPin)){
- prevA = millis();
- buttonStateA = 1;
- while((millis()-prevA)<=DURATION_A_IN_MILLIS){
- if(!(digitalRead(ButtonAPin))){
- buttonStateA = 2;
- break;
- }}}
- if(!buttonStateA){
- // Nothin pressed
- }else if(buttonStateA == 1){
- //LONG PRESS
- Serial.println("LongpressA");
- digitalWrite(A1, HIGH);
- delay(100);
- }else if(buttonStateA ==2){
- //SHORT PRESS
- digitalWrite(A1, HIGH);
- Serial.println("ShortpressA");
- delay(100);
- }
- //BUTTON B CHECK
- buttonStateB = 0;
- if(digitalRead(ButtonBPin)){
- prevB = millis();
- buttonStateB = 1;
- while((millis()-prevB)<=DURATION_B_IN_MILLIS){
- if(!(digitalRead(ButtonBPin))){
- buttonStateB = 2;
- break;
- }}}
- if(!buttonStateB){
- // Nothin pressed
- }else if(buttonStateB == 1){
- //SHORT PRESS
- Serial.println("LongpressB");
- delay(100);
- }else if(buttonStateB ==2){
- //LONG PRESS
- digitalWrite(A1, LOW);
- Serial.println("ShortpressB");
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement