An Adafruit DotStar based light organesque thingie
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

seeeduino-pixel-organ.ino 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <Adafruit_DotStar.h>
  2. #include <SPI.h> // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET
  3. #define DEBUG true
  4. #define NUMPIXELS 72 // Number of LEDs in strip
  5. #define DATAPIN 4
  6. #define CLOCKPIN 5
  7. #define DECAYFACTOR 0.95
  8. Adafruit_DotStar strip = Adafruit_DotStar(
  9. NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
  10. #define MICPIN A0 // microphone
  11. //Turns out QUIETPIN is not that important
  12. //#define QUIETPIN A1 // potentiometer to determine "quiet" level (no output below this)
  13. #define LOUD 768 // constant. Higher than that means compress to 0..255
  14. #define BUFFSIZE 96 // at least NUMPIXELS!
  15. #define AVGSIZE 5
  16. #define MILLI_AMNESIA 23 // How much to "falsely remeber" current value as history (1..999)
  17. int rawBuff[BUFFSIZE];
  18. int notHighBuff[BUFFSIZE];
  19. int lowBuff[BUFFSIZE];
  20. int index, minLow, maxLow, minMid, maxMid, minHigh, maxHigh, dynRange;
  21. #define rawLow(i) (lowBuff[i])
  22. #define rawMid(i) (notHighBuff[i]-lowBuff[i])
  23. #define rawHigh(i) (rawBuff[i]-notHighBuff[i])
  24. #define normLow(i) map(rawLow(i),minLow,maxLow,0,dynRange)
  25. #define normMid(i) map(rawMid(i),minMid,maxMid,0,dynRange)
  26. #define normHigh(i) map(rawHigh(i),minHigh,maxHigh,0,dynRange)
  27. unsigned long lastTime, thisTime;
  28. void setup() {
  29. if (DEBUG) {
  30. Serial.begin(230400);
  31. }
  32. lastTime = millis();
  33. index = 0;
  34. strip.begin(); // Initialize pins for output
  35. strip.show(); // Turn all LEDs off ASAP
  36. for (int i = 0; i < BUFFSIZE; i++) {
  37. rawBuff[i] = notHighBuff[i] = lowBuff[i] = 512;
  38. }
  39. }
  40. void loop() {
  41. thisTime = millis();
  42. unsigned long rate = 1000 / max(thisTime - lastTime, 1);
  43. rawBuff[index] = analogRead(MICPIN);
  44. notHighBuff[index] = 0;
  45. int nextIndex = (index + 1) % BUFFSIZE;
  46. for (int i = nextIndex + BUFFSIZE - AVGSIZE; i % BUFFSIZE != nextIndex; i++) {
  47. notHighBuff[index] += rawBuff[i % BUFFSIZE];
  48. }
  49. notHighBuff[index] /= AVGSIZE;
  50. lowBuff[index] = (notHighBuff[index] + notHighBuff[(index + BUFFSIZE - AVGSIZE) % BUFFSIZE]) / 2;
  51. minLow = minMid = minHigh = 1024;
  52. maxLow = maxMid = maxHigh = -1;
  53. long pseudoRaw = rawBuff[index] * MILLI_AMNESIA;
  54. long pseudoNotHigh = notHighBuff[index] * MILLI_AMNESIA;
  55. long pseudoLow = lowBuff[index] * MILLI_AMNESIA;
  56. for (int i = 0; i < BUFFSIZE; i++) {
  57. rawBuff[i] = (long(rawBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoRaw)) / 1000;
  58. notHighBuff[i] = (long(notHighBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoNotHigh)) / 1000;
  59. lowBuff[i] = (long(lowBuff[i] * long(1000 - MILLI_AMNESIA) + pseudoLow)) / 1000;
  60. minLow = min(minLow, rawLow(i));
  61. minMid = min(minMid, rawMid(i));
  62. minHigh = min(minHigh, rawHigh(i));
  63. maxLow = max(maxLow, rawLow(i));
  64. maxMid = max(maxMid, rawMid(i));
  65. maxHigh = max(maxHigh, rawHigh(i));
  66. }
  67. int quiet = LOUD / 5; // was: map(analogRead(QUIETPIN), 0, 1024, 0, LOUD * 7 / 8);
  68. dynRange = constrain(
  69. map(max(maxLow, max(maxMid, maxHigh)) - min(minLow, min(minMid, minHigh)),
  70. quiet, LOUD, 0, 255),
  71. 0, 255);
  72. int buffIndex = index;
  73. float ledFactor = 1.0;
  74. for (int i = 0; i < NUMPIXELS; i++) {
  75. uint32_t rgb = uint32_t(
  76. ((uint32_t(float(normHigh(buffIndex)) * ledFactor) << 8) +
  77. uint32_t(float(normMid(buffIndex)) * ledFactor) << 8) +
  78. uint32_t(float(normLow(buffIndex)) * ledFactor));
  79. strip.setPixelColor(NUMPIXELS - i, rgb);
  80. buffIndex = (buffIndex + BUFFSIZE - 1) % BUFFSIZE;
  81. ledFactor *= DECAYFACTOR;
  82. }
  83. strip.show();
  84. if (DEBUG) {
  85. Serial.print(dynRange);
  86. Serial.print(' ');
  87. Serial.print(normLow(index));
  88. Serial.print(' ');
  89. Serial.print(normMid(index));
  90. Serial.print(' ');
  91. Serial.print(normHigh(index));
  92. Serial.print(' ');
  93. Serial.print('\n');
  94. }
  95. index = nextIndex;
  96. lastTime = thisTime;
  97. delay(1); // this is considered good manners ;)
  98. }