I need to monitor hall sensors for position encoding with up to 350 changes per second.

Alright guys, I need to monitor hall sensors for position encoding with up to 350 changes per second. My current Python program watches GPIO pins but uses too much CPU. Here's my polling loop:
REFRESH_RATE = .0005

while True:
new_p1_state = GPIO.input(hall_p1)
new_p2_state = GPIO.input(hall_p2)

if new_p1_state != p1_state or new_p2_state != p2_state:
if p1_state == GPIO.HIGH:
if new_p2_state == GPIO.HIGH:
position -= 1
else:
position += 1
else:
if new_p2_state == GPIO.LOW:
position -= 1
else:
position += 1

p1_state = new_p1_state
p2_state = new_p2_state

time.sleep(REFRESH_RATE)
REFRESH_RATE = .0005

while True:
new_p1_state = GPIO.input(hall_p1)
new_p2_state = GPIO.input(hall_p2)

if new_p1_state != p1_state or new_p2_state != p2_state:
if p1_state == GPIO.HIGH:
if new_p2_state == GPIO.HIGH:
position -= 1
else:
position += 1
else:
if new_p2_state == GPIO.LOW:
position -= 1
else:
position += 1

p1_state = new_p1_state
p2_state = new_p2_state

time.sleep(REFRESH_RATE)
How can I make this more efficient? Should I use a different language or tool? If so, which one? @Middleware & OS
Solution:
@Sterling Your program keeps checking the sensors, wasting power. Try letting the sensors interrupt the program only when they change, or use a separate task to handle them. C/C++ might be faster than Python for this job.
Jump to solution
2 Replies
Solution
Dtynin
Dtynin6mo ago
@Sterling Your program keeps checking the sensors, wasting power. Try letting the sensors interrupt the program only when they change, or use a separate task to handle them. C/C++ might be faster than Python for this job.
Sterling
Sterling6mo ago
So I guess it's more so that using python is the cause ? @Dtynin
Want results from more Discord servers?
Add your server