StefanWxx 2 Posted June 4, 2014 Share Posted June 4, 2014 Hi, in my code i want so set the color for a rgb led stripe. I use the WS2811Driver library. I tried this but it doesn't work: // blink_strip - ws2811 led strip blink example // 11-15-2012 Initial Version 0.1 #include <WS2811Driver.h> byte leds0[30]; byte leds1[30]; // 24 bits of color data stored GGRRBB //const uint8_t leds0[] = { 0x00,0xff,0x00, 0xff,0x00,0x00, 0x00,0x00,0xff, 0xff,0xff,0xff }; // R,G,B,W //const uint8_t leds1[] = { 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00, 0x00,0x00,0x00 }; // 0,0,0,0, WS2811Driver ledStrip; // uses P1_7 as datapin connect to DIN on strip void setup(void) { ledStrip.setLEDCount(10); // setup for 4 leds on a strip ledStrip.begin(); // configure P1.7 for output } void loop() { //ledStrip.write(leds0); // set leds to red,green,blue,white //delay(500); //ledStrip.write(leds1,sizeof(leds1)); // turn off all 4 leds //delay(500); String color = "0000FF"; for(byte x = 0; x < 30 ; x++){ leds0[x] = byte('F'); //leds0[x+1] = byte(color.substring(2,3)); //leds0[x+2] = byte(color.substring(4,5)); } ledStrip.write(leds0); delay(5000); for(byte x = 0; x < 30 ; x++){ leds1[x] = 0x00; //leds0[x+1] = byte(color.substring(2,3)); //leds0[x+2] = byte(color.substring(4,5)); } ledStrip.write(leds1); delay(5000); } How can I convert the string to 3 bytes? Quote Link to post Share on other sites
roadrunner84 466 Posted June 4, 2014 Share Posted June 4, 2014 #include <cstdlib> const char* s = "ff33bb"; unsigned long a = strtoul (s, NULL, 16); // a is equal to 0xff33bb is equal to 996283 // to break the 32-bit integer into three separate 8-bit integers unsigned char b[3]; b[0] = (a >> 16) & 0xFF; b[1] = (a >> 8) & 0xFF; b[2] = a & 0xFF; // b[0] is equal to 0xff is equal to 255 // b[1] is equal to 0x33 is equal to 51 // b[2] is equal to 0xbb is equal to 187 StefanWxx 1 Quote Link to post Share on other sites
StefanWxx 2 Posted June 4, 2014 Author Share Posted June 4, 2014 Okay, just another Question about this.. How can ich make this with a loop: void loop() { const char* color = "34acef"; //GGRRBB unsigned long a = strtoul (color, NULL, 16); // want to do this in a loop: leds0[0] = (a >> 16) & 0xFF; leds0[1] = (a >> 8) & 0xFF; leds0[2] = a & 0xFF; leds0[3] = (a >> 16) & 0xFF; leds0[4] = (a >> 8) & 0xFF; leds0[5] = a & 0xFF; leds0[6] = (a >> 16) & 0xFF; leds0[7] = (a >> 8) & 0xFF; leds0[8] = a & 0xFF; leds0[9] = (a >> 16) & 0xFF; leds0[10] = (a >> 8) & 0xFF; leds0[11] = a & 0xFF; leds0[12] = (a >> 16) & 0xFF; leds0[13] = (a >> 8) & 0xFF; leds0[14] = a & 0xFF; leds0[15] = (a >> 16) & 0xFF; leds0[16] = (a >> 8) & 0xFF; leds0[17] = a & 0xFF; ledStrip.write(leds0); delay(5000); } Quote Link to post Share on other sites
yosh 121 Posted June 4, 2014 Share Posted June 4, 2014 @@StefanWxx Something like: for(int i=0;i<6;i++) { leds0[i*3 + 0] = (a >> 16) & 0xFF; leds0[i*3 + 1] = (a >> 8) & 0xFF; leds0[i*3 + 2] = a & 0xFF; } StefanWxx 1 Quote Link to post Share on other sites
StefanWxx 2 Posted June 4, 2014 Author Share Posted June 4, 2014 Thank you, it works beautifully :-) Quote Link to post Share on other sites
jpnorair 340 Posted June 4, 2014 Share Posted June 4, 2014 Why don't you just use binary arrays instead? Maybe something like this: uint8_t array[3] = { 0x00, 0x11, 0x22 }; Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.