I am using a pre-made decibel meter with a 24 RGB LED Adafruit Neopixel ring and FastLED.
The code works, but the peak hold LED flickers with the value stored in timePeakHold.
Also the peak hold RGB LED drops quickly and the whole thing could react smoother, but maybe that depends on the noise input to the decibel meter's microphone (I am making a noise reactive device for an underground station), and there are only 24 RGB LEDs in the ring.
Any ideas how to solve the first problem (and maybe the two secondary problems) would be much appreciated.
ps: If needed, I could make a short video and put it in my Dropbox for a while.
#include "FastLED.h"
const byte pinData = 6;
const byte pinSEN0232 = A0;
const byte pinPotentiometerMin = A1;
const byte pinPotentiometerMax = A2;
const float EMA_a = 0.4;
const byte ledCount = 24;
const byte ledBrightness = 128;
struct CRGB ledRing[ledCount];
struct CRGB ledGradient[ledCount];
CHSV gradientHueStart = CHSV(96, 255, 100);
CHSV grandientHueEnd = CHSV(0, 255, 100);
long timePeakHold = 500;
long dBValue = 0;
int dBValueEMA = 0;
byte dBMin = 0;
byte dBMax = 0;
int ledPeak = 0;
int ledPeakPrevious = 0;
long timeLastPeak = 0;
void setup()
{
FastLED.addLeds<NEOPIXEL, pinData>(ledRing, ledCount);
FastLED.setBrightness(ledBrightness);
fill_gradient(ledGradient, 0, gradientHueStart, 23, grandientHueEnd, SHORTEST_HUES);
}
void loop()
{
readSEN0232();
readPotentiometers();
if (ledPeak > ledPeakPrevious)
{
ledPeakPrevious = ledPeak;
timeLastPeak = millis();
}
if (millis() > (timeLastPeak + timePeakHold))
{
ledPeakPrevious = ledPeakPrevious - 1;
}
FastLED.clear();
for ( byte i = 0; i <= ledPeak; i++)
{
ledRing[i] = ledGradient[i];
}
ledRing[ledPeakPrevious] = ledGradient[ledCount - 1];
FastLED.show();
}
void readSEN0232()
{
float voltageValue;
voltageValue = analogRead(pinSEN0232) / 1024.0 * 5.0;
dBValue = voltageValue * 50.0;
dBValueEMA = int ((EMA_a * dBValue) + ((1 - EMA_a) * dBValueEMA)) + 0.5;
ledPeak = constrain(map(dBValueEMA, dBMin, dBMax, 0, 23), 0, 23);
}
void readPotentiometers()
{
dBMin = constrain(map(analogRead(pinPotentiometerMin), 0, 1023, 35, 60), 35, 60);
dBMax = constrain(map(analogRead(pinPotentiometerMax), 0, 1023, 60, 130), 60, 130);
}