from machine import Pin, PWM,ADC #import the necessary modules Pin and ADC
import utime #used to set time and delays
#declaring variables and assigning them to their GP pins (only GP pin 26,27 and 28 have ADC capabilities)
x1Axis= ADC(Pin(27))
y1Axis= ADC(Pin(26))
button1=Pin(16, Pin.IN,Pin.PULL_UP)
Servo=PWM(Pin(17))
Servo.freq(50)
#create a continous loop that checks and prints the value of x,y and button
while True:
x1Value=int(x1Axis.read_u16()*180/65535) #Takes an analog reading and return an integer in the range 0-65535
y1Value=int(y1Axis.read_u16()*180/65535)
button1Value=button1.value() #value() is a class in Pin
print("x-axis :" + str(x1Value))
print("y-axis :" + str(y1Value))
if button1Value==1:
print("button not pressed")
utime.sleep(0.2)
else:
print("button pressed")
for i in range(2000000,1000000,-100000):
Servo.duty_ns(i)
utime.sleep(0.2)
if x1Value>40 and x1Value<140 and y1Value>40 and y1Value<140:
Servo.duty_ns(1500000)
utime.sleep(0.1)
elif x1Value<40:
for i in range(1500000,1000000,-100000):
Servo.duty_ns(i)
utime.sleep(2)
elif y1Value<40:
for i in range(2000000,1500000,-5000):
Servo.duty_ns(i)
utime.sleep(0.05)
elif x1Value>140:
for i in range(1000000,1500000,5000):
Servo.duty_ns(i)
utime.sleep(0.05)
elif y1Value>140:
for i in range(1500000,2000000,100000):
Servo.duty_ns(i)
utime.sleep(0.5)
# MID TERM ASSIGNMENT
# 1) TYPE ALL THE FOLLOWING CODES LINE BY LINE ON THONNY IDE
# 2) WHAT ARE ACTUATORS USED FOR IN ROBOTICS?
# 3) MENTION THE FUNCTIONS OF THE PINS IN A JOYSTICK ?
# 4) MENTION THE FUNCTIONS OF THE PINS IN A SERVO MOTOR ?
# 5) WHAT IS THE FULL MEANING OF PWM?
# 6) WHAT IS THE FULL MEANING OF ADC?
# 7) WHY DO YOU SET FREQUENCY AND DUTY CYCLE FOR PWM SIGNALS?