Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save systembolaget/3d9369888556c97ee8210caa777ac69e to your computer and use it in GitHub Desktop.
Save systembolaget/3d9369888556c97ee8210caa777ac69e to your computer and use it in GitHub Desktop.
Decibel_meter_peak_hold_and_drop
#include "FastLED.h"
const byte pinData = 6;
const byte pinSEN0232 = A0;
const byte pinPotentiometerMin = A1;
const byte pinPotentiometerMax = A2;
const float EMA_a = 0.7; // Lower is more laggy but smoother
const byte ledCount = 24;
const byte ledBrightness = 192;
struct CRGB ledRing[ledCount];
struct CRGB ledGradient[ledCount];
CHSV gradientHueStart = CHSV(96, 255, 64); // Darker at the bottom
CHSV grandientHueEnd = CHSV(0, 255, 192);
const int intervalPeakHold = 900;
const int intervalPeakDecay = 30;
long dBValue = 0;
int dBValueEMA = 0;
byte dBMin = 0;
byte dBMax = 0;
int newPeak = 0;
int previousPeak = 0;
unsigned long timePeak = 0;
byte brightnessPeak = 192;
bool decay = false;
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 (newPeak >= previousPeak)
{
previousPeak = newPeak;
timePeak = millis();
brightnessPeak = 192;
decay = false;
}
else if (!decay && (millis() - timePeak >= intervalPeakHold))
{
timePeak += intervalPeakHold - intervalPeakDecay;
decay = true;
}
else if (decay && (millis() - timePeak > intervalPeakDecay))
{
if (previousPeak > 0)
{
previousPeak --;
if (brightnessPeak <= 0)
{
brightnessPeak = 0;
}
else
{
brightnessPeak -= 16;
}
timePeak += intervalPeakDecay;
}
}
FastLED.clear();
for ( byte i = 0; i <= newPeak; i++)
{
ledRing[i] = ledGradient[i];
}
ledRing[previousPeak] = CHSV(0, 255, brightnessPeak);
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;
newPeak = 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment