I wanted to install a 5 LED strip underneath my PC's front panel. I want to send color over PC so it can change when i open different apps, with temperature or etc!
So all I think the best way to do this is to just use Arduino to set the LED Values!
But I have had some troubles creating a good communication protocol for this, I've been just simply sending 45 digits (15 3 digit numbers) to my Arduino with serial. here's code: Arduino :
#include "Adafruit_NeoPixel.h"
#include "WS2812_Definitions.h"
#define PIN 4
#define LED_COUNT 5
struct pixel {
int R = 0;
int B = 0;
int G = 0;
};
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
char count = 1;
int pixelNumber = 0;
struct pixel tempPixel;
//struct pixel Pixels[LED_COUNT];
void setup()
{
Serial.begin(9600);
Serial.println("Init");
leds.begin(); // Call this to start up the LED strip.
clearLEDs(); // This function, defined below, turns all LEDs off...
leds.show(); // ...but the LEDs don't actually update until you call this.
}
void loop() {
if(Serial.available() > 0) {
if(count == 1) {
tempPixel.R = PC_Receive();
} else if (count == 2) {
tempPixel.G = PC_Receive();
} else if (count == 3) {
tempPixel.B = PC_Receive();
count = 0;
setPixelColor(pixelNumber, tempPixel);
Serial.println(pixelNumber);
leds.show();
// Pixels[pixelNumber] = tempPixel;
if(++pixelNumber >= LED_COUNT) {
pixelNumber = 0;
}
}
count++;
}
// if(pixelNumber == 0) {
// for(int i = 0; i < LED_COUNT; i++ ) {
// setPixelColor(i, Pixels[i]);
// }
//
// leds.show();
// }
}
void setPixelColor(int a, struct pixel P)
{
leds.setPixelColor(a, P.R, P.G, P.B);
}
char PC_Receive() {
int result = 0;
while(Serial.available() == 0);
result += ((int)Serial.read() - '0') * 100;
while(Serial.available() == 0);
result += ((int)Serial.read() - '0') * 10;
while(Serial.available() == 0);
result += ((int)Serial.read() - '0');
Serial.println(result);
return result;
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
for(int i = 0; i < LED_COUNT; i++) {
leds.setPixelColor(i, red, green, blue);
}
leds.show();
}
And here's PC's dirty code in python:
import serial
import time
import random
class Pixel:
def __init__(self, R, G, B):
self.R = R
self.G = G
self.B = B
def clearAll():
for i in range(0,5):
ser.write("000000000")
def sendPixel(pixels):
for i in range(0,5):
ser.write(str(pixels[i].R).zfill(3))
ser.write(str(pixels[i].B).zfill(3))
ser.write(str(pixels[i].G).zfill(3))
ser = serial.Serial('/dev/ttyACM2', 9600)
little = [0,0,0,0,0]
for i in range(0,5):
little[i] = Pixel(0,255,50)
sendPixel(little)
time.sleep(0.5)
clearAll()
the problem is it isn't stable, i think i should just use an \n at end or send back OK or something to confirm it, but that would make it slow if i did it for each value! (and checking only once after all 5 leds are send would mean sending more values until i receive the ok and clearing and sending them again!)
Can you recommend me a better way?
Thanks!
EDIT: Just wanted to say! it's almost done! It does change color when i open chrome, and i think at 115200 baud rate it's fast enough to not notice a delay (although at 9600 i felt like it's slower then my (xxx/xxx/xxx....) protocol but it's better and works at 115200
i will add some basic functions to my python side and add a rainbow effect I will also add GPU temperature and etc... it's not going to be really hard on linux but windows, im not sure :D
setup()
function as well. The code doesn't make sense when includes, variable declarations, and initialization code isn't shown. That is not to say it will make sense when they are shown :), merely that without them it doesn't.