Digispark_Test.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <Adafruit_NeoPixel.h>
  2. void HSVtoRGB(float H, float S,float V,int &R, int &G, int &B){
  3. while(H>360) H-=360;
  4. if(H>360 || H<0 || S>100 || S<0 || V>100 || V<0){
  5. return;
  6. }
  7. float s = S/100;
  8. float v = V/100;
  9. float C = s*v;
  10. float X = C*(1-abs(fmod(H/60.0, 2)-1));
  11. float m = v-C;
  12. float r,g,b;
  13. if(H >= 0 && H < 60){
  14. r = C,g = X,b = 0;
  15. }
  16. else if(H >= 60 && H < 120){
  17. r = X,g = C,b = 0;
  18. }
  19. else if(H >= 120 && H < 180){
  20. r = 0,g = C,b = X;
  21. }
  22. else if(H >= 180 && H < 240){
  23. r = 0,g = X,b = C;
  24. }
  25. else if(H >= 240 && H < 300){
  26. r = X,g = 0,b = C;
  27. }
  28. else{
  29. r = C,g = 0,b = X;
  30. }
  31. R = (r+m)*255;
  32. G = (g+m)*255;
  33. B = (b+m)*255;
  34. }
  35. #define PIN1 0
  36. #define PIN2 3
  37. #define PIN3 4
  38. #define NUMPIXELS 25 // 24
  39. #define CENTER 12.5
  40. Adafruit_NeoPixel pixels[] = {
  41. Adafruit_NeoPixel(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800),
  42. Adafruit_NeoPixel(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800),
  43. Adafruit_NeoPixel(NUMPIXELS, PIN3, NEO_GRB + NEO_KHZ800)
  44. };
  45. int PIN_COUNT = 3;
  46. double hue = 0.0;
  47. void setup() {
  48. pinMode(1, OUTPUT); //LED on Model A
  49. pinMode(PIN1, OUTPUT);
  50. pinMode(PIN2, OUTPUT);
  51. pinMode(PIN3, OUTPUT);
  52. for(int x=0;x<PIN_COUNT; x++) {
  53. pixels[x].begin();
  54. }
  55. }
  56. // the loop routine runs over and over again forever:
  57. void loop() {
  58. static double power = 1.5;
  59. static double fac = 0.5/pow(0.5,power);
  60. for(int x=0;x<PIN_COUNT; x++) {
  61. for(int i=0;i<NUMPIXELS;i++){
  62. int r,g,b;
  63. int hueOffset = (abs(CENTER-i)/(float)CENTER) * 50 * ( x + 1 );
  64. double h = (hue + hueOffset)/360.,y;
  65. while(h>1) h-=1;
  66. if(h<=0.5)
  67. y = pow(h,power)*fac;
  68. else
  69. y = 1-pow(1-h,power)*fac;
  70. HSVtoRGB(y*360, 100, 55,r,g,b);
  71. pixels[x].setPixelColor(i, pixels[x].Color(r,g,b)); // Moderately bright green color.
  72. }
  73. pixels[x].show(); // This sends the updated pixel color to the hardware.
  74. }
  75. hue -= 0.4;
  76. if(hue>=360) {
  77. hue -= 360;
  78. }
  79. if(hue<=0) {
  80. hue = 360 + hue;
  81. }
  82. // digitalWrite(1, LOW); //LED on Model A
  83. delay(15); // Delay for a period of time (in milliseconds).
  84. digitalWrite(1, HIGH); //LED on Model A
  85. }