Created
May 12, 2017 14:59
-
-
Save lvidarte/320bb89aaf40efb95041dee59119056c to your computer and use it in GitHub Desktop.
VU Meter with NodeMCU and KY-037
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Adafruit_NeoPixel.h> | |
#define NUM_PIXELS 10 | |
#define PIN_PIXELS 14 | |
#define AUDIO_ANALOG A0 | |
#define AUDIO_DIGITAL 12 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIN_PIXELS, NEO_GRB + NEO_KHZ800); | |
const int yellowTopLED = (1024 * .4) * NUM_PIXELS / 1024; // 40% LEDs | |
const int greenTopLED = (1024 * .8) * NUM_PIXELS / 1024; // 80% LEDs | |
void setup () | |
{ | |
Serial.begin(115200); | |
pinMode(PIN_PIXELS, OUTPUT); | |
pinMode(AUDIO_ANALOG, INPUT); | |
pinMode(AUDIO_DIGITAL, INPUT); | |
pixels.setBrightness(64); | |
} | |
void loop () | |
{ | |
int i; | |
int sensorValue = analogRead(AUDIO_ANALOG); | |
int leds = sensorValue / (1024 / NUM_PIXELS); | |
//Serial.printf("sensor %4d, leds %2d\n", sensorValue, leds); | |
// turn on leds | |
for (i = 0; i < leds; i++) | |
{ | |
if (i < yellowTopLED) // yellow | |
pixels.setPixelColor(i, pixels.Color(255, 255, 0)); | |
else if (i < greenTopLED) // green | |
pixels.setPixelColor(i, pixels.Color(0, 255, 0)); | |
else // red | |
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); | |
pixels.show(); | |
} | |
// turn off all leds | |
for(i = NUM_PIXELS - 1; i > 0; i--) | |
{ | |
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); | |
pixels.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment