Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "arduinoFFT.h"
- #include "FastLED.h"
- #define LED_PIN 5
- #define NUM_LEDS 60
- #define BRIGHTNESS 64
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- #define tunPin 1
- CRGB leds[NUM_LEDS];
- #define UPDATES_PER_SECOND 1
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- extern CRGBPalette16 myRedWhiteBluePalette;
- extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
- #define SAMPLES 128 //Must be a power of 2
- #define SAMPLING_FREQUENCY 4000 //Hz, must be less than 10000 due to ADC
- //default: samples 128
- arduinoFFT FFT = arduinoFFT();
- unsigned int sampling_period_us;
- unsigned long microseconds;
- double tunVal = 0;
- double vReal[SAMPLES];
- double vImag[SAMPLES];
- int flag = 0;
- int count = 0;
- void setup() {
- Serial.begin(115200);
- pinMode(LED_BUILTIN, OUTPUT);
- delay(1000); // power-up safety delay
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- currentBlending = LINEARBLEND;
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(0, 0, 0);
- }
- FastLED.show();
- sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
- pinMode(tunPin, INPUT); // declare the tunPin as an INPUT
- /*SAMPLING*/
- for (int i = 0; i<SAMPLES; i++)
- {
- microseconds = micros(); //Overflows after around 70 minutes!
- vReal[i] = analogRead(0);
- vImag[i] = 0;
- while (micros() < (microseconds + sampling_period_us)) {
- }
- }
- /*FFT*/
- FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
- FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
- FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
- double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
- for (int i = 0; i<(SAMPLES / 2); i++)
- {
- /*View all these three lines in serial terminal to see which frequencies has which amplitudes*/
- /*
- Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
- Serial.print(" \t");
- Serial.print("i = ");
- Serial.print(i);
- Serial.print(" \t");
- Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins */
- }
- }
- void loop() {
- tunVal = analogRead(tunPin);
- tunVal = map(tunVal, 0, 1023, 0, 100);
- tunVal = tunVal / 100;
- /*Serial.print("TunVal: ");
- Serial.println(tunVal);*/
- /*SAMPLING*/
- for (int i = 0; i<SAMPLES; i++)
- {
- microseconds = micros(); //Overflows after around 70 minutes!
- vReal[i] = analogRead(0)*tunVal;
- vImag[i] = 0;
- while (micros() < (microseconds + sampling_period_us)) {
- }
- }
- /*FFT*/
- FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
- FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
- FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
- double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
- /*PRINT RESULTS*/
- //Serial.println(peak); //Print out what frequency is the most dominant.
- int led_state = LOW;
- //blueBass(led_state, vReal, tunVal); // Regular FFT blue bass
- //anternatingGreen(led_state, vReal, tunVal);
- redShift(led_state, vReal, tunVal);
- Serial.println(count);
- FastLED.show();
- }
- void blueBass(int led_state, double vReal[], double tunVal) {
- float snit = 0, snit_if = 0, snit_ifif = 0;
- float num_var = 0, lay_still = 0;
- for (int i = 3; i <= 5; i++) {
- snit += vReal[i];
- num_var++;
- }
- snit = snit / num_var;
- for (int i = 0; i < NUM_LEDS; i++) {
- if (snit > 50) {
- snit_if = snit * 0.35;
- }
- if (snit > 100 / tunVal) {
- snit_ifif = snit * 0.5;
- }
- leds[i] = CRGB(snit_if, snit_ifif, snit);
- }
- }
- void anternatingGreen(int led_state, double vReal[], double tunVal) {
- float snit = 0, snit_if = 0, snit_ifif = 0;
- float num_var = 0, lay_still = 0;
- for (int i = 3; i <= 5; i++) {
- snit += vReal[i];
- num_var++;
- }
- snit = snit / num_var;
- switch (flag) {
- case 0:
- for (int i = 0; i < NUM_LEDS / 2; i++) {
- if (snit > 50) {
- snit_if = snit * 0.8;
- flag = 1;
- }
- leds[i] = CRGB(0, snit, snit_if);
- }
- if (flag == 1) {
- for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
- leds[i] = CRGB(0, 10, 0);
- delay(5);
- FastLED.show();
- }
- for (int i = 0 ; i < NUM_LEDS/2; i++) {
- leds[i] = CRGB(0, 0, 0);
- }
- }
- break;
- case 1:
- for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
- if (snit > 50) {
- snit_if = snit * 0.8;
- flag = 0;
- }
- leds[i] = CRGB(0, snit, snit_if);
- }
- if(flag==0){
- for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
- leds[i] = CRGB(0, 10, 0);
- delay(5);
- FastLED.show();
- }
- for (int i = NUM_LEDS / 2; i < NUM_LEDS; i++) {
- leds[i] = CRGB(0, 0, 0);
- }
- }
- break;
- }
- }
- void redShift(int led_state, double vReal[], double tunVal) {
- float snit = 0, snit_if = 0, snit_ifif = 0;
- float num_var = 0, lay_still = 0;
- for (int i = 3; i <= 5; i++) {
- snit += vReal[i];
- num_var++;
- }
- snit = snit / num_var;
- switch (flag) {
- case 0:
- for (int i = 0; i < NUM_LEDS; i=i+2) {
- if (snit > 50) {
- snit_if = snit * 0.8;
- flag = 1;
- count++;
- }
- leds[i] = CRGB(snit, 0, 0);
- for (int i = 1; i < NUM_LEDS; i = i + 2) {
- leds[i] = CRGB(0, 0, 0);
- }
- }
- break;
- case 1:
- for (int i = 1; i < NUM_LEDS; i = i + 2) {
- if (snit > 50) {
- snit_if = snit * 0.8;
- flag = 0;
- count++;
- }
- leds[i] = CRGB(snit, 0, 0);
- for (int i = 0; i < NUM_LEDS; i = i + 2) {
- leds[i] = CRGB(0, 0, 0);
- }
- }
- break;
- }
- if (count > 1000*4) {
- while (count > 0) {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(120, 120, 120);
- }
- FastLED.show();
- delay(30);
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(0, 0, 0);
- }
- FastLED.show();
- delay(30);
- count = count - 80;
- }
- }
- }
- double smooth(double prev, double current) {
- if (prev > current) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement