Advertisement
Guest User

Untitled

a guest
Mar 16th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <avr/power.h>
  3.  
  4. #define PIN 9
  5. #define NUMPIXELS 1
  6. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  7.  
  8. void setup() {
  9.  
  10. pixels.begin();
  11. }
  12.  
  13. void loop() {
  14.  
  15. //hue from 0 to 360 degrees
  16. for(int hue = 0; hue < 360; hue++) {
  17.  
  18. // saturation and value = 1
  19. setLedColorHSV(hue, 1, 1);
  20. delay(50);
  21. }
  22. }
  23.  
  24. // thanks to http://forum.arduino.cc/index.php?topic=307655.5
  25. void setLedColorHSV(int h, double s, double v) {
  26.  
  27. double r=0;
  28. double g=0;
  29. double b=0;
  30. double hf=h/60.0;
  31.  
  32. int i=(int)floor(h/60.0);
  33. double f = h/60.0 - i;
  34. double pv = v * (1 - s);
  35. double qv = v * (1 - s*f);
  36. double tv = v * (1 - s * (1 - f));
  37.  
  38. switch (i)
  39. {
  40. case 0:
  41. r = v;
  42. g = tv;
  43. b = pv;
  44. break;
  45. case 1:
  46. r = qv;
  47. g = v;
  48. b = pv;
  49. break;
  50. case 2:
  51. r = pv;
  52. g = v;
  53. b = tv;
  54. break;
  55. case 3:
  56. r = pv;
  57. g = qv;
  58. b = v;
  59. break;
  60. case 4:
  61. r = tv;
  62. g = pv;
  63. b = v;
  64. break;
  65. case 5:
  66. r = v;
  67. g = pv;
  68. b = qv;
  69. break;
  70. }
  71.  
  72. //set each component to a integer value between 0 and 255
  73. int red = constrain((int)255*r,0,255);
  74. int green = constrain((int)255*g,0,255);
  75. int blue = constrain((int)255*b,0,255);
  76.  
  77. pixels.setPixelColor(0, pixels.Color(red, green, blue));
  78. pixels.show();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement