Control individual Addressable RGB LEDs with Python

Hi all,

This is my first Auduino project and I may have bitten off more than I can chew! I have an ongoing project where I am building my own button box with a lasercut panel which I intend to back light.

My plan is to have around 40 RBG LEDs (part of a Neopixel strip) which will individually change their state based on toggle switch positions. These switches are being controlled via Joystick Gremlin and its associated Python modules, what I am after is a way of passing multiple variables from Python into Sketch.

I have been experimenting with the below which works for a single LED, but I don't know how I could handle passing multiple variables.

Python

import serial #Serial imported for Serial communication
import time #Required to use delay functions
 
ArduinoSerial = serial.Serial('com3',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

print (ArduinoSerial.readline()) #read the serial data and print it as line
print ("Enter 1 to turn ON LED and 0 to turn OFF LED")

 
while 1: #Do this forever

    var = input() #get input from user
    print ("you entered", var) #print the intput for confirmation
    
    if (var == '1'): #if the value is 1
        ArduinoSerial.write(var.encode()) #send 1
        print ("LED turned ON")
        time.sleep(1)
    
    if (var == '0'): #if the value is 0
        ArduinoSerial.write(var.encode()) #send 0
        print ("LED turned OFF")
        time.sleep(1)

Sketch:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int data;

void setup() { 
  Serial.begin(9600); //initialize serial COM at 9600 baudrate
  pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output
  digitalWrite (LED_BUILTIN, LOW);
  
  Serial.println("Hi!, I am Arduino");
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop() {
while (Serial.available()){
  data = Serial.read();
}

if (data == '1')
pixels.setPixelColor(2, pixels.Color(0,0,100));

else if (data == '0')
pixels.setPixelColor(2, pixels.Color(100,0,0));

pixels.show();
}

Essentially I have 40 Python variables (Variable1, Variable2 ect.) associated with switches that are either True or False (on or off), I would like to pass this value to a corresponding Variable in Sketch to change the LED based on the switch state. The Python side of this I am fine with other than knowing how to get the right Variable in Sketch to update.

I would send a message to the Arduino that looks like this "1000110011 ...." where 1 is ON and 0 is OFF for each of the 40 LEDs.

Then the Arduino can just iterate over the array and set the values.

For example

for (byte n = 0; n < 40; n++) {
  if ( receivedChars[n]  == '1') {
     // do stuff
  }
}

These links may help
Python - Arduino demo
Serial Input Basics

...R