from machine import Pin, 
import time


print("Button Handler Class")

class buttonHandler:
    def __init__(self,pin):
        self.pin = Pin (pin, Pin.IN, Pin.PULL_UP)
        self.tOn = 0
        self.tmp = 0
        self.evnt = None 
    
    def event(self):
        if self.pin():
            if (self.tOn <10):    # does the key get pressed for at least 10 scans ?
                self.tOn=0        # no, it was just a bounce, ignore
                return None                
            else:    
                self.tmp=self.tOn  # yes, save amount of scans 
                self.tOn = 0       # and reset counter           
                
                if (self.tmp < 100): # does the key get pressed for less than 100 scans ?
                    return 1         # yes, it was a short press
        else:
            if (self.tOn < 500):
                self.tOn = self.tOn + 1  # restrict max value, just in case
                if self.tOn == 100:
                    return 2 # return long only once
            return None


b = buttonHandler (12)   # instantiate and init pin



while 1:
    bte = b.event()
    if (bte):
        if (bte==1):
            print ("short")
        elif (bte==2):
            print ("long")    

            
    time.sleep_ms(5)
Loading
esp32-devkit-c-v4