Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define LED_PIN 2
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812B
- #define NUM_LEDS 144
- #define BRIGHTNESS 200
- #define FRAMES_PER_SECOND 60
- #define MSGEQ7_STROBE_PIN 4
- #define MSGEQ7_RESET_PIN 5
- #define MSGEQ7_LEFT_PIN A1
- #define MSGEQ7_RIGHT_PIN A0
- int filter_min = 100;
- int filter_max = 1016;
- int prev_value;
- int left_value;
- int right_value;
- float lowPass_audio = 0.18;
- uint8_t band;
- int left[7];
- int right[7];
- int mono[7];
- int mapped[7];
- uint8_t zero;
- uint8_t three;
- uint8_t six;
- int HALF_POS = (NUM_LEDS * 0.5) - 1;
- CRGB leds[NUM_LEDS];
- void setup() {
- delay(3000); // sanity delay
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- }
- void loop()
- {
- // Add entropy to random number generator; we use a lot of it.
- // random16_add_entropy( random());
- initializeAudio();
- READ_AUDIO(); // run simulation frame
- radiate();
- }
- void initializeAudio() {
- pinMode(MSGEQ7_LEFT_PIN, INPUT);
- pinMode(MSGEQ7_RIGHT_PIN, INPUT);
- pinMode(MSGEQ7_RESET_PIN, OUTPUT);
- pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
- digitalWrite(MSGEQ7_RESET_PIN, LOW);
- digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
- }
- void READ_AUDIO() {
- float left_volume = 0.0;
- float right_volume = 0.0;
- int mono_volume = 0;
- float mono_factor = 0;
- digitalWrite(MSGEQ7_RESET_PIN, HIGH);
- digitalWrite(MSGEQ7_RESET_PIN, LOW);
- delayMicroseconds(1);
- for (int band = 0; band < 7; band++)
- {
- digitalWrite(MSGEQ7_STROBE_PIN, LOW);
- delayMicroseconds(36);
- prev_value = left[band];
- left_value = analogRead(MSGEQ7_LEFT_PIN);
- left_value = constrain(left_value, filter_min, filter_max);
- left_value = map(left_value, filter_min, filter_max, 0, 255);
- left[band] = prev_value + (left_value - prev_value) * lowPass_audio;
- left_volume += left[band];
- prev_value = right[band];
- right_value = analogRead(MSGEQ7_RIGHT_PIN);
- right_value = constrain(right_value, filter_min, filter_max);
- right_value = map(right_value, filter_min, filter_max, 0, 255);
- right[band] = prev_value + (right_value - prev_value) * lowPass_audio;
- right_volume += right[band];
- digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
- delayMicroseconds(1);
- mono[band] = (left[band] + right[band]) * 0.5;
- mono_volume += mono[band];
- }
- mono_factor = float(HALF_POS) / mono_volume;
- for (int band = 0; band < 7; band++)
- {
- mapped[band] = mono[band] * mono_factor;
- }
- }
- void radiate() {
- zero = mono[0];
- three = mono[4];
- six = mono[6];
- leds[HALF_POS] = CRGB(zero, three, six);
- // high frequencies = blue = FAST SPEED
- EVERY_N_MILLISECONDS(11) {
- for (int i = NUM_LEDS; i > HALF_POS; i--) {
- leds[i].blue = leds[i - 1].blue;
- }
- }
- // mid frequencies = green = MEDIUM SPEED
- EVERY_N_MILLISECONDS(30) {
- for (int i = NUM_LEDS; i > HALF_POS; i--) {
- leds[i].green = leds[i - 1].green;
- }
- }
- // low frequencies = red = SLOW SPEED
- EVERY_N_MILLISECONDS(58) {
- for (int i = NUM_LEDS; i > HALF_POS; i--) {
- leds[i].red = leds[i - 1].red;
- }
- }
- // for mirroring to the other half of the strip
- for (int i = NUM_LEDS; i > HALF_POS; i--) {
- leds[HALF_POS - (i - HALF_POS)] = leds[i - 1];
- }
- FastLED.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment