import RPi.GPIO as GPIO import time import AirtablePancake at = AirtablePancake.at() p1 = 31 p2 = 29 GPIO.setmode(GPIO.BOARD) pin1 = GPIO.setup(p1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) pin2 = GPIO.setup(p2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) ena = 11 # Left motor enable (PWM) in1 = 13 # Left motor direction 1 in2 = 15 # Left motor direction 2 GPIO.setup(ena, GPIO.OUT) GPIO.setup(in1, GPIO.OUT) GPIO.setup(in2, GPIO.OUT) # Set all pins low to start (prevents movement at startup) GPIO.output(in1, GPIO.LOW) GPIO.output(in2, GPIO.LOW) # Create PWM instances (both motors at 50 Hz) motor1 = GPIO.PWM(ena, 50) # Left motor # Start PWM at 0% motor1.start(0) def forward(speed): GPIO.output(in1, GPIO.HIGH) GPIO.output(in2, GPIO.LOW) motor1.ChangeDutyCycle(speed) def back(speed): GPIO.output(in1, GPIO.LOW) GPIO.output(in2, GPIO.HIGH) motor1.ChangeDutyCycle(speed) def stop(): GPIO.output(in1, GPIO.LOW) GPIO.output(in2, GPIO.LOW) motor1.ChangeDutyCycle(0) def checkPosition(p1,p2,position): if GPIO.input(p1) == 0 and GPIO.input(p2) == 0: position = "moving" elif GPIO.input(p1) == 1: position = "retracted" elif GPIO.input(p2) == 1: position = "extended" return position def cookPancake(): print("cooking pancake") time.sleep(5) print("done cooking pancake") moving = 0 position = "extended" try: while True: # determine current position and airtable value airtable = at.checkValue("Cooking 1 Status") position = checkPosition(p1,p2,position) print(position) # if the pancake is done cooking, extend the fork if airtable == 4 and (position == "retracted"): position = "moving" forward(20) moving = 1 while position == "moving" or position == "retracted": position = checkPosition(p1,p2,position) moving = 0 stop() print("fork extended, motor off") # if the table is raised, retract the fork if airtable == 5 and position == "extended": position = "moving" forward(30) moving = 1 while position == "moving" or position == "extended": position = checkPosition(p1,p2,position) moving = 0 stop() print("fork extended, motor off") time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup() print("done")