juliansilver
Posts: 7
Joined: Sun Jan 10, 2016 7:21 pm

Using RCtime.py as a switch syntax

Fri Jan 29, 2016 3:50 pm

Hi there I am starting to get to grips with GPIOs and python but have hit a syntax error. Please can someone correct my mistake so I can venture forth and If/then myself to the internet of things.

The err is:

pi@chickenpi:~ $ ./shut.py
File "./shut.py", line 26
if RCtime > 30000
^
SyntaxError: invalid syntax
pi@chickenpi:~ $


The code is based on the RCtime.py from adafruit as follows:

pi@chickenpi:~ $ cat shut.py
#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os

DEBUG = 1
GPIO.setmode(GPIO.BCM)

def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(0.1)

GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading


print RCtime(18) # Read RC timing using pin #18
if RCtime > 30000
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, True)


pi@chickenpi:~ $

So my syntax error is in the if statement at the bottom. Not sure what to do here.

Any help is gratefully received.

Regds...jules

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Using RCtime.py as a switch syntax

Fri Jan 29, 2016 3:58 pm

This should have a :

if RCtime > 30000:

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using RCtime.py as a switch syntax

Fri Jan 29, 2016 3:59 pm

You forgot the colon at the end of the if statement.

BTW: please post code in a block. It makes it easier to read *and* in Python indentation is an essential part of the language...

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Using RCtime.py as a switch syntax

Fri Jan 29, 2016 4:02 pm

1. Indentation is a vital part of python syntax. Please use the Code button at the top of the message edit screen to maintain indentation. Your script is otherwise indecipherable.

2. Yes, the Python interpreter is judge and jury. There is an error in that line. Looking up the correct syntax for an if statement would be a sensible thing to try.

[Which turns out to be what @DirkS said while I was typing this... ]

juliansilver
Posts: 7
Joined: Sun Jan 10, 2016 7:21 pm

Re: Using RCtime.py as a switch syntax

Sat Mar 05, 2016 12:37 pm

Thank you very much for your help...sorry for the tardy response.

Had a chance to test it today and my chickens are very happy with their light sensitive door!

To get it to work I had to include the colon as you suggested and the pin number - see code.

Thanks again.

Regards...jules

Code: Select all

#!/usr/bin/env python
 
# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!
 
import RPi.GPIO as GPIO, time, os      
 
DEBUG = 1
GPIO.setmode(GPIO.BCM)
 
def RCtime (RCpin):
        reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        time.sleep(0.1)
 
        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1
        return reading
 
                                     
        print RCtime(18)     # Read RC timing using pin #18
if RCtime(18) > 30000:
	GPIO.setup(17, GPIO.OUT)
	GPIO.output(17, True)	
	

Return to “Beginners”