Latest revision |
Your text |
Line 31: |
Line 31: |
| </pre> | | </pre> |
| | | |
− | == StripSerial == | + | == StripHSV == |
| Rainbeau test sketch for strip of addressable RGB LEDs. | | Rainbeau test sketch for strip of addressable RGB LEDs. |
| | | |
| Requires http://fastled.io/ library. | | Requires http://fastled.io/ library. |
− |
| |
− | Updated with optional remote serial control from PC. [[User:Six26|Six26]] ([[User talk:Six26|talk]]) 22:19, 12 September 2014 (UTC)
| |
| | | |
| <pre> | | <pre> |
− | /*
| |
− | * Serial control interface for LED Strip
| |
− | *
| |
− | * Updated: 20140923
| |
− | *
| |
− | * H4X0r'd some shitz uppple
| |
− | * -- insert LED of some sort in A0 & Ground (in right direction)
| |
− | * -- pseudo lite senzor for mode switching
| |
− | * -- pseudo lite mode 8 noise floor graph
| |
− | *
| |
− | * Command Summary:
| |
− | * MODE0 == Auto (Rainbeau)
| |
− | * MODE1 == Off
| |
− | * MODE2 == Remote (PC Control)
| |
− | * HH..H == Sequence of Hue values to apply to LEDs
| |
− | * Note: Set "MODE2" before sending hues
| |
− | * S == Set saturation
| |
− | * MODE3 == Center Peak Rainbeau
| |
− | * MODE4 == Center Peak Pastel
| |
− | */
| |
− |
| |
| #include "FastLED.h" | | #include "FastLED.h" |
| | | |
| // How many leds in your strip? | | // How many leds in your strip? |
− | #define NUM_LEDS 72 | + | #define NUM_LEDS 12 |
| | | |
| // Spacing between pixel/color | | // Spacing between pixel/color |
− | #define HSV_PAD 4 | + | #define HSV_PAD 22 |
| | | |
| // Delay between cycles | | // Delay between cycles |
− | #define LOOP_DELAY 50 | + | #define LOOP_DELAY 20 |
| | | |
− | // Strip Data In (DI) | + | // For led chips like Neopixels, which have a data line, ground, and power, you just |
| + | // need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock, |
| + | // ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN |
| #define DATA_PIN 11 | | #define DATA_PIN 11 |
| + | // #define CLOCK_PIN 13 |
| | | |
| // Define the array of leds | | // Define the array of leds |
| CRGB leds[NUM_LEDS]; | | CRGB leds[NUM_LEDS]; |
| | | |
− | float counter = 0;
| |
| byte count = 0; | | byte count = 0; |
− |
| |
− | byte mode = 8;
| |
− | byte hues[NUM_LEDS];
| |
− | byte sat = 255; // Saturation 0-255
| |
− |
| |
− | boolean irEnable = true;
| |
− | float ir = 0.0;
| |
− |
| |
− | const int morse[27] = {2,0,1,0,1,0,2,0,0,2,0,2,0,2,0,0,2,0,1,0,1,0,2,0,0,0,0};
| |
− |
| |
− | String inputString = "";
| |
| | | |
| void setup() { | | void setup() { |
− | pinMode(13, OUTPUT);
| + | // Uncomment/edit one of the following lines for your leds arrangement. |
− |
| + | FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | for (int i = 0; i < NUM_LEDS; i++)
| + | |
− | {
| + | // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | hues[i] = 0;
| + | // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | }
| + | // FastLED.addLeds<NEOPIXEL, DATA_PIN, RGB>(leds, NUM_LEDS); |
− |
| |
− | /*
| |
− | * Confirm GRB color ordering for WS2812b
| |
− | *
| |
− | * \/ \/ \/ \/ BIG FAT NOTE \/ \/ \/ \/
| |
− | *
| |
− | */
| |
− | FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
| |
− |
| |
− | // Max brightness 0-255 (64 ~< 100ma for 12 LEDs)
| |
− | FastLED.setBrightness(64);
| |
− |
| |
− | Serial.begin(9600);
| |
− | Serial.setTimeout(50);
| |
− |
| |
− | delay(200);
| |
− | }
| |
− | | |
− | void loop() {
| |
− | int i;
| |
− | int v;
| |
− | float p;
| |
| | | |
− | if (irEnable)
| + | // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | {
| + | // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | ir = max((analogRead(A0) / 1.0 - ir) * 0.5 - 10 + ir, ir);
| + | // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | if (ir > 60)
| + | // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS); |
− | {
| + | // FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS); |
− | // Bump mode
| + | // FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS); |
− | ir = 0;
| + | // FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS); |
− | if (++mode > 8)
| + | // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); |
− | mode = 0;
| + | // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); |
− | }
| + | // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS); |
− | }
| |
− | | |
− |
| |
− | if (mode == 0)
| |
− | {
| |
− | // Rainbeau
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | leds[i] = CHSV(count + (i * HSV_PAD), sat, 255);
| |
− | }
| |
− | FastLED.show();
| |
− | count++;
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 1)
| |
− | {
| |
− | // Off
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | leds[i] = CRGB(0, 0, 0);
| |
− | }
| |
− | FastLED.show();
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 2)
| |
− | {
| |
− | // Remote
| |
− | }
| |
− | else if (mode == 3)
| |
− | {
| |
− | // Rainbeau
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | // variable position dimming | |
− | // Set 'p' to 0.0 - 1.0 so the ends are 0.0 and the center is 1.0
| |
− | p = (((NUM_LEDS / 2) - abs(i - (NUM_LEDS / 2.0)))) / (NUM_LEDS / 2.0);
| |
− | v = min(255, round(p * 255.0) + 64);
| |
− | | |
− | // sin function added for oscillating color change | |
− | counter += 0.000001337;
| |
− | count = sin(counter * 3.14 * 180) * 255 + (i * HSV_PAD);
| |
− | leds[i] = CHSV(count, sat, v);
| |
− | }
| |
− | FastLED.show();
| |
− | count++;
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 4)
| |
− | {
| |
− | // Rainbeau
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | // variable position dimming | |
− | // Set 'p' to 0.0 - 1.0 so the ends are 0.0 and the center is 1.0
| |
− | p = (((NUM_LEDS / 2) - abs(i - (NUM_LEDS / 2.0)))) / (NUM_LEDS / 2.0);
| |
− | v = min(255, round(p * 255.0) + 64);
| |
− |
| |
− | // sin function added for oscillating color change | |
− | counter += 0.000001337;
| |
− | count = sin(counter * 3.14 * 180) * 255 + (i * HSV_PAD);
| |
− | leds[i] = CHSV(count, sat - (v/1.5), v);
| |
− | }
| |
− | FastLED.show();
| |
− | count++;
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 5)
| |
− | {
| |
− | // Monochrome/Peak
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | p = (((NUM_LEDS / 2) - abs(i - (NUM_LEDS / 2.0)))) / (NUM_LEDS / 2.0); | |
− | v = min(255, round(p * 255.0) + 16);
| |
− | leds[i] = CRGB(v, 0, 0); // CHSV(100, sat, v);
| |
− | }
| |
− |
| |
− | // leds[int(random(0, NUM_LEDS))] = CRGB(96,0,0);
| |
− |
| |
− | FastLED.show();
| |
− | count++;
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 6)
| |
− | {
| |
− | // count = 0;
| |
− |
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | leds[i] = CHSV(morse[(i + count) % 27] == 1 ? 120 : 0, sat, morse[(i + count) % 27] == 0 ? 0 : 255);
| |
− | }
| |
− | FastLED.show();
| |
− |
| |
− | count++;
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− | else if (mode == 7)
| |
− | {
| |
− | byte r;
| |
− |
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | // counter = sin(count * 3.14 * 180) * 255 + i;
| |
| | | |
− | r = random(127, 255); | + | FastLED.setBrightness(64); |
− | leds[i] = CRGB(r, random(0, 100) < 20 ? r : 0, 0);
| |
− | }
| |
− | // count++;
| |
− |
| |
− | FastLED.show();
| |
− |
| |
− | delay(LOOP_DELAY / 5);
| |
− | }
| |
− | else if (mode == 8)
| |
− | {
| |
− | // ir = max((analogRead(A0) / 1.0 - ir) * 0.5 - 10 + ir, ir);
| |
− |
| |
− | for (i = 0; i < NUM_LEDS; i++)
| |
− | {
| |
− | if (i < ir)
| |
− | {
| |
− | leds[i] = CHSV(0, 255, 255); // ir > 30 ? 127 : 32);
| |
− | }
| |
− | else
| |
− | {
| |
− | leds[i] = CHSV(66, 255, 32);
| |
− | }
| |
− | }
| |
− |
| |
− | // ir = min(0, ir - 1.337);
| |
− |
| |
− | FastLED.show();
| |
− |
| |
− | delay(LOOP_DELAY);
| |
− | }
| |
− |
| |
− | if (irEnable)
| |
− | ir = min(0, ir - 1.337);
| |
| } | | } |
| | | |
− | void updateHues() { | + | void loop() { |
| + | // Rainbeau |
| for (int i = 0; i < NUM_LEDS; i++) | | for (int i = 0; i < NUM_LEDS; i++) |
| { | | { |
− | leds[i] = CHSV(hues[i], 255, 255); | + | leds[i] = CHSV(count + (i * HSV_PAD), 255, 255); |
| } | | } |
| FastLED.show(); | | FastLED.show(); |
| | | |
− | // Toggle onboard LED | + | count++; |
− | digitalWrite(13, !digitalRead(13));
| |
− | }
| |
− | | |
− | void serialEvent() {
| |
− | byte byteCount = 0;
| |
− | byte data;
| |
− |
| |
− | inputString = "";
| |
− |
| |
− | while(Serial.available())
| |
− | {
| |
− | data = Serial.read();
| |
− | inputString += (char)data;
| |
− | if (byteCount < NUM_LEDS)
| |
− | {
| |
− | hues[byteCount++] = data;
| |
− | }
| |
− |
| |
− | // Catch pending bits
| |
− | if (!Serial.available())
| |
− | delay(20);
| |
− | }
| |
| | | |
− | if (inputString == "MODE0") | + | delay(LOOP_DELAY); |
− | {
| |
− | mode = 0;
| |
− | }
| |
− | else if (inputString == "MODE1")
| |
− | {
| |
− | mode = 1;
| |
− | }
| |
− | else if (inputString == "MODE2")
| |
− | {
| |
− | mode = 2;
| |
− | }
| |
− | else if (inputString == "MODE3")
| |
− | {
| |
− | mode = 3;
| |
− | }
| |
− | else if (inputString == "MODE4")
| |
− | {
| |
− | mode = 4;
| |
− | }
| |
− | else if (inputString == "NINJA")
| |
− | {
| |
− | mode = 5;
| |
− | }
| |
− | else if (inputString == "MORSE")
| |
− | {
| |
− | mode = 6;
| |
− | }
| |
− | else if (inputString.length() == NUM_LEDS)
| |
− | {
| |
− | updateHues();
| |
− | }
| |
− | else if (inputString.length() == 1)
| |
− | {
| |
− | sat = data;
| |
− | }
| |
| } | | } |
| </pre> | | </pre> |
| | | |
| == Desktop Emulator == | | == Desktop Emulator == |
− | [[File:HSV.jpg|960px]]
| |
− |
| |
− | See also: [[FlashDevelop]]
| |
− |
| |
| | | |
| === Main === | | === Main === |