lionelep
Posts: 3
Joined: Sun Aug 12, 2018 2:23 pm

GPIO Pin reading for Photoresistor help please

Sun Aug 12, 2018 2:30 pm

So i have a raspberry pi running and i wired a photoresistor to it like this image here Image . Then the code i used to read the values from the sensor is this

Code: Select all

#!/usr/local/bin/python

import RPi.GPIO as GPIO
import time

__author__ = 'Gus (Adapted from Adafruit)'
__license__ = "GPL"
__maintainer__ = "pimylifeup.com"

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 interupted, cleanup correctly
try:
    # Main loop
    while True:
        print rc_time(pin_to_circuit)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()
My issue is that I want to be able to set the line of code that returns the number values depending on the light in the area to a variable. So if the line GPIO.output(pin_to_circuit, GPIO.LOW) returns the number value, I want to set that line to equal x. Then I would do an if statement where if x is within a defined range of values, then I would insert my own code for the then part such as sending a notification to my phone. The problem is that I cant seem to be able to set that previous line of code to equal a variable i make. Is it possible to make the output code equal to a new variable to use?

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

Re: GPIO Pin reading for Photoresistor help please

Sun Aug 12, 2018 3:47 pm

After this line

print rc_time(pin_to_circuit)

Try Adding...

x = rc_time(pin_to_circuit)

if x > 1 and x < 9: (change numbers to Suit)
Do something...

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

Re: GPIO Pin reading for Photoresistor help please

Sun Aug 12, 2018 3:55 pm

This sounds like a very basic python coding exercise.

Maybe the value you need as the basis for your future decision making is just the return value of the rc_time() function?

So

Code: Select all

my_new_variable = rc_time(pin_to_circuit)
might be your starting point?


[Also suggested by someone else as I typed this reply... ]

No shame in 'cut and paste', but it will help you move forward if you take a little time to try to grasp what you have copied...

lionelep
Posts: 3
Joined: Sun Aug 12, 2018 2:23 pm

Re: GPIO Pin reading for Photoresistor help please

Mon Aug 13, 2018 4:58 pm

gordon77 wrote:
Sun Aug 12, 2018 3:47 pm
After this line

print rc_time(pin_to_circuit)

Try Adding...

x = rc_time(pin_to_circuit)

if x > 1 and x < 9: (change numbers to Suit)
Do something...
thank you so much. I got it to do what I wanted with your help!

lionelep
Posts: 3
Joined: Sun Aug 12, 2018 2:23 pm

Re: GPIO Pin reading for Photoresistor help please

Mon Aug 13, 2018 5:10 pm

B.Goode wrote:
Sun Aug 12, 2018 3:55 pm
This sounds like a very basic python coding exercise.

Maybe the value you need as the basis for your future decision making is just the return value of the rc_time() function?

So

Code: Select all

my_new_variable = rc_time(pin_to_circuit)
might be your starting point?


[Also suggested by someone else as I typed this reply... ]

No shame in 'cut and paste', but it will help you move forward if you take a little time to try to grasp what you have copied...
Yup thanks for the help, similarly to the other post from the other person I was able to do what I wanted to do.

Return to “Python”