Skip to content

Instantly share code, notes, and snippets.

@JosiasSena
Last active October 2, 2016 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosiasSena/9a677d1cdd85d4b41359f8247c4b8243 to your computer and use it in GitHub Desktop.
Save JosiasSena/9a677d1cdd85d4b41359f8247c4b8243 to your computer and use it in GitHub Desktop.
Raspberry Pi - Display red, green, or blue randomly on button press
import random
import time
import RPi.GPIO as GPIO
# Constants
RUNNING = True
SLEEP_TIME = .2 # in seconds
FREQUENCY_ON = 100 # Hz
FREQUENCY_OFF = 0 # Hz
# Init GPIO - set GPIO to Broadcom system
GPIO.setmode(GPIO.BCM)
# Disable warnings
GPIO.setwarnings(False)
# RGB LED breadboard Pin numbers (change accordingly)
redPin = 13
greenPin = 6
bluePin = 5
# Button breadboard pin number (change accordingly)
buttonPin = 2
# Set RGB LED pins to output mode
GPIO.setup(redPin, GPIO.OUT)
GPIO.setup(greenPin, GPIO.OUT)
GPIO.setup(bluePin, GPIO.OUT)
# Setting button as an input device
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Setup all the LED colors with an initial
# duty cycle of 0 which is off
RED = GPIO.PWM(redPin, FREQUENCY_ON)
RED.start(FREQUENCY_OFF)
GREEN = GPIO.PWM(greenPin, FREQUENCY_ON)
GREEN.start(FREQUENCY_OFF)
BLUE = GPIO.PWM(bluePin, FREQUENCY_ON)
BLUE.start(FREQUENCY_OFF)
lights = [RED, GREEN, BLUE]
# Main loop
try:
while RUNNING:
button_input_state = GPIO.input(buttonPin)
light = random.choice(lights)
if not button_input_state:
light.ChangeDutyCycle(FREQUENCY_ON)
light = random.choice(lights)
time.sleep(SLEEP_TIME)
else:
light.ChangeDutyCycle(FREQUENCY_OFF)
# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
RUNNING = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment