Hi, I'm trying to make a vu meter. I found the Adafuit LED Ampli-Tie project and got it working with my dotstar. I would like to use the FastLED library instead but I'm having trouble converting the code. Is this possible or do I have to start from scratch if I want to use fastLED library?
I've looked up other fastLED vu meter code on Github and the Ampli-Tie is still the options/features that I am looking for(mostly falling dot/ underlying color gradient or animations). If you have a better or cooler looking fastLED vu meter code please share it with me too.
I commented the original code and wrote what I think fastLED code should be underneath it. Please let me know what I'm doing wrong or if there is a way i can do it.
Thank you so much.
#include <FastLED.h>
#include <math.h>
#define N_PIXELS 72
#define MIC_PIN A5
#define DATAPIN 6
#define CLOCKPIN 7
#define SAMPLE_WINDOW 2
#define PEAK_HANG 5
#define PEAK_FALL 1
#define INPUT_FLOOR 270
#define INPUT_CEILING 450
struct CRGB leds[N_PIXELS];
byte peak = 16;
unsigned int sample;
byte dotCount = 0;
byte dotHangCount = 0;
void setup()
{
delay(1000);
FastLED.addLeds<DOTSTAR, DATAPIN, CLOCKPIN, BGR> (leds, N_PIXELS);
FastLED.setBrightness(5);
FastLED.show();
}
void loop()
{
unsigned long startMillis= millis();
float peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1023;
unsigned int c, y;
while (millis() - startMillis < SAMPLE_WINDOW)
{
sample = analogRead(MIC_PIN);
if (sample < 1024)
{
if (sample > signalMax)
{
signalMax = sample;
}
else if (sample < signalMin)
{
signalMin = sample;
}
}
}
peakToPeak = signalMax - signalMin;
// for (int i=0;i<=strip.numPixels()-1;i++){
// strip.setPixelColor(i,Wheel(map(i,0,strip.numPixels()-1,30,150)));
// }
for (int i=0;i<=N_PIXELS -1;i++){
leds[i] = CHSV(map(i,0,N_PIXELS-1, 30, 150));
}
// c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);
c = fscale(INPUT_FLOOR, INPUT_CEILING, N_PIXELS, 0, peakToPeak, 2);
if(c < peak) {
peak = c;
dotHangCount = 0;
}
// if (c <= strip.numPixels()) {
// drawLine(strip.numPixels(), strip.numPixels()-c, strip.Color(0, 0, 0));
// }
if (c <= N_PIXELS) {
drawLine(N_PIXELS, N_PIXELS - c, 0);
}
// y = strip.numPixels() - peak;
y = N_PIXELS - peak;
//strip.setPixelColor(y-1,Wheel(map(y,0,strip.numPixels()-1,30,150)));
y-1 = CHSV(map(y,0,N_PIXELS - 1,30,150));
FastLED.show();
if(dotHangCount > PEAK_HANG) {
if(++dotCount >= PEAK_FALL) {
peak++;
dotCount = 0;
}
}
else {
dotHangCount++;
}
}
void drawLine(uint8_t from, uint8_t to, uint32_t c) {
uint8_t fromTemp;
if (from > to) {
fromTemp = from;
from = to;
to = fromTemp;
}
// for(int i=from; i<=to; i++){
// strip.setPixelColor(i, c);
// }
for(int i=from; i<=to; i++){
leds[i] = c;
}
}
float fscale( float originalMin, float originalMax, float newBegin, float
newEnd, float inputValue, float curve){
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ;
curve = pow(10, curve);
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin){
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange;
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0){
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}