Hi @skatun
I have some RGB lights, using mysensors and openhab
Item configuration
Color Light_6_0 "Light Left" (gBio,gLights) {mysensors="6;0;V_RGB"}
Sketch
#include <MySensor.h>
#include <ChainableLED.h>
#include <SPI.h>
#define NODE_ID 6
#define NUM_LEDS 1
int counter[NUM_LEDS];
int current[NUM_LEDS][3];
int step[NUM_LEDS][3];
unsigned long SLEEP_TIME = 10;
MySensor gw;
//ChainableLED leds(7, 8, 1);
ChainableLED leds(5, 6, NUM_LEDS); // CLK, DATA, LEDS
void setup() {
leds.init();
for(byte childId = 0; childId < NUM_LEDS; childId++) {
counter[childId] = 0;
current[childId][0] = 0;
current[childId][1] = 0;
current[childId][2] = 0;
step[childId][0] = 0;
step[childId][1] = 0;
step[childId][2] = 0;
leds.setColorRGB(childId, current[childId][0], current[childId][1], current[childId][2]); // Turn of on startup
}
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, NODE_ID, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("RGB Led", "1.2");
for(byte childId = 0; childId < NUM_LEDS; childId++) {
gw.present(childId, S_RGB_LIGHT);
}
}
void loop() {
// Alway process incoming messages whenever possible
gw.process();
for(byte childId = 0; childId < NUM_LEDS; childId++) {
if(counter[childId] >= 0) {
counter[childId]--;
int i = 1020 - counter[childId];
current[childId][0] = calculateVal(step[childId][0], current[childId][0], i);
current[childId][1] = calculateVal(step[childId][1], current[childId][1], i);
current[childId][2] = calculateVal(step[childId][2], current[childId][2], i);
leds.setColorRGB(childId, current[childId][0], current[childId][1], current[childId][2]);
}
}
gw.wait(SLEEP_TIME);
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_RGB) {
String hexstring = message.getString();
long number = (long) strtol( &hexstring[0], NULL, 16);
int colorR = number >> 16;
int colorG = number >> 8 & 0xFF;
int colorB = number & 0xFF;
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", Red: ");
Serial.print(colorR);
Serial.print(", Green: ");
Serial.print(colorG);
Serial.print(", Blue: ");
Serial.print(colorB);
Serial.print(", New status: ");
Serial.println(message.getString());
setColor(message.sensor, colorR, colorG, colorB);
//leds.setColorRGB(message.sensor, colorR, colorG, colorB);
}
}
void setColor(byte led, int R, int G, int B) {
step[led][0] = calculateStep(current[led][0], R);
step[led][1] = calculateStep(current[led][1], G);
step[led][2] = calculateStep(current[led][2], B);
counter[led] = 255;
}
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 255/step; // divide by 1020
}
return step;
}
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
The sketch may be a bit over complicated, but the important part is the incomingMessage, all the rest is because I like to fade between the colors.
Hope it help you.