Skip to content

Instantly share code, notes, and snippets.

@jverive

jverive/colors.py

Created May 5, 2016
Embed
What would you like to do?
# filename: colors.py
# written February, 2016
# last edit: February 22, 2016
#
# This is a Python 2.7 script for controlling some relays attached
# to an Omega Onion microcontroller board. This specific code was written
# for a relay control board with four separate relays, each having a set of SPDT contacts.
# The drive circuitry energizes a relay when the input signal is at a low voltage
# (logic LOW), and de-energizes the relay when the input signal is at a HIGH
# logic level. This may seem "backwards", so the script corrects for this (see the
# variable assignments below).
#
# For my application, three of the relays are being used, each
# relay's "normally open (NO)" contact connected to the common anode of a
# string of red, green, or blue LEDs; the common contact for each of the relays
# is connected to a +12V source (the LED strings have internal series current
# limiting resistors). Finally, the common anodes for all or the strings are
# connected together and this connection is then tied to the ground (0V) terminal
# of the 12V power supply. To turn on a given color, we send a LOW to the
# corresponding GPIO pin. See below for color:pin assignments.
#
# The script is very simple, asking the user to choose a color to be illuminated.
# Options are as follows:
#
# 'r' - red (illuminate red LEDs)
# 'y' - yellow (illuminate red and green LEDs)
# 'g' - green (illuminate green LEDs)
# 'c' - cyan (illuminate green and blue LEDs)
# 'b' - blue (illumintae blue LEDs)
# 'v' - violet (illuminate red and blue LEDs)
# 'w' - white (illuminate red, green, and blue LEDs)
# 'k' - black (turn off all LEDs)
# 'q' - quit execution
# 'i' - (i)nitialize GPIO pins (only needed if colors aren't responding)
# Note that the user input is case sensitive: only lower case letters will be recognized
# as valid inputs. All other characters will not affect the illuminated colors. Also note
# that the color choices are limited (for example, no pink or orange). This is because any
# colors other than the displayed choices require intensity modulation of the various
# component colors, and relays are not really suited for this purpose.
#
#
# the 'sleep' routine adds some delays to give the user time to interpret the screen
# results. All instances of sleep() may be deleted without changing the script's
# functionality.
#
from time import sleep
# import the functions for controlling the GPIO port pins
#
import omega_gpio
# initialize variables to reflect GPIO port pins
#
RED = 0 #GPIO port 0 pin controls red LEDs
GREEN = 1 #GPIO port 1 pin controls green LEDs
BLUE = 6 #GPIO port 6 pin controls blue LEDs
# initialize variables to represent ON and OFF states. Since the LED strings are turned
# ON with a LOW logic level signal and turned OFF with a HIGH logic level (which may
# seem counter-intuitive), we create variables named ON and OFF to represent the signal
# levels instead of using HIGH and LOW (or 1 and 0). This improves readability.
#
ON = 0
OFF = 1
# main program execution starts here:
#
while 1:
sleep(1)
print ""
print ""
print "r - (r)ed (illuminate red LEDs)"
print "y - (y)yellow (illuminate red and green LEDs)"
print "g - (g)reen (illuminate green LEDs)"
print "c - (c)yan (illuminate green and blue LEDs)"
print "b - (b)lue (illumintae blue LEDs)"
print "v - (v)iolet (illuminate red and blue LEDs)"
print "w - (w)hite (illuminate red, green, and blue LEDs)"
print "k - blac(k) (turn off all LEDs)"
print "q - (q)uit execution"
print "i - (i)nitialize LED controller (all LEDs will be turned OFF)"
print ""
reply = raw_input('What color is desired? (r,y,g,c,b,v,w,k,q):')
if reply == 'q':
break
elif reply == 'r':
omega_gpio.setoutput(RED, ON)
omega_gpio.setoutput(GREEN, OFF)
omega_gpio.setoutput(BLUE, OFF)
elif reply == 'y':
omega_gpio.setoutput(RED, ON)
omega_gpio.setoutput(GREEN, ON)
omega_gpio.setoutput(BLUE, OFF)
elif reply == 'g':
omega_gpio.setoutput(RED, OFF)
omega_gpio.setoutput(GREEN, ON)
omega_gpio.setoutput(BLUE, OFF)
elif reply == 'c':
omega_gpio.setoutput(RED, OFF)
omega_gpio.setoutput(GREEN, ON)
omega_gpio.setoutput(BLUE, ON)
elif reply == 'b':
omega_gpio.setoutput(RED, OFF)
omega_gpio.setoutput(GREEN, OFF)
omega_gpio.setoutput(BLUE, ON)
elif reply == 'v':
omega_gpio.setoutput(RED, ON)
omega_gpio.setoutput(GREEN, OFF)
omega_gpio.setoutput(BLUE, ON)
elif reply == 'w':
omega_gpio.setoutput(RED, ON)
omega_gpio.setoutput(GREEN, ON)
omega_gpio.setoutput(BLUE, ON)
elif reply == 'k':
omega_gpio.setoutput(RED, OFF)
omega_gpio.setoutput(GREEN, OFF)
omega_gpio.setoutput(BLUE, OFF)
elif reply == 'i':
# If the relays are not responding, the GPIO pins must be re-initialized.
# Note: 'out' is previously defined in the omega_gpio library we imported;
# if instead we had wanted the pins to be inputs, we would have initialized them to 'in'.
#
omega_gpio.initpin(RED, 'out')
omega_gpio.initpin(GREEN, 'out')
omega_gpio.initpin(BLUE, 'out')
omega_gpio.setoutput(RED, OFF)
omega_gpio.setoutput(GREEN, OFF)
omega_gpio.setoutput(BLUE, OFF)
@jverive

This comment has been minimized.

Copy link
Owner Author

@jverive jverive commented May 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment