from machine import PWM, Pin
from time import sleep
from servo import Servo
from encoder import RotaryEncoder

servo1 = Servo(12)
servo2 = Servo(14)

# Define the sensor pin
sensor_pin = Pin(34, Pin.IN)

def rotation_handler(direction):
    if direction == RotaryEncoder.ROT_CW:
        servo1.rotate(9)
        servo2.rotate(-9)
    else:
        servo1.rotate(-9)
        servo2.rotate(9)

myEncoder = RotaryEncoder(26, 27, rotation_handler)

def main():
    # Read the sensor value
    sensor_value = sensor_pin.value()
    if sensor_value == 1:  # Sensor is pressed
        rotation_handler(RotaryEncoder.ROT_CW)
        sleep(1)
        rotation_handler(RotaryEncoder.ROT_CCW)
    sleep(0.1)

print("Hello, ESP32!")
if __name__ == '__main__':
    while True:
        main()