Hello everyone,
I'm trying to program an ambilight on my computer using Python on Windows and Arduino.
With Python, I extract the colors from my screen and get an array of 100 rgb lights wanted (for my 100 LED), like this : [[255,0,0],[254,3,0], etc...]
My problem is... how can I send this to arduino really fast ?
I've tried to send it with the "write" command but if i send too much data in too less time, everything is messed up.
For instance, with p the position wanted :
225r225g225b1p
237r238g238b2p
255r255g255b3p
255r255g255b4p
255r25525255r255g255b11p
As you can see, the 5th "5p" didnt get to arduino and was replaced by something else...
If i put a time.sleep of at least 0.5s between each write, it's working unless i concatenate the strings.
Do you have any way to send multiple arrays at once and in a fast way ? I need to send around 200*3 values in 0.1s.
Thanks in advance !
Here's the arduino code :
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 12
String readString;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(144, PIN, NEO_RGBW + NEO_KHZ800);
void setup() {
Serial.begin (2000000);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.println ("Booted successfully");
}
void loop() {
int r=0;
int g=0;
int b=0;
int pos=0;
while (Serial.available()) {
delay(1); //small delay to allow input buffer to fill
if (Serial.available() >0){
char c = Serial.read(); //gets one byte from serial buffer
Serial.println(c);
if (c == 'r') {
r = readString.toInt();
readString = "";
} else if (c == 'g') {
g = readString.toInt();
readString = "";
} else if (c == 'b') {
b = readString.toInt();
readString = "";
} else if (c == 'p') {
pos = readString.toInt();
readString = "";
strip.setPixelColor(pos, strip.Color(g,r,b));
} else if (c == '|') {
strip.show();
} else {
readString += c;
}
}
}
}
And the Python code :
arduino = serial.Serial("COM4",2000000,timeout=1)
px=""
for i in range(1,colors_w.shape[0]):
color = colors_w[i]
px += str(color[0])+"r"+str(color[1])+"g"+str(color[2])+"b"+str(i)+"p"
arduino.write(px.encode())
px = ""
time.sleep(0.02)
arduino.write("|".encode())
(i've tried with multiple baud rates with no success)