Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1584

Python • LED control using variables

$
0
0
I'm exploring how to control individual addressable LEDs (WS2812b). In the end, I want to read in a CSV with 20 different conditions (Button A is pressed, Button B is pressed, etc) where each condition dictates the color of each individual LED.

Before I get there, I'm trying to pass along a variable to the neopixel code syntax. I've tried a few different things, and get different error messages.

I'm using the neopixel and adafruit_led_animation.color packages.

The adafruit_led_animation.color library holds RGB values to color names.

Code:

import csvfrom gpiozero import Buttonimport timeimport boardimport neopixelfrom adafruit_led_animation.color import RED, YELLOW, ORANGE,GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE, WHITE, BLACK#load CSV and read color from column 6with open("LEDtestcsv.csv", 'r') as file:csvreader = csv.DictReader(file)for row in csvreader:print(row["6"])z = row["6"]#Initialise a strips variable, provide the GPIO Data Pin#utilised and the amount of LED Nodes on strip and brightness (0 to 1 value)pixels1 = neopixel.NeoPixel(board.D18, 20, brightness=1)#Also create an arbitrary count variablex=0#Focusing on a particular strip, use the command Fill to make it all a single colour#based on decimal code R, G, B. Number can be anything from 255 - 0. Use an RGB Colour#Code Chart Website to quickly identify the desired fill colour.pixels1.fill((GREEN))#assign button to GPIO 2button = Button(2)button.wait_for_press()#assign colors to LEDsprint(z)#pixels1[1] = "(" + z + ")"pixels1[3] = (YELLOW)pixels1[4] = (PURPLE)pixels1[8] = (RED)
the variable z = RED

pixels1[3] = (YELLOW)
This line works correctly

pixels1[1] = "(" + z + ")"
This line of code gives me an error. ValueError: Expected tuple of length 3, got 5
I believe the syntax is looking for a (R,G,B) value. however, as mentioned, it reads pixels1[3] = (YELLOW) correctly.

I'm guessing I have to convert my variable to text or some other syntax adjustment to make this work. I'm very new to python still, so any help is appreciated.

Statistics: Posted by alfordtp — Wed Jun 11, 2025 2:12 pm



Viewing all articles
Browse latest Browse all 1584

Trending Articles