1

I have a Python 3 program but the code below doesn't work in Python 3. I've searched for days but didn't find any solution. Is there any way to get this working in Python 3?

The only output I get is a syntax error like:

    print rc_time(pin_to_circuit)     # Read RC timing using pin #18
                 ^
SyntaxError: invalid syntax

The setup looks like the following picture:

from pimylifeup.com

#!/usr/local/bin/python

import RPi.GPIO as GPIO import time

GPIO.setmode(GPIO.BOARD)

    #define the pin that goes to the circuit 
pin_to_circuit = 7

def rc_time (pin_to_circuit):
    count = 0

    #Output on the pin for 
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)

    #Change the pin back to input
    GPIO.setup(pin_to_circuit, GPIO.IN)

    #Count until the pin goes high
    while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        count += 1

    return count

#Catch when script is interrupted, cleanup correctly try:
    # Main loop
while True:
    print rc_time(pin_to_circuit) except KeyboardInterrupt:
pass finally:
    GPIO.cleanup()
5
  • 1
    What, exactly, isn't working? What would you like to happen, and how does your result differ from what you want?
    – Bex
    May 24, 2018 at 13:04
  • A photoresistor is an analog sensor; if you want to use it digitally I think it normally would be paired with a resistor, but that looks like a capacitor in the diagram. Please explain the wiring in more detail if you want help, e.g., if that is a resistor, how many ohms is it.
    – goldilocks
    May 24, 2018 at 13:19
  • 1
    the wiring is the following: I set up the capacitor for a delay. I load them for 0.1 sec and count till it is ´low´ on the GPIO. a resistor is not needed in this case. I updated the question with the output
    – Dinera
    May 24, 2018 at 13:25
  • print(rc_time(pin_to_circuit))
    – stevieb
    May 24, 2018 at 13:50
  • @PeterPan, the correct way to debug a problem like that is to replace the line print rc_time(pin_to_circuit) with something like this print 1234 ..... that would give you the same error, which would tell you that the print statement is incorrectly formatted ..... you could then google python3 print statement and you would have your answer
    – jsotola
    May 24, 2018 at 22:54

3 Answers 3

3

In Python 3 print is a function.

In Python 2 you could do print "hello".

In Python 3 you must do print("hello").

0
0

correct answer to your problem is mentioned above by joan. also i would suggest you to use time.time() instead of counter. like so use:

def rc_time (pin_to_circuit):    
    #Output on the pin for 
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)

    #Change the pin back to input
    GPIO.setup(pin_to_circuit, GPIO.IN)
    stTime=time.time()
    #Count until the pin goes high
    while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        pass

    return(time.time()-stTime)

instead of this:

def rc_time (pin_to_circuit):
    count = 0

    #Output on the pin for 
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)

    #Change the pin back to input
    GPIO.setup(pin_to_circuit, GPIO.IN)

    #Count until the pin goes high
    while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        count += 1

    return count

this way returned value will be in nanoseconds instead of some random count units and will ease your calculations afterwards.

also think about to put some timeout inside to take care of damaged sensor that will take forever to charge with this code in a while loop:

while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        if(stTime<(time.time()-1)):
                 raise Exception("timeout")

1 second is more than enough to charge your circuit

-2

Isn't this just an indentation problem? Change the last four lines to:

while True:
    print rc_time(pin_to_circuit) except KeyboardInterrupt:
pass finally:
    GPIO.cleanup()
3
  • Thanks but the issue still exists.
    – Dinera
    May 24, 2018 at 13:41
  • " print light_time(pin_to_circuit) " This line does not appear in the code you show us. May 24, 2018 at 13:43
  • sorry for that. I changed the function and variable names in my project. I correct it in my request.
    – Dinera
    May 24, 2018 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.