controlling motor servo with 2 push buttons and raspberry an
controlling motor servo with 2 push buttons and raspberry and also phyton interface ? Please help me :D Thanks.
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
Write python code so that the motor will begin at 90 degrees and the buttons will add or subtract from this angle so that the motor can be set in any position. You can use the code above as a starting point for your code
Solution
#!/usr/bin/python
# Import required libraries
import sys
import time
import RPi.GPIO as GPIO
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO signals to use
# Physical pins 11,18,17,19
# GPIO17,GPIO22,GPIO23,GPIO24
StepPins = [18,24,11,21]
# Set all pins as output
for pin in StepPins:
print \"Setup pins\"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
# Define advanced sequence
# as shown in manufacturers datasheet
Seq = [[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]
StepCount = len(Seq)
StepDir = 1 # Set to 2 or 3 for clockwise
# Set to -2 or -3 for anti-clockwise
# Read wait time from command line
if len(sys.argv)>1:
WaitTime = int(sys.argv[1])/float(2000)
else:
WaitTime = 20/float(2000)
# Initialise variables
StepCounter = 0
# Start main loop
while True:
print StepCounter,
print Seq[StepCounter]
for pin in range(0,6):
xpin=StepPins[pin]# Get GPIO
if Seq[StepCounter][pin]!=0:
print \" Enable GPIO %i\" %(xpin)
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
StepCounter += StepDir
# If we reach the end of the sequence
# start again
if (StepCounter>=StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount+StepDir
# Wait before moving on
time.sleep(WaitTime)

