###################################################
#
# This code is for the pi pico w to control a servo motor.
# The servo motor is connected to pin 0 on the pi.
# The code uses a PWM signal to control the servo motor,
# and makes the motor move alternately clockwise and anticlockwise.
#
# Author: Becky Tyler (2461535)
# Date: 17 March 2024
#
# Note: CoPilot AI was used to predict next words to save time typing
# full words with eye gaze
#
###################################################
# Import the necessary libraries
import machine
from time import sleep
from machine import Pin, PWM
print("test")
# Create a PWM object from the Pin 0
servo = PWM(Pin(0))
# Set the frequency of the PWM signal to 50Hz
servo.freq(50)
Up_Button = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_DOWN)
Down_Button = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)
# Create an infinite loop
while True:
print(Up_Button.value())
if Up_Button.value() == 1:
# Move the servo motor clockwise
for i in range(1000, 9000, 50):
# Set the duty cycle of the PWM signal to move the servo motor
servo.duty_u16(i)
# Wait for a short time
sleep(0.01)
else if Down_Button.value() == 1:
# Move the servo motor anticlockwise
for i in range(9000, 1000, -90):
# Set the duty cycle of the PWM signal to move the servo motor
servo.duty_u16(i)
# Wait for a short time
sleep(0.01)