Currently, my program accepts user input for blink length and delay between blinks and then loops through the lights a set amount of time. I have an RGB light also wired to the Pi and I would like it so that depending on how fast the lights are blinking, the RGB light will change color. As the program is now, the RGB light will stay red. I've tested the RGB light with a different program and it works fine so I'm fairly certain the error is in my code not the wiring. Thanks for the help!
# @kalenpw
import RPi.GPIO as GPIO
import time
#use raspberry pi pin numbers
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#GPIO output channels
GPIO.setup(31, GPIO.OUT)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(33, GPIO.OUT)
GPIO.setup(35, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(37, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
#RGB Light
GPIO.setup(15, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
#Make LED blink
def blink(pin):
GPIO.output(pin,1)
time.sleep(lightOnDuration)
GPIO.output(pin,0)
time.sleep(lightOffDuration)
return
#turn on RGB led based off blink duration
def RGBTurnOn():
#turn on red
if lightOnDuration < .5:
GPIO.output(15,1)
#turn on green
elif lightOnDuration < 2:
GPIO.output(13,1)
#turn on blue
else:
GPIO.output(11,1)
#Get number from user TODO fix
def getFloat(message):
while True:
try:
userInput = float(input(message))
except ValueError:
print("Error(01): please enter a number.")
continue
else:
return userInput
break
#Get integer from user TODO fix
def getInt(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Error(02): please enter an integer.")
continue
else:
return userInput
break
#Starts LED loop
def StartLoop():
RGBTurnOn()
for i in range(0,numberOfCycles):
blink(32)#5v green
blink(38)#5v yellow
blink(36)#5v blue
blink(40)#5v red
blink(37)#3v3 green
blink(35)#3v3 yellow
blink(33)#3v3 blue
blink(31)#3v3 red
#Get light on & off durations
lightOnDuration = getFloat("How man seconds for blink length?\n")
lightOffDuration = getFloat("How many seconds do you want between blinks?\n")
cycleDuration = (lightOnDuration + lightOffDuration) *8
cycleDurationString = str(cycleDuration) + " second(s)."
numberOfCycles = getInt("Enter total number of cycles please.\n"
"Each cycle will take aproximately " + cycleDurationString +"\n")
StartLoop()
GPIO.cleanup()
Pictures of wiring:
RGB: https://i.sstatic.net/rulYL.jpg
LEDs: https://i.sstatic.net/h0zl0.jpg
LEFT red pin 31 gpio 6
LEFT blue pin 33 gpio 13
LEFT yellow pin 35 gpio 19
LEFT green pin 37 gpio 26
RIGHT red pin 40 gpio 21
RIGHT blue pin 36 gpio 16
RIGHT yellow pin 38 gpio 20
RIGHT green pin 32 gpio 12
rgb red pin pin 15 gpio 22
rgb green pin 13 gpio 27
rgb blue pin 11 gpio 17
Your programming seems correct to me.
Just a wild guess:
From the pictures I see your RGB-LED is the common cathode type. Therefore, you should add a separate resistor for every led anode - not a single one for your cathode. You didn't and maybe therefore, the red LED draws all available current (the state of the red GPIO is left unclear if green or blue are set to ON).
Try to set the GPIOs of not needed RGB-pins to OFF on startup or in your method.
def RGBTurnOn():
GPIO.output(15,0)
GPIO.output(13,0)
GPIO.output(11,0)
#turn on red
if lightOnDuration < .5:
GPIO.output(15,1)
#turn on green
elif lightOnDuration < 2:
GPIO.output(13,1)
#turn on blue
else:
GPIO.output(11,1)