December 20, 2023, 18:42
py from RPi import GPIO from time import sleep clk = 16 dt = 20 GPIO.setmode(GPIO.BCM) GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) counter = 0 clkLastState = GPIO.input(clk) try: while True: clkState = GPIO.input(clk) dtState = GPIO.input(dt) if clkState != clkLastState: if dtState != clkState: counter += 1 else: counter -= 1 print (counter) clkLastState = clkState sleep(0.01) finally: GPIO.cleanup()
py #importeer GPIO en time package import RPi.GPIO as GPIO import time #gebruik BCM mode voor de pin nummering (conform breakout) GPIO.setmode(GPIO.BCM) #zet de pin als input GPIO.setup(4, GPIO.IN) # loop through 20 times for i in range(20): print(GPIO.input(4)) time.sleep(1) GPIO.cleanup()