Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "RGB.h"
- RGB led(9, 10, 11);
- byte *colors[] = { &led.color.r, &led.color.g, &led.color.b };
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- if (Serial.available() > 0)
- {
- unsigned long rgb = 0;
- for (int i=6; i>0; i--)
- {
- byte incomingByte = Serial.read();
- #ifdef DEBUG
- Serial.println(incomingByte);
- #endif
- rgb += hex2int(incomingByte)*myPow(16, i-1); // MFB first
- }
- led.color.r = (rgb >> 16);
- led.color.g = ((rgb >> 8) & 0xFF);
- led.color.b = (rgb & 0xFF);
- led.write();
- #ifdef DEBUG
- Serial.print(*colors[0], HEX);
- Serial.print(*colors[1], HEX);
- Serial.print(*colors[2], HEX);
- Serial.println();
- #endif
- }
- }
- int hex2int(char c)
- {
- if (c >= '0' && c <= '9') {
- return c - '0';
- } else if (c >= 'a' && c <= 'f') {
- return c - 'a' + 10;
- } else if (c >= 'A' && c <= 'F') {
- return c - 'A' + 10;
- } else {
- return -1;
- }
- }
- unsigned long myPow(int b, int e)
- {
- unsigned long result = 1;
- for (; e>0; e--)
- result *= b;
- return result;
- }
Add Comment
Please, Sign In to add comment