0

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()
Bikee
  • 1,197
  • 8
  • 21
TGFoxy
  • 39
  • 5

1 Answers1

1

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.

EbraHim
  • 2,279
  • 2
  • 16
  • 28
  • @TGFoxy You may want to accept my answer as a accepted answer by clicking on the Tick mark at the left side of the answer :) – EbraHim Apr 24 '16 at 09:32