I am working on a small project involving servos on the Raspberry Pi
.
I wanted the servos to run for x amount of time then stop. Was trying out my code and am currently getting Invalid syntax on "def sleeper"
and have no idea why.
Also being new to Stackoverflow, I had some issues indenting the code, my apologies!
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
while True:
GPIO.output(7,1)
time.sleep(0.0015)
GPIO.output(7,0)
def sleeper():
while True:
num = input('How long to wait: ')
try:
num = float(num)
except ValueError:
print('Please enter in a number.\n')
continue
print('Before: %s' % time.ctime())
time.sleep(num)
print('After: %s\n' % time.ctime())
try:
sleeper()
except KeyboardInterrupt:
print('\n\nKeyboard exception received. Exiting.')
exit()
That's because you didn't wrote any except
block for the first try ... except
pair:
This may work as you want:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
try:
while True:
GPIO.output(7,1)
time.sleep(0.0015)
GPIO.output(7,0)
except:
pass
def sleeper():
while True:
num = input('How long to wait: ')
try:
num = float(num)
except ValueError:
print('Please enter in a number.\n')
continue
print('Before: %s' % time.ctime())
time.sleep(num)
print('After: %s\n' % time.ctime())
try:
sleeper()
except KeyboardInterrupt:
print('\n\nKeyboard exception received. Exiting.')
exit()
Check indentations please.