okay I'm using this code turn an led on and off, using values provided by a photocell and a 1microf capacitor.
Code: Select all
import RPi.GPIO as GPIO import time import os #!/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) GPIO.setup(22, GPIO.OUT) def RCtime (RCpin): reading = 0 GPIO.setup(RCpin, GPIO.OUT) GPIO.output(RCpin, GPIO.LOW) time.sleep(.5) GPIO.setup(RCpin, GPIO.IN) while (GPIO.input(RCpin) == GPIO.LOW): reading += 1 return reading while True: if RCtime(18) > 500: print (RCtime(18)) GPIO.output(22, True) if RCtime(18) < 500: print ('less than') GPIO.output(22, False) GPIO.cleanup()
It works great as is, but i have two problems.
1.) I need a way to stop it on command, I want it to read values until I tell it to stop, for now it just reads values until I close the terminal.
2.) Right now it makes an instantaneous decision on wether or not to take action on the led, I would like it to run for an indefinite amount of time and have it set to record a value every (n)seconds, if the values over a period of time (n) are greater than a set value it needs to turn on the led, if they are lower i need it to turn it off.
I have basic knowledge of other languages but i have no experience with using time as a variable, and I'm new to python as a whole. I'm not asking you to write it for me, i just need help in the right direction. mainly the syntax is what I'm weakest at.