Created
November 29, 2016 23:44
-
-
Save lvidarte/2babaf21ad55fd43fab0178fd2a61d15 to your computer and use it in GitHub Desktop.
Neopixels by Serial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#define NEOPIXEL_LEN 10 | |
#define NEOPIXEL_PIN 13 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_LEN, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); | |
void setup () | |
{ | |
Serial.begin(57600); | |
pixels.begin(); | |
} | |
uint8_t getCmd (byte data) | |
{ | |
return (data >> 7) & 1; | |
} | |
uint8_t getValue (byte data) | |
{ | |
return data & 127; | |
} | |
uint8_t getMap (uint8_t value) | |
{ | |
return map(value, 0, 127, 0, 255); | |
} | |
void loop () | |
{ | |
static byte data; | |
static uint8_t i, r, g, b; | |
while (Serial.available() < 4) {} | |
Serial.println("-- start ---------------"); | |
bool error = false; | |
for (uint8_t j = 0; j < 4; j++) | |
{ | |
data = Serial.read(); | |
Serial.print("data "); | |
Serial.println((int) data); | |
uint8_t cmd = getCmd(data); | |
if ((j == 0 && cmd != 0) || (j > 0 && cmd != 1)) | |
{ | |
error = true; | |
break; | |
} | |
switch (j) | |
{ | |
case 0: i = getValue(data); break; | |
case 1: r = getMap(getValue(data)); break; | |
case 2: g = getMap(getValue(data)); break; | |
case 3: b = getMap(getValue(data)); break; | |
} | |
} | |
if ( ! error) | |
{ | |
Serial.print("led "); | |
Serial.print(i); | |
Serial.print(" "); | |
Serial.print(r); | |
Serial.print(" "); | |
Serial.print(g); | |
Serial.print(" "); | |
Serial.println(b); | |
pixels.setPixelColor(i, pixels.Color(r, g, b)); | |
pixels.show(); | |
} | |
else Serial.println("-- error -----------------"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment