Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
LED exercise
#1
I only started programming on my Rpi this past week, I've never programmed before, and I'm making the most of my lockdown time by keeping my brain engaged.
I Have a Rpi Zero WH connected to a breadboard and 8 LED's (red, amber, green and blue)
Following guides etc I managed to get them to switch on and off, and modified the program myself so that alternate LED's switch on and off.

The project I have in mind requires the LED's to blink at differing speeds at the same time as some of the other LED's are on or blinking at a different speed. Eventually I want to have sound files playing along with the LED's (It's for a diorama that I am building).
As I said, I only started this past week, and have searched but am struggling to find an example of code that I could use/modify to allow me to do what I need to do.

Any help/pointers would be appreciated, I am using Thonny on the RPi to write the code.


Here's the code I have modified from the standard GPIO LED on/off script

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)
GPIO.setup(8,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)

while True:
    GPIO.output((((14, 23, 24, 7))), True) #red and green LED on
    time.sleep(.5)
    GPIO.output((((14, 23, 24, 7))), False) #red and green LED off
    time.sleep(.5)

    GPIO.output((((15, 18, 8, 25))), True) #amber and blue LED on
    time.sleep(.5)
    GPIO.output((((15, 18, 8, 25))), False) #amber and blue LED off
    time.sleep(.5)
Reply
#2
If you want to have "parallel" stuff going on, you might want to go via
a chip (shift register) with 8 gates. Costs almost nothing and is not that difficult to do.
Plenty of examples on the internet.
The ultimate challenge is to have 8 leds, and program them to show all the binary numbers
from 0 to 255. :-)
(And then have people guess in what order they blink)
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Thanks DPaul,

Yeah I do want to have parallel stuff going on, I am in process of building a Martian War Machine from the 1953 movie, I have a scene already created, just want a lighting and sound system to accompany it, I could just go and buy a kit from a company in the USA, but I thought it'd be more fun to build it myself using a RPi and learn to program in the process.
If you look up Martian War machine lighting and sound kit on Youtube you'll see what I'm hoping to achieve.

I get the process of lighting now, although it's only serial, I have a box full of components which no doubt wont contain a shift register, but you never know!

I still will need to understand the process of getting multiple led's blinking at different rates, I can't seem to find any info in that regard for me to look at and play with.

Thanks for your reply.
Reply
#4
Hi Sand319,

Look here:
http://www.multiwingspan.co.uk/micro.php?page=shift
Doing it in Python is no big deal.
Success,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
Awesome link, thanks very much!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020