Here is the beginning of my code import RPi.GPIO as GPIO from time import sleep
ID: 2079982 • Letter: H
Question
Here is the beginning of my code
import RPi.GPIO as GPIO
from time import sleep
pin = 18 farLeft = 7
farRight = 7
GPIO.setmode(GPIO.BCM) # Set the pin numbering to those on the board
GPIO.setup(pin, GPIO.OUT) # Set the chosen pin(18) to output mode
pwm = GPIO.PWM(pin,50) # Set up a 50Hz PWM signal on the chosen pin pwm.start(5)
#pwm.ChangeDutyCycle(farRight) # You should only use one of these two lines at
pwm.ChangeDutyCycle(farLeft) # a time. Find the left limit and then change to # the right.
sleep(1) pwm.stop() #to stop PWM
GPIO.cleanup() # clean up GPIO channels that your script has used.
Note #that GPIO.cleanup() also clears the pin numbering system
Explanation / Answer
import RPi ## Import GPIO Library.
import time ## Import ‘time’ library for a delay.
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering.
GPIO.setup(22, GPIO.OUT) ## set output.
pwm=GPIO.PWM(22,100) ## PWM Frequency
pwm.start(5)
variable1=10
duty1= float(Variable1)/10 + 2.5 ## variable To Duty cycle Conversion
variable2=160
duty2= float(variable2)/10 + 2.5
ck=0
while ck<=5:
pwm.ChangeDutyCycle(duty1)
time.sleep(0.6)
pwm.ChangeDutyCycle(duty2)
time.sleep(0.6)
ck=ck+1
time.sleep(1)
GPIO.cleanup()