Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #pragma GCC optimize ("-O1")
  2. #define FHT_N 128
  3. #define LOG_OUT 1
  4. #define LIN_OUT8 0
  5. #define LIN_OUT 0
  6. #define SCALE 1
  7.  
  8. int fht_max;
  9. int fht_min;
  10. const int ARRSIZE = 48;
  11. const int auxPin0 = 0;
  12. int sampleArr[ARRSIZE];
  13. int lvls[48];
  14. char bars[100];
  15.  
  16. const int btnMode = 3;
  17. const int btnMirror = 4;
  18. const int ledMode = 5;
  19. const int ledMirror = 6;
  20. int curMode = 0;
  21. int prvMode = 0;
  22. int curMirror = 0;
  23. int prvMirror = 0;
  24.  
  25. #include <FHT.h>
  26.  
  27. void setup() {
  28.  
  29. // FFT stuff
  30. //TIMSK0 = 0; // turn off timer0 for lower jitter - delay() and millis() killed
  31. ADCSRA = 0xe5; // set the adc to free running mode
  32. ADMUX = 0x40; // use adc0
  33. DIDR0 = 0x01; // turn off the digital input for adc0
  34.  
  35. Serial.begin(9600);
  36. pinMode(auxPin0, INPUT);
  37. pinMode(btnMode, INPUT);
  38. pinMode(ledMode, OUTPUT);
  39. pinMode(btnMirror, INPUT);
  40. pinMode(ledMirror, OUTPUT);
  41. bars[99] = '/0';
  42.  
  43. // Use Analog reads
  44. myFHT();
  45. normalizeValues();
  46. }
  47.  
  48. void loop() {
  49.  
  50.  
  51. // Mode Button:
  52. // 1: bars growing up
  53. // 2: bars growing from the center
  54.  
  55. int nowMode = digitalRead(btnMode);
  56. if (nowMode == HIGH && prvMode == LOW)
  57. {
  58. curMode = (curMode + 1) & 0x1;
  59. }
  60. prvMode = nowMode;
  61.  
  62. // Mirror Button:
  63. // Left to Right i.e. Lows -> Med -> High
  64. // Right to Left i.e. High -> Med -> Low
  65.  
  66. int nowMirror = digitalRead(btnMirror);
  67. if (nowMirror == HIGH && prvMirror == LOW)
  68. {
  69. curMirror = (curMirror + 1) & 0x1;
  70. }
  71.  
  72. prvMirror = nowMirror;
  73.  
  74.  
  75. switch ((curMode << 1) ^ curMirror)
  76. {
  77. case 0: // Mode = 0, Mirror = 0
  78. lvlsToBars0();
  79. //Serial.print(0);
  80. break;
  81.  
  82. case 1: // Mode = 0, Mirror = 1
  83. lvlsToBars1();
  84. //Serial.print(1);
  85. break;
  86. case 2: // Mode = 1, Mirror = 0
  87. lvlsToBars2();
  88. //Serial.print(2);
  89. break;
  90. case 3: // Mode = 1, Mirror = 1
  91. lvlsToBars3();
  92. //Serial.print(3);
  93. break;
  94.  
  95. default:
  96. break;
  97. }
  98.  
  99. Serial.print(bars);
  100. digitalWrite(ledMode, curMode);
  101. digitalWrite(ledMirror, curMirror);
  102.  
  103.  
  104. // Use Analog reads
  105. myFHT();
  106. normalizeValues();
  107. delay(2000);
  108. }
  109.  
  110. void myFHT()
  111. {
  112.  
  113. //INPUT: sampleARR
  114. //OUTPUT: lvls[48] with values [0..16]
  115.  
  116. cli();
  117. for (int i = 0; i < FHT_N; ++i) {
  118. while(!(ADCSRA & 0x10)); // wait for adc to be ready
  119. ADCSRA = 0xf5; // restart adc
  120. byte m = ADCL; // fetch adc data
  121. //Serial.print(m + "\n");
  122. byte j = ADCH;
  123. int k = (j << 8) | m; // form into an int
  124. k -= 0x0200; // form into a signed int
  125. k <<= 6; // form into a 16b signed int
  126. fht_input[i] = k; // put real data into even bins
  127. }
  128.  
  129. fht_window(); // window the data for better frequency response
  130. fht_reorder(); // reorder the data before doing the fft
  131. fht_run(); // process the data in the fft
  132. fht_mag_log(); // take the output of the fft
  133. sei(); // turn interrupts back on
  134.  
  135. // fft_log_out (has 64 integers)
  136. fht_max = 0;
  137. fht_min = 10000;
  138. for (int i = 8; i < 56; ++i) {
  139. if (fht_log_out[i] > fht_max) {
  140. fht_max = fht_log_out[i];
  141. }
  142. if (fht_log_out[i] < fht_min) {
  143. fht_min = fht_log_out[i];
  144. }
  145.  
  146. lvls[i-8] = fht_log_out[i];
  147. }
  148.  
  149. }
  150.  
  151. void normalizeValues()
  152. {
  153. for(int i = 0; i < 48; ++i)
  154. {
  155. lvls[i] = map(lvls[i],fht_min, fht_max, 0, 16);
  156. }
  157. }
  158.  
  159.  
  160. void lvlsToBars0()
  161. {
  162. bars[99] = '\0';
  163. bars[0] = curMode + 1;
  164. bars[33] = curMode + 1;
  165. bars[66] = curMode + 1;
  166. for(int i = 0; i < 48; ++i)
  167. {
  168. //Serial.print(mapTop(i)); Serial.print(","); Serial.print(mapBot(i)); Serial.write(lvls[i]+'0'); Serial.print("|");
  169. if (lvls[i] > 8)
  170. {
  171. bars[mapTop(i)] = (lvls[i] - 8) + 1;
  172. bars[mapBot(i)] = 9;
  173. }
  174. else
  175. {
  176. bars[mapTop(i)] = 1;
  177. bars[mapBot(i)] = lvls[i] + 1;
  178. }
  179. }
  180. //delay(5000);
  181. }
  182. void lvlsToBars1()
  183. {
  184. bars[99] = '\0';
  185. bars[0] = curMode + 1;
  186. bars[33] = curMode + 1;
  187. bars[66] = curMode + 1;
  188. for(int i = 0; i < 48; ++i)
  189. {
  190. if (lvls[i] > 8)
  191. {
  192. bars[mapTop(47-i)] = (lvls[i] - 8) + 1;
  193. bars[mapBot(47-i)] = 9;
  194. }
  195. else
  196. {
  197. bars[mapTop(47-i)] = 1;
  198. bars[mapBot(47-i)] = lvls[i] + 1;
  199. }
  200. }
  201. }
  202.  
  203. void lvlsToBars2()
  204. {
  205. bars[99] = '\0';
  206. bars[0] = curMode + 1;
  207. bars[33] = curMode + 1;
  208. bars[66] = curMode + 1;
  209. for(int i = 0; i < 48; ++i)
  210. {
  211. lvls[i] = lvls[i] / 2;
  212.  
  213. bars[mapTop(i)] = lvls[i] + 1;
  214. bars[mapBot(i)] = ( 8 - lvls[i] ) % 8 + 1;
  215. }
  216. }
  217.  
  218. void lvlsToBars3()
  219. {
  220. bars[99] = '\0';
  221. bars[0] = curMode + 1;
  222. bars[33] = curMode + 1;
  223. bars[66] = curMode + 1;
  224. for(int i = 0; i < 48; ++i)
  225. {
  226. lvls[i] = lvls[i] / 2;
  227.  
  228. bars[mapTop(47-i)] = lvls[i] + 1;
  229. bars[mapBot(47-i)] = ( 8 - lvls[i] ) % 8 + 1;
  230. }
  231. }
  232.  
  233. int mapTop(int i)
  234. {
  235. return (i+(i / 16)*16)+(i/16+1);
  236. }
  237.  
  238. int mapBot(int i)
  239. {
  240. return (i+(i / 16)*16)+16+(i/16+1);
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement