Nitrodude
Posts: 1
Joined: Thu Jan 07, 2016 2:47 am

Need help with python to flash LEDs

Thu Jan 07, 2016 3:16 am

First I'll start off by saying, I'm new to the pi world! Always wanted one and finally scooped up a b+ in an ultimate starter kit from Vilros.

Anyways, onto my issue. I threw together a simple little LED flashing project, and it works well. The only problem I'm having now, is that I want the LEDs to flash from the outside in, simultaneously. Not one LED at a time, but starting from the outsides working it's way in, 2 LEDs at a time. I hope this makes sense. Enclosed I've attached pictures of what I set up as well as the code I have controlling it. Can anybody tell me how to do what I'm trying to achieve? Thank you in advance!
Image

Image


Image

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Need help with python to flash LEDs

Thu Jan 07, 2016 8:11 am

Nice work.
uploading code in CODE blocks is a little easier than photos of your monitor, but... whatever works I guess...

I'm guessing you're fairly new to python/programming?
First thing I'd do is define a list of GPIO ports representing your LEDs, something like

Code: Select all

LEDS=(2,3,4,17,27,22, ... etc. ..., 16,12)

This will let you initialise the LEDs quickly and easily:

Code: Select all

N = len(LEDS)
for idx in range(N):
    led = LEDS[idx]
    print('index %d, LED %d' % (idx, led))
    RPi.GPIO.setup(pin, RPI.GPIO.OUT)
you can then replicate your current code with something like

Code: Select all

while True:
    for idx in range(N):
        led = LEDS[idx]
        print('index %d, LED %d' % (idx, led))
        RPi.GPIO.output(led, True)
        time.sleep(0.2)
        RPi.GPIO.output(led, False)
        time.sleep(0.2)
finally

Code: Select all

while True:
    for idx in range(N/2):
        ledA = LEDS[idx]
        ledB = LEDS[N - idx]
        RPi.GPIO.output(ledA, True)
        RPi.GPIO.output(ledB, True)
        time.sleep(0.2)
# etc... 

Return to “Troubleshooting”