You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB
Python

3 years ago
from machine import Pin
import utime
MOTOR_LEFT_DIR = True # False if Reverse
MOTOR_RIGHT_DIR = True # False if Reverse
ENCODER_LEFT_COUNT = 0
ENCODER_RIGHT_COUNT = 0
ENCODER_LEFT_PIN = Pin(4, Pin.IN)
ENCODER_RIGHT_PIN = Pin(5, Pin.IN)
def handle_encoder_left(pin):
global ENCODER_LEFT_COUNT
if MOTOR_LEFT_DIR:
ENCODER_LEFT_COUNT += 1
else:
ENCODER_LEFT_COUNT -= 1
def handle_encoder_right(pin):
global ENCODER_RIGHT_COUNT
if MOTOR_RIGHT_DIR:
ENCODER_RIGHT_COUNT += 1
else:
ENCODER_RIGHT_COUNT -= 1
def main():
global MOTOR_LEFT_DIR
ENCODER_LEFT_PIN.irq(trigger=Pin.IRQ_RISING, handler=handle_encoder_left)
ENCODER_RIGHT_PIN.irq(trigger=Pin.IRQ_RISING, handler=handle_encoder_right)
while True:
print("LEFT: {} , RIGHT: {}".format(ENCODER_LEFT_COUNT, ENCODER_RIGHT_COUNT))
utime.sleep(3)
print("LEFT: {} , RIGHT: {}".format(ENCODER_LEFT_COUNT, ENCODER_RIGHT_COUNT))
utime.sleep(3)
print("LEFT: {} , RIGHT: {}".format(ENCODER_LEFT_COUNT, ENCODER_RIGHT_COUNT))
utime.sleep(3)
print("SWITCH DIR")
MOTOR_LEFT_DIR = not MOTOR_LEFT_DIR