Advertisement
Guest User

Color Rainbow

a guest
Jul 22nd, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. // Rainbow color changing RGB leds example
  2. // I am using common cathode RGB leds
  3. int PIN_RED = 10;
  4. int PIN_GREEN = 11;
  5. int PIN_BLUE = 9;
  6. int counter = 0;
  7.  
  8. // Number of colors used for animating, higher = smoother and slower animation)
  9. int numColors = 255;
  10.  
  11.   // The combination of numColors and animationDelay determines the
  12.   // animation speed, I recommend a higher number of colors if you want
  13.   // to slow down the animation. Higher number of colors = smoother color changing.
  14. int animationDelay = 10; // number milliseconds before RGB LED changes to next color
  15.  
  16. void setup() {
  17.   pinMode(PIN_RED, OUTPUT);
  18.   pinMode(PIN_BLUE, OUTPUT);
  19.   pinMode(PIN_GREEN, OUTPUT);
  20. }
  21.  
  22.  
  23. void loop() {
  24.   // This part takes care of displaying the
  25.   // color changing in reverse by counting backwards if counter
  26.   // is above the number of available colors  
  27.   float colorNumber = counter > numColors ? counter - numColors: counter;
  28.  
  29.   // Play with the saturation and brightness values
  30.   // to see what they do
  31.   float saturation = 1; // Between 0 and 1 (0 = gray, 1 = full color)
  32.   float brightness = .05; // Between 0 and 1 (0 = dark, 1 is full brightness)
  33.   float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
  34.   long color = HSBtoRGB(hue, saturation, brightness);
  35.  
  36.   // Get the red, blue and green parts from generated color
  37.   int red = color >> 16 & 255;
  38.   int green = color >> 8 & 255;
  39.   int blue = color & 255;
  40.  
  41.   setColor(red, green, blue);
  42.  
  43.   // Counter can never be greater then 2 times the number of available colors
  44.   // the colorNumber = line above takes care of counting backwards (nicely looping animation)
  45.   // when counter is larger then the number of available colors
  46.   counter = (counter + 1) % (numColors * 2);
  47.  
  48.   // If you uncomment this line the color changing starts from the
  49.   // beginning when it reaches the end (animation only plays forward)
  50.   // counter = (counter + 1) % (numColors);
  51.  
  52.   delay(animationDelay);
  53. }
  54.  
  55. void setColor (unsigned char red, unsigned char green, unsigned char blue)
  56. {        
  57.    analogWrite(PIN_RED, red);
  58.     analogWrite(PIN_GREEN, green);
  59.     analogWrite(PIN_BLUE, blue);
  60. }
  61.  
  62. long HSBtoRGB(float _hue, float _sat, float _brightness) {
  63.     float red = 0.0;
  64.     float green = 0.0;
  65.     float blue = 0.0;
  66.    
  67.     if (_sat == 0.0) {
  68.         red = _brightness;
  69.         green = _brightness;
  70.         blue = _brightness;
  71.     } else {
  72.         if (_hue == 360.0) {
  73.             _hue = 0;
  74.         }
  75.  
  76.         int slice = _hue / 60.0;
  77.         float hue_frac = (_hue / 60.0) - slice;
  78.  
  79.         float aa = _brightness * (1.0 - _sat);
  80.         float bb = _brightness * (1.0 - _sat * hue_frac);
  81.         float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
  82.        
  83.         switch(slice) {
  84.             case 0:
  85.                 red = _brightness;
  86.                 green = cc;
  87.                 blue = aa;
  88.                 break;
  89.             case 1:
  90.                 red = bb;
  91.                 green = _brightness;
  92.                 blue = aa;
  93.                 break;
  94.             case 2:
  95.                 red = aa;
  96.                 green = _brightness;
  97.                 blue = cc;
  98.                 break;
  99.             case 3:
  100.                 red = aa;
  101.                 green = bb;
  102.                 blue = _brightness;
  103.                 break;
  104.             case 4:
  105.                 red = cc;
  106.                 green = aa;
  107.                 blue = _brightness;
  108.                 break;
  109.             case 5:
  110.                 red = _brightness;
  111.                 green = aa;
  112.                 blue = bb;
  113.                 break;
  114.             default:
  115.                 red = 0.0;
  116.                 green = 0.0;
  117.                 blue = 0.0;
  118.                 break;
  119.         }
  120.     }
  121.  
  122.     long ired = red * 255.0;
  123.     long igreen = green * 255.0;
  124.     long iblue = blue * 255.0;
  125.    
  126.     return long((ired << 16) | (igreen << 8) | (iblue));
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement