Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include "RGB.h"
  2.  
  3. RGB led(9, 10, 11);
  4. byte *colors[] = { &led.color.r, &led.color.g, &led.color.b };
  5.  
  6. void setup()
  7. {
  8.   Serial.begin(9600);
  9. }
  10.  
  11. void loop()
  12. {
  13.   if (Serial.available() > 0)
  14.   {
  15.     unsigned long rgb = 0;
  16.    
  17.     for (int i=6; i>0; i--)
  18.     {
  19.       byte incomingByte = Serial.read();    
  20.       #ifdef DEBUG
  21.       Serial.println(incomingByte);
  22.       #endif
  23.       rgb += hex2int(incomingByte)*myPow(16, i-1); // MFB first
  24.     }
  25.    
  26.     led.color.r = (rgb >> 16);
  27.     led.color.g = ((rgb >> 8) & 0xFF);
  28.     led.color.b = (rgb & 0xFF);
  29.     led.write();
  30.    
  31.     #ifdef DEBUG
  32.     Serial.print(*colors[0], HEX);
  33.     Serial.print(*colors[1], HEX);
  34.     Serial.print(*colors[2], HEX);
  35.     Serial.println();
  36.     #endif
  37.   }
  38. }
  39.  
  40. int hex2int(char c)
  41. {
  42.     if (c >= '0' && c <= '9') {
  43.       return c - '0';
  44.     } else if (c >= 'a' && c <= 'f') {
  45.       return c - 'a' + 10;
  46.     } else if (c >= 'A' && c <= 'F') {
  47.       return c - 'A' + 10;
  48.     } else {
  49.       return -1;
  50.     }
  51. }
  52.  
  53. unsigned long myPow(int b, int e)
  54. {
  55.   unsigned long result = 1;
  56.   for (; e>0; e--)
  57.     result *= b;
  58.   return result;
  59. }
Add Comment
Please, Sign In to add comment