Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1.  
  2. #include "arduinoFFT.h"
  3. #include "FastLED.h"
  4.  
  5. #define LED_PIN 5
  6. #define NUM_LEDS 60
  7. #define BRIGHTNESS 64
  8. #define LED_TYPE WS2811
  9. #define COLOR_ORDER GRB
  10. #define tunPin 1
  11. CRGB leds[NUM_LEDS];
  12.  
  13. #define UPDATES_PER_SECOND 1
  14.  
  15. CRGBPalette16 currentPalette;
  16. TBlendType currentBlending;
  17.  
  18. extern CRGBPalette16 myRedWhiteBluePalette;
  19. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  20.  
  21.  
  22.  
  23. #define SAMPLES 128 //Must be a power of 2
  24. #define SAMPLING_FREQUENCY 4000 //Hz, must be less than 10000 due to ADC
  25. //default: samples 128
  26.  
  27.  
  28. arduinoFFT FFT = arduinoFFT();
  29.  
  30. unsigned int sampling_period_us;
  31. unsigned long microseconds;
  32. double tunVal = 0;
  33.  
  34. double vReal[SAMPLES];
  35. double vImag[SAMPLES];
  36.  
  37. int flag = 0;
  38. int count = 0;
  39.  
  40. void setup() {
  41. Serial.begin(115200);
  42. pinMode(LED_BUILTIN, OUTPUT);
  43.  
  44. delay(1000); // power-up safety delay
  45. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  46. FastLED.setBrightness(BRIGHTNESS);
  47.  
  48. currentBlending = LINEARBLEND;
  49. for (int i = 0; i < NUM_LEDS; i++) {
  50. leds[i] = CRGB(0, 0, 0);
  51. }
  52. FastLED.show();
  53.  
  54. sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
  55.  
  56. pinMode(tunPin, INPUT); // declare the tunPin as an INPUT
  57. /*SAMPLING*/
  58. for (int i = 0; i<SAMPLES; i++)
  59. {
  60. microseconds = micros(); //Overflows after around 70 minutes!
  61.  
  62. vReal[i] = analogRead(0);
  63. vImag[i] = 0;
  64.  
  65. while (micros() < (microseconds + sampling_period_us)) {
  66. }
  67. }
  68.  
  69. /*FFT*/
  70. FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  71. FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  72. FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
  73. double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
  74.  
  75. for (int i = 0; i<(SAMPLES / 2); i++)
  76. {
  77. /*View all these three lines in serial terminal to see which frequencies has which amplitudes*/
  78. /*
  79. Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
  80. Serial.print(" \t");
  81. Serial.print("i = ");
  82. Serial.print(i);
  83. Serial.print(" \t");
  84. Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins */
  85. }
  86. }
  87.  
  88. void loop() {
  89.  
  90. tunVal = analogRead(tunPin);
  91. tunVal = map(tunVal, 0, 1023, 0, 100);
  92. tunVal = tunVal / 100;
  93. /*Serial.print("TunVal: ");
  94. Serial.println(tunVal);*/
  95.  
  96. /*SAMPLING*/
  97. for (int i = 0; i<SAMPLES; i++)
  98. {
  99. microseconds = micros(); //Overflows after around 70 minutes!
  100.  
  101. vReal[i] = analogRead(0)*tunVal;
  102. vImag[i] = 0;
  103.  
  104.  
  105. while (micros() < (microseconds + sampling_period_us)) {
  106. }
  107. }
  108.  
  109. /*FFT*/
  110. FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  111. FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  112. FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
  113. double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
  114.  
  115. /*PRINT RESULTS*/
  116. //Serial.println(peak); //Print out what frequency is the most dominant.
  117.  
  118. int led_state = LOW;
  119.  
  120. //blueBass(led_state, vReal, tunVal); // Regular FFT blue bass
  121. //anternatingGreen(led_state, vReal, tunVal);
  122. redShift(led_state, vReal, tunVal);
  123. Serial.println(count);
  124. FastLED.show();
  125. }
  126.  
  127. void blueBass(int led_state, double vReal[], double tunVal) {
  128. float snit = 0, snit_if = 0, snit_ifif = 0;
  129. float num_var = 0, lay_still = 0;
  130. for (int i = 3; i <= 5; i++) {
  131. snit += vReal[i];
  132. num_var++;
  133. }
  134. snit = snit / num_var;
  135. for (int i = 0; i < NUM_LEDS; i++) {
  136. if (snit > 50) {
  137. snit_if = snit * 0.35;
  138. }
  139. if (snit > 100 / tunVal) {
  140. snit_ifif = snit * 0.5;
  141. }
  142. leds[i] = CRGB(snit_if, snit_ifif, snit);
  143. }
  144. }
  145.  
  146. void anternatingGreen(int led_state, double vReal[], double tunVal) {
  147. float snit = 0, snit_if = 0, snit_ifif = 0;
  148. float num_var = 0, lay_still = 0;
  149. for (int i = 3; i <= 5; i++) {
  150. snit += vReal[i];
  151. num_var++;
  152. }
  153. snit = snit / num_var;
  154. switch (flag) {
  155. case 0:
  156. for (int i = 0; i < NUM_LEDS / 2; i++) {
  157. if (snit > 50) {
  158. snit_if = snit * 0.8;
  159. flag = 1;
  160. }
  161. leds[i] = CRGB(0, snit, snit_if);
  162. }
  163. if (flag == 1) {
  164. for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
  165. leds[i] = CRGB(0, 10, 0);
  166. delay(5);
  167. FastLED.show();
  168. }
  169.  
  170. for (int i = 0 ; i < NUM_LEDS/2; i++) {
  171. leds[i] = CRGB(0, 0, 0);
  172. }
  173. }
  174.  
  175. break;
  176. case 1:
  177. for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
  178. if (snit > 50) {
  179. snit_if = snit * 0.8;
  180. flag = 0;
  181. }
  182. leds[i] = CRGB(0, snit, snit_if);
  183. }
  184. if(flag==0){
  185. for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
  186. leds[i] = CRGB(0, 10, 0);
  187. delay(5);
  188. FastLED.show();
  189. }
  190.  
  191.  
  192. for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
  193. leds[i] = CRGB(0, 0, 0);
  194. }
  195. }
  196. break;
  197. }
  198. }
  199.  
  200. void redShift(int led_state, double vReal[], double tunVal) {
  201. float snit = 0, snit_if = 0, snit_ifif = 0;
  202. float num_var = 0, lay_still = 0;
  203. for (int i = 3; i <= 5; i++) {
  204. snit += vReal[i];
  205. num_var++;
  206. }
  207. snit = snit / num_var;
  208. switch (flag) {
  209. case 0:
  210. for (int i = 0; i < NUM_LEDS; i=i+2) {
  211. if (snit > 50) {
  212. snit_if = snit * 0.8;
  213. flag = 1;
  214. count++;
  215. }
  216. leds[i] = CRGB(snit, 0, 0);
  217.  
  218. for (int i = 1; i < NUM_LEDS; i = i + 2) {
  219. leds[i] = CRGB(0, 0, 0);
  220. }
  221. }
  222.  
  223. break;
  224. case 1:
  225. for (int i = 1; i < NUM_LEDS; i = i + 2) {
  226. if (snit > 50) {
  227. snit_if = snit * 0.8;
  228. flag = 0;
  229. count++;
  230. }
  231. leds[i] = CRGB(snit, 0, 0);
  232. for (int i = 0; i < NUM_LEDS; i = i + 2) {
  233. leds[i] = CRGB(0, 0, 0);
  234. }
  235. }
  236. break;
  237. }
  238. if (count > 1000*4) {
  239. while (count > 0) {
  240. for (int i = 0; i < NUM_LEDS; i++) {
  241. leds[i] = CRGB(120, 120, 120);
  242. }
  243. FastLED.show();
  244. delay(30);
  245. for (int i = 0; i < NUM_LEDS; i++) {
  246. leds[i] = CRGB(0, 0, 0);
  247. }
  248. FastLED.show();
  249. delay(30);
  250. count = count - 80;
  251. }
  252.  
  253. }
  254. }
  255.  
  256. double smooth(double prev, double current) {
  257. if (prev > current) {
  258.  
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement