Advertisement
3rdWard_Arduino

class4_combo_rainbow

Jun 24th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. const int red = 0;
  2. const int green = 1;
  3. const int blue = 2;
  4.  
  5. float current_hue = 0;
  6. float current_saturation = 0;
  7.  
  8. int ledPins[] = {
  9.   6, 5, 3};
  10. int ledPWM[] = {
  11.   0, 85, 170};
  12. int ledPWM_i[] = {
  13.   1, 1, 1};
  14.  
  15. void setup(){
  16.   Serial.begin(9600);
  17.   for(int i = 0; i < 3; i++){
  18.     pinMode( ledPins[i], OUTPUT);
  19.   }
  20. }
  21.  
  22. void loop(){
  23.   //animate_rainbow();
  24.   //control_rainbow();
  25.   combo_rainbow();
  26.   delay(10);
  27. }
  28.  
  29. void combo_rainbow(){
  30.   current_hue ++;
  31.   if(current_hue > 360){
  32.    current_hue = 0;
  33.   }
  34.  
  35.   current_saturation = analogRead(0);
  36.   current_saturation = map(current_saturation, 0, 1023, 0, 100);
  37.  
  38.   hsbToRgb(current_hue, current_saturation, 100);
  39.  
  40.   for(int i = 0; i < 3; i++){
  41.     analogWrite(ledPins[i], ledPWM[i]);
  42.     Serial.print(ledPWM[i]);
  43.     Serial.print("  ");
  44.   }
  45.   Serial.println();
  46. }
  47.  
  48. void control_rainbow(){
  49.   current_hue = analogRead(0);
  50.   current_hue = map(current_hue, 0, 1023, 0, 359);
  51.  
  52.   hsbToRgb(current_hue, 100, 100);
  53.   for(int i = 0; i < 3; i++){
  54.     analogWrite(ledPins[i], ledPWM[i]);
  55.     Serial.print(ledPWM[i]);
  56.     Serial.print("  ");
  57.   }
  58.   Serial.println();
  59. }
  60.  
  61.  
  62. void animate_rainbow(){
  63.   current_hue ++;
  64.   if(current_hue > 360){
  65.    current_hue = 0;
  66.   }
  67.   hsbToRgb(current_hue, 100, 100);
  68.  
  69.   for(int i = 0; i < 3; i++){
  70.     analogWrite(ledPins[i], ledPWM[i]);
  71.     Serial.print(ledPWM[i]);
  72.     Serial.print("  ");
  73.   }
  74.   Serial.println();
  75. }
  76.  
  77. //-------------------------------------------------
  78. //            Turns HSB color to RGB color
  79. //-------------------------------------------------
  80.  
  81. void hsbToRgb(float _hue, float _sat, float _bri) {
  82.   if (_bri == 0) {
  83.     // black
  84.     // blue
  85.     ledPWM[0] = 0;
  86.     // green
  87.     ledPWM[1] = 0;
  88.     // red
  89.     ledPWM[2] = 0;
  90.  
  91.     return;
  92.   }
  93.   if (_bri == 100 && _sat == 0){
  94.     // white
  95.     // blue
  96.     ledPWM[0] = 255;
  97.     // green
  98.     ledPWM[1] = 255;
  99.     // red
  100.     ledPWM[2] = 255;
  101.  
  102.     return;
  103.   }
  104.  
  105.   float red_HSB = 0;
  106.   float green_HSB = 0;
  107.   float blue_HSB = 0;
  108.  
  109.   _hue = map(_hue, 0, 360, 0, 360);
  110.   _sat = map(_sat, 0, 100, 0, 100);
  111.   _bri = map(_bri, 0, 100, 0, 100);
  112.  
  113.  
  114.  
  115.   if(_hue == 360) _hue = 0;
  116.  
  117.   _hue /= 60;
  118.   _sat /= 100;
  119.   _bri /= 100;
  120.  
  121.  
  122.  
  123.   int i = (int)floor(_hue);
  124.   float f = _hue - i;
  125.   float p = _bri*(1-_sat);
  126.   float q = _bri*(1-(_sat*f));
  127.   float t = _bri*(1-(_sat*(1-f)));
  128.  
  129.  
  130.  
  131.   switch (i) {
  132.   case 0:
  133.     red_HSB = _bri;
  134.     green_HSB = t;
  135.     blue_HSB = p;
  136.     break;
  137.  
  138.   case 1:
  139.     red_HSB = q;
  140.     green_HSB = _bri;
  141.     blue_HSB = p;
  142.     break;
  143.  
  144.   case 2:
  145.     red_HSB = p;
  146.     green_HSB = _bri;
  147.     blue_HSB = t;
  148.     break;
  149.  
  150.   case 3:
  151.     red_HSB = p;
  152.     green_HSB = q;
  153.     blue_HSB = _bri;
  154.     break;
  155.  
  156.   case 4:
  157.     red_HSB = t;
  158.     green_HSB = p;
  159.     blue_HSB = _bri;
  160.     break;
  161.  
  162.   case 5:
  163.     red_HSB = _bri;
  164.     green_HSB = p;
  165.     blue_HSB = q;
  166.     break;
  167.   }
  168.  
  169.   int r = (int)round(red_HSB*255);
  170.   int g = (int)round(green_HSB*255);
  171.   int b = (int)round(blue_HSB*255);
  172.   // blue
  173.   ledPWM[0] = r;
  174.   // green
  175.   ledPWM[1] = g;
  176.   // red
  177.   ledPWM[2] = b;
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement