Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //ButtonAEvents
  3.  
  4.  
  5.  
  6. long prevA = 0;
  7. long prevB = 0;
  8. int buttonStateA = 0;  // 0 = not pressed   --- 1 = long pressed --- 2 short pressed
  9. int buttonStateB = 0;  // 0 = not pressed   --- 1 = long pressed --- 2 short pressed
  10. int ButtonAPin = 2;
  11. int ButtonBPin = 3;
  12. int DURATION_A_IN_MILLIS = 1000;
  13. int DURATION_B_IN_MILLIS = 1000;
  14.  
  15.                                                                               //ONE TIME SETUP
  16.  
  17. void setup(){
  18.   pinMode(ButtonAPin, INPUT);
  19.     pinMode(ButtonBPin, INPUT);
  20.   Serial.begin(9600);
  21. }
  22.  
  23.  
  24.                                                                               //LOOP BEGIN
  25.  
  26.  
  27. void loop(){
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.                                                                               //BUTTON A CHECK
  36.   buttonStateA = 0;
  37.   if(digitalRead(ButtonAPin)){
  38.     prevA = millis();
  39.     buttonStateA = 1;
  40.     while((millis()-prevA)<=DURATION_A_IN_MILLIS){
  41.       if(!(digitalRead(ButtonAPin))){
  42.         buttonStateA = 2;
  43.         break;
  44.       }}}
  45.  
  46.   if(!buttonStateA){
  47.     // Nothin pressed
  48.   }else if(buttonStateA == 1){
  49.                                                                               //LONG PRESS
  50.  
  51.  
  52.     Serial.println("LongpressA");
  53.     digitalWrite(A1, HIGH);  
  54.     delay(100);
  55.    
  56.   }else if(buttonStateA ==2){
  57.                                                                               //SHORT PRESS
  58.     digitalWrite(A1, HIGH);    
  59.     Serial.println("ShortpressA");
  60.     delay(100);
  61.  
  62.  
  63.  
  64.                                                                            
  65.   }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.                                                                               //BUTTON B CHECK
  87.   buttonStateB = 0;
  88.   if(digitalRead(ButtonBPin)){
  89.     prevB = millis();
  90.     buttonStateB = 1;
  91.     while((millis()-prevB)<=DURATION_B_IN_MILLIS){
  92.       if(!(digitalRead(ButtonBPin))){
  93.         buttonStateB = 2;
  94.         break;
  95.       }}}
  96.  
  97.   if(!buttonStateB){
  98.     // Nothin pressed
  99.   }else if(buttonStateB == 1){
  100.                                                                               //SHORT PRESS
  101.  
  102.                             Serial.println("LongpressB");
  103.                             delay(100);
  104.                                                  
  105.  
  106.  
  107.   }else if(buttonStateB ==2){
  108.                                                                               //LONG PRESS
  109.   digitalWrite(A1, LOW);
  110.   Serial.println("ShortpressB");
  111.   delay(100);  
  112.                                                                          
  113.   }
  114.  
  115.  
  116.  
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement