Kleine RGB LEDs
Kleine RGB LEDs
Wir verwenden WS2801 Breakout Boardsvon Sparkfun. Pin Belegung: Digital3 - Clock Digital2 - Serial Data 5V GND Die LEDs können zu einer Kette aneinander gehängt werden. Anbei ein kleines Code Beispiel:/* Based on Code by Nathan Seidle SparkFun Electronics 2011 Each IC requires 24 bits of 'greyscale' data. This means you can have 256 levels of red, 256 of blue, and 256 levels of green for each RGB LED. REALLY granular. You will need to connect 5V/Gnd from the Arduino (USB power seems to be sufficient). 4-pin connection: 5V - 5V SDI - PIN2 CKI - PIN3 GND - GND */ // Arduino Pins: int SDI = 2; int CKI = 3; #define STRIP_LENGTH 3 // 3 LEDs on this strip <---- adjust here long strip_colors[STRIP_LENGTH]; // create array void setup() { pinMode(SDI, OUTPUT); // Pin definitions pinMode(CKI, OUTPUT); //Clear out the array for(int x = 0 ; x < STRIP_LENGTH ; x++) strip_colors[x] = 0; // Serial.begin(9600); // Serial.println("Hello!"); } void loop() { long input0 = analogRead(0) /4; // 3 analog inputs 0-1023 mapped to 8bit 0-255 long input1 = analogRead(1) /4; long input2 = analogRead(2) /4; // Serial.println(input0, HEX); // show hex // Serial.println(input1, HEX); // Serial.println(input2, HEX); long color = input0 << 16 | input1 << 8 | input2; // bit shifted to one r g b value // Serial.println(color, HEX); long led0 = color; // analog inputs make one color long led1 = 0xFFFFFF; // 2nd led is white long led2 = 0xFF0000; // 3rd led is red strip_colors[0] = led0; // strip_colors[1] = led1; //Bright Green strip_colors[2] = led2; //Bright Blue post_frame(); //Push the current color array to the strip delay(500); // wait for a bit (wait period may be faster...) } //Takes the current strip color array and pushes it out void post_frame (void) { //Each LED requires 24 bits of data //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor //Pulling the clock low for 500us or more causes the IC to post the data. for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) { long this_led_color = strip_colors[LED_number]; //24 bits of color data for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) { //Feed color bit 23 first (red data MSB) digitalWrite(CKI, LOW); //Only change data when clock is low long mask = 1L << color_bit; //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit. if(this_led_color & mask) digitalWrite(SDI, HIGH); else digitalWrite(SDI, LOW); digitalWrite(CKI, HIGH); //Data is latched when clock goes high } } //Pull clock low to put strip into reset/post mode digitalWrite(CKI, LOW); delayMicroseconds(500); //Wait for 500us to go into reset }Hier noch ein Beispiel, wie die High Power LEDs mit den kleinen WS2801 LEDs kombiniert werden können. // Code for the HPRGB board driving a 8.5 Watt Dealextreme LED // and Sparkfun 10504 WS2801 Breakout board LED chain // Libraries --------------------------------------------------------- #include <Wire.h> #include <HPRGB2.h> // Instance of HPRGB Object ------------------------------------------ HPRGB ledShield; // default mcp4728 id(0) and default PCA9685 id(0) // Pin definitions --------------------------------------------------- // define the pins for buttons and switches #define RGBPIN 4 #define BUT1 3 #define BUT2 2 // define small rgb led pins #define SDI 6 #define CKI 7 #define STRIP_LENGTH 2 // adjust length of small LED strip here <---- !!! // Variables --------------------------------------------------------- boolean rgbState = true; long slider0 = 0; long slider1 = 0; long slider2 = 0; int whiteState; byte rgbArray[3]; // array to store rgb values converted from hsb to rgb long strip_colors[STRIP_LENGTH]; // array holding colors for all leds in strip // Setup ------------------------------------------------------------- void setup() { ledShield.begin(); ledShield.setCurrent(350,350,350); // set maximum current for channel 1-3 (mA) ledShield.setFreq(600 );// operation frequency of the LED driver (KHz) ledShield.eepromWrite();// write current settings to EEPROM delay(100); // wait for EEPROM writing pinMode(RGBPIN, INPUT); pinMode(BUT1, INPUT); pinMode(BUT2, INPUT); digitalWrite(RGBPIN, HIGH); digitalWrite(BUT1, HIGH); digitalWrite(BUT2, HIGH); pinMode(SDI, OUTPUT); // small LED pins pinMode(CKI, OUTPUT); //Clear out the array for(int x = 0 ; x < STRIP_LENGTH ; x++) strip_colors[x] = 0; Serial.begin(9600); } // Loop ------------------------------------------------------------- void loop() { rgbState = digitalRead(RGBPIN); // read state of rgb/hsb switch slider0 = analogRead(0) /4; // read slider 1 - 3 and convert to from 10 to 8 bit slider1 = analogRead(1) /4; slider2 = analogRead(2) /4; whiteState = digitalRead(BUT1); // read 'white' button if (whiteState == LOW){ // 'white' button is pressed ledShield.goToRGB(127, 127, 127); } if (whiteState == HIGH){ // if 'white' button is not pressed if (rgbState == HIGH){ // we are in rgb mode --------- RGB ledShield.goToRGB(slider0, slider1, slider2); strip_colors[0] = slider0 << 16 | slider1 << 8 | slider2; // bit shift this rgb color into a 24bit value strip_colors[1] = strip_colors[0]; // save into strip_colors array post_frame(); // and send it out to the small rgb led strip } if (rgbState == LOW) { // we are in hsb mode --------- HSB ledShield.goToHSB(slider0, slider1, slider2); // set color in HIGH Power LEDs Serial.println(slider0, DEC); byte s0 = slider0; byte s1 = slider1; byte s2 = slider2; convertHSBtoRGB(s0, s1, s2, rgbArray); // convert the same hsb color into rgb // convert bytes to long because we will bit shift afterwards!!! long red = rgbArray[0]; long green = rgbArray[1]; long blue = rgbArray[2]; Serial.println(rgbArray[0], HEX); strip_colors[0] = red << 16 | green << 8 | blue; // bit shift this rgb color into a 24bit value strip_colors[1] = strip_colors[0]; post_frame(); // and send it out to the small rgb led strip } } } // Functions ------------------------------------------------------------------------------------------------ // Helper Function to convert HSB values to RGB // HSV to RGB adopted from cyzRGB and adafruit // http://code.google.com/p/codalyze/source/browse/cyz_rgb/trunk/cyz/color.c // https://github.com/adafruit/RGB-matrix-Panel/blob/master/examples/plasma/plasma.pde void convertHSBtoRGB(byte h, byte s, byte b, byte rgbData[]){ if ( s == 0 ){ rgbData[0] = rgbData[1] = rgbData[2] = b; } else { int i = ((unsigned int)h * 6) / 256; int f = ((unsigned int)h * 6) % 256; int p = ((unsigned int)b * (255 - (unsigned int)s)) / 256; int q = ((unsigned int)b * (255 - ((unsigned int)s * f) / 256)) / 256; int t = ((unsigned int)b * (255 - ((unsigned int)s * (255 - f)) / 256)) / 256; if ( i == 0 ) { rgbData[0] = b ; rgbData[1] = t ; rgbData[2] = p; } // 0 deg (r) to 60 deg (r+g) else if ( i == 1 ) { rgbData[0] = q ; rgbData[1] = b ; rgbData[2] = p; } // 60 deg (r+g) to 120 deg (g) else if ( i == 2 ) { rgbData[0] = p ; rgbData[1] = b ; rgbData[2] = t; } // 120 deg (g) to 180 deg (g+b) else if ( i == 3 ) { rgbData[0] = p ; rgbData[1] = q ; rgbData[2] = b; } // 180 deg (g+b) to 240 deg (b) else if ( i == 4 ) { rgbData[0] = t ; rgbData[1] = p ; rgbData[2] = b; } // 240 deg (b) to 300 deg (b+r) else { rgbData[0] = b ; rgbData[1] = p ; rgbData[2] = q; } // 300 deg (b+r) to 0 deg (r) } } //Takes the current strip color array and pushes it out void post_frame (void) { for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) { long this_led_color = strip_colors[LED_number]; //24 bits of color data for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) { digitalWrite(CKI, LOW); //Only change data when clock is low long mask = 1L << color_bit; if(this_led_color & mask) digitalWrite(SDI, HIGH); else digitalWrite(SDI, LOW); digitalWrite(CKI, HIGH); //Data is latched when clock goes high } } digitalWrite(CKI, LOW); delayMicroseconds(500); //Wait for 500us to go into reset } [/code]