The Maker Cupboard

- Digital Making - Raspberry Pi Projects - 3D printing - Educational Resources -

Disco lights

A simple Python script which generates a random number (3, 5 or 7) and uses that number to switch on a LED.

disco




The LEDs are connected to pins 3,5 and 7 with a resistor connected to the cathode of each LED (short leg) connected to pins 17,20 and 25.

import time
import RPi.GPIO as GPIO
import random

GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)

GPIO.output(3,GPIO.LOW)
GPIO.output(5,GPIO.LOW)
GPIO.output(7,GPIO.LOW)

time.sleep(1)

list = [3,5,7]

while True:
light = random.choice(list)
GPIO.output(light,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(light,GPIO.LOW)

Please leave any comments and suggestions below.


blog comments powered by Disqus