Originally Posted by
ofnuts
Your problem is comparing a plain int (0, 5, 10) with a float which is the result of computations and may not be exactly equal to 5.00000000000000000 or 10.0000000000000000. Try this:
Code:
cnt=0
delay=.1
for i in range(50):
x=x+delay
print '%3.20f' % x
still nothing
Code:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
pin = 19
pin2 = 21
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(pin2, GPIO.OUT)
delay = 0.1
five='%3.20f' % 5
ten='%3.20f' % 10
try:
while True:
cnt = 0
while GPIO.input(pin) == 1:
if cnt in [0, five, 5, 5.0, "5.0", ten, 10, 10.0, "10.0"]:
print "Hello"
GPIO.output(pin2, 1)
time.sleep(delay)
GPIO.output(pin2, 0)
cnt=cnt+delay
else:
cnt=cnt+delay
time.sleep(delay)
if cnt > 5 and cnt < 10:
# 5 to 10 seconds
print "5 - done"
elif cnt > 9:
# 10 seconds or more
print "10 - done"
elif cnt > 0:
# Less than 5 seconds
print "0 - done"
time.sleep(delay)
except KeyboardInterrupt:
GPIO.cleanup()
print "\n"
Bookmarks