Advertisement
Schupp

Taster entprellen

Jan 27th, 2021 (edited)
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int test1 = 0;
  2. boolean blocklast = false;
  3. unsigned long MB[6] = {34, 39, 35, 0, 5, 6};
  4. unsigned long MyPreviousMillis[6], tempstate[6], lastAtm, tm;
  5. unsigned long laststate[6] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
  6.  
  7. void setup() {
  8.   // put your setup code here, to run once:
  9.   for (int MyButton = 0; MyButton <= 1; MyButton++)
  10.     pinMode(MB[MyButton], INPUT);
  11.   Serial.begin(115200);
  12. }
  13.  
  14.  
  15. void loop() {
  16.   tm = millis();
  17.   if ((tm - lastAtm) >=  100UL )                        //Abtastrate //Entprellung
  18.   {
  19.     lastAtm = tm; //Letzte abtastung setzen
  20.     if ((!tempstate[0] && !laststate[0] && !tempstate[1] && !laststate[1]))
  21.     {
  22.       test1 = 0;
  23.       laststate[0] = HIGH;
  24.       laststate[1] = HIGH;
  25.       tempstate[0] = HIGH;
  26.       tempstate[1] = HIGH;
  27.  
  28.     } else
  29.       for (int MyButton = 0; MyButton <= 1; MyButton++) { //Buttons durchgehen
  30.         tempstate[MyButton] = digitalRead(MB[MyButton]);  //Button einlesen
  31.         if ( tempstate[MyButton] == LOW) {                //Gedrückt?
  32.           if (!laststate[MyButton]) {                     //War der Button vorher schon gedrückt?
  33.             if ( tm - MyPreviousMillis[MyButton] >= 200) {//wiederholzeit bei gedrückt
  34.               MyPreviousMillis[MyButton] = tm;
  35.               blocklast = true;                           //Blockiere beim loslassen einen weiteren Auslöser
  36.               if (MyButton == 0)Serial.println(--test1); else if (MyButton == 1)Serial.println(++test1); //Mache hier was für den jeweiligen Button, bestenfalls switch-case
  37.             }
  38.           } //Ende:War der Button vorher schon gedrückt?
  39.           laststate[MyButton] = tempstate[MyButton];
  40.         } else {
  41.           if (laststate[MyButton] == LOW) { //War der Button vorher gedrückt?
  42.             if (MyButton == 0 && !blocklast)Serial.println(--test1);
  43.             if (MyButton == 1 && !blocklast)Serial.println(++test1);
  44.             blocklast = false;
  45.             laststate[MyButton] = HIGH;
  46.           }
  47.         }
  48.       }
  49.   }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement