User manual

47
We will explain now more loops and programming methods in Python by using different LED flashing
patterns. The next programme offers different LED samples that the user can select by making keyboard
entries.
The programme
ledmuster.py allows the LEDs to use different flashing patterns.
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import random
GPIO.setmode(GPIO.BCM)
LED = [4,18,23,24]
for i in LED:
GPIO.setup(i, GPIO.OUT, initial=0)
z = len(LED); w = 5; t = 0.2
print (“Light effects selection"); print ("1 – cyclical chaser")
print ("2 – chase bidirectional"); print ("3 – ascending and descending")
print ("4 – all flashing simultaneously"); print ("5 – all flashing at random")
print (“Ctrl+C exits the program")
try:
while True:
e = raw_input (“Please select a pattern: ")
if e == "1":
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
elif e == "2":
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
for j in range(z-1, -1, -1):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
elif e == "3":
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True); time.sleep(t)
time.sleep(2*t)
for j in range(z-1, -1, -1):
GPIO.output(LED[j], False)
time.sleep(t)
time.sleep(2*t)
elif e == "4":
for i in range(w):
for j in range(z):
GPIO.output(LED[j], True)
time.sleep(2*t)