-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotary.py
44 lines (35 loc) · 1.1 KB
/
rotary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import RPi.GPIO as GPIO
from time import sleep
# Define GPIO pins
CLK_PIN = 18
DT_PIN = 17
SW_PIN = 27
# Set GPIO mode
GPIO.setmode(GPIO.BCM)
# Set up GPIO pins
GPIO.setup(CLK_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(DT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Define a callback function for the encoder
def rotary_callback(channel):
global counter
if GPIO.input(DT_PIN):
counter += 1
print("Encoder twisted clockwise")
else:
counter -= 1
print("Encoder twisted counter-clockwise")
print("Counter:", counter)
# Define a callback function for the switch
def switch_callback(channel):
print("Switch pressed")
# Add event detection to the CLK_PIN and SW_PIN
GPIO.add_event_detect(CLK_PIN, GPIO.RISING, callback=rotary_callback)
GPIO.add_event_detect(SW_PIN, GPIO.FALLING, callback=switch_callback, bouncetime=300)
# Initialize counter variable
counter = 0
try:
while True:
sleep(0.1) # Sleep to reduce CPU usage
except KeyboardInterrupt:
GPIO.cleanup() # Clean up GPIO on CTRL+C exit