I'm begginer at arduino and coding and trying to make ambilight for my monitor. I've started this project a few days ago and can't get any results.
There's no errors in Python or Arduino IDE, LED works correct, if i command it manual. I think trouble in reading COM data, but i can't solve it.
There's Arduino code:
#include <FastLED.h>
#define NUM_LEDS 71
#define PIN 6
CRGB leds[NUM_LEDS];
String state;
String LastData = "";
int ActiveLed = 0;
int r = 0;
int g = 0;
int b = 0;
void setup() {
FastLED.addLeds <WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(10);
Serial.begin(9600);
Serial.setTimeout(20);
}
void loop()
{
String receivedData = Serial.readString();
if(LastData != Serial.readString())
{
state = LastData;
}
switch(state[0])
{
case 'r':
state.remove(0);
r = state.toInt();
case 'g':
state.remove(0);
g = state.toInt();
case 'b':
state.remove(0);
b = state.toInt();
case 'l':
state.remove(0);
ActiveLed = state.toInt();
}
leds[ActiveLed] = CRGB(255, 255, 255);
leds[ActiveLed] = CRGB(r, g, b);
leds[5] = CRGB(0, 0, 255);
LastData = Serial.readString();
FastLED.show();
}
And Python:
import serial
import time
import pyautogui
time.sleep(2)
ser = serial.Serial("COM4", 9600)
ser.timeout = 0.02
i = 0
color = pyautogui.pixel(100, 200)
l = 0
r = 0
g = 0
b = 0
while True:
for i in range(71):
if i < 21:
color = pyautogui.pixel(3800, round(2120 / 21 * i))
elif i > 50:
color = pyautogui.pixel(40, round(2120 / (71 - 51) * (71 - i)))
else:
color = pyautogui.pixel(round(3800 / 32 * (i - 20)), 40)
ser.write(('r' + str(color[0])).encode('utf-8'));
ser.write(('g' + str(color[1])).encode('utf-8'));
ser.write(('b' + str(color[2])).encode('utf-8'));
ser.write(('l' + str(i)).encode('utf-8'))