thatbakerguy
Posts: 13
Joined: Tue Dec 25, 2012 8:08 pm

how to use time as a varible

Tue Feb 19, 2013 6:24 am

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() 
okay I'm using this code turn an led on and off, using values provided by a photocell and a 1microf capacitor.
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.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2848
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: how to use time as a varible

Tue Feb 19, 2013 8:23 pm

You've already got

Code: Select all

import time
and

Code: Select all

time.sleep(.5)
in the code. the number in the time.sleep() function is the time in seconds that you want to wait for.
Change the number for what you want and that part of it's sorted.

You can halt the program at any time with CTRL+C, but it sounds like you're looking for something more subtle than that.
Alex Eames RasPi.TV, RasP.iO

thatbakerguy
Posts: 13
Joined: Tue Dec 25, 2012 8:08 pm

Re: how to use time as a varible

Wed Feb 20, 2013 2:00 am

yeah im trying to make like a while loop statement you know like "press s to stop"

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: how to use time as a varible

Wed Feb 20, 2013 5:46 pm

http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”