I get that the LED configuration in the magazine is kind of backwards, with the GPIO pin "pushing against" the 3V3 pin, so that when you set the GPIO pin to False, the light comes on, and setting it to True turns the light off. Seems backward to me, and I don't know why they did it that way; they didn't explain why they chose that method. The straight-forward method does work, hooking the GPIO pin to the LED, to the resistor, to the GND, and True is on, False is off. So experiments one and two have paid off.
Experiment three: I didn't have a switch; Nothing I could find had legs that would fit in a breadboard configuration, and I hadn't gotten around to soldering anything up as yet, so I set my mind to the task of replacing the switch in the MagPi article and came up with... Photo sensor! Again, works like a champ, although it works backward from what I expected. If I cover it up, my LED start blinking, all under program control. Take my hand off to light it up, and it stops blinking. So... Either the photo sensor works backward from how I thought, or the switch in the article works backward from how I thought. Could be either; I'm not an electrician.
Then I pulled out the soldering iron and set to creating a breadboard-able switch. So much the same as the one in the article, it has four pins, with the two on the left passing through and the two on the right passing through. Pressing the switch connects all four contacts across the center. This is where things fall apart.
No matter how I hook this switch up, either like the MagPi article, or in the most simple of configurations, the program behaves as if it always sees the switch as pressed. The simplest circuit I've tried is GPIO pin to switch to GND. Actually, the simplest is GPIO pin hooked to nothing at all. The program acts like the pin is always True, no matter what I do.
A photo of the breadboard just before the last switch can be found here: http://www.facebook.com/photo.php?fbid= ... =1&theater
Could someone out there help me explain this anomaly, so I can sleep at night again? I can provide additional photos, and a copy of the code I'm running follows:
Code: Select all
pi@rpural:~/python$ cat ledbackground.py
#!/usr/bin/python
import time
import random
import thread
import RPi.GPIO as GPIO
go1 = 12
go2 = 11
go3 = 13
gi1 = 15
gi2 = 16
mindelay = .01
maxdelay = .25
runtime = 15
timerlock = thread.allocate_lock()
def blinker(gp, final):
while timerlock.locked():
state = random.choice((True, False,))
wait = random.random() * maxdelay + mindelay
GPIO.output(gp, state)
time.sleep(wait)
else:
GPIO.output(gp, final)
def runner():
while timerlock.locked():
GPIO.output(go3, True)
time.sleep(.15)
GPIO.output(go2, False)
GPIO.output(go3, False)
time.sleep(.15)
GPIO.output(go1, False)
GPIO.output(go2, True)
time.sleep(.15)
GPIO.output(go1, True)
time.sleep(.5)
GPIO.setup(go1, GPIO.OUT)
GPIO.setup(go2, GPIO.OUT)
GPIO.setup(go3, GPIO.OUT)
GPIO.setup(gi1, GPIO.IN)
GPIO.setup(gi2, GPIO.IN)
GPIO.output(go1, True)
GPIO.output(go2, False)
GPIO.output(go3, False)
time.sleep(.5)
GPIO.output(go2, True)
time.sleep(.5)
GPIO.output(go2, False)
time.sleep(.5)
GPIO.output(go2, True)
while True:
if GPIO.input(gi1):
timerlock.acquire()
thread.start_new_thread(blinker, (go1, True,))
thread.start_new_thread(blinker, (go2, True,))
thread.start_new_thread(blinker, (go3, False,))
time.sleep(runtime)
timerlock.release()
if GPIO.input(gi2):
timerlock.acquire()
thread.start_new_thread(runner, ())
time.sleep(runtime)
timerlock.release()
time.sleep(1)
pi@rpural:~/python$