Last active
May 11, 2021 20:04
-
-
Save systembolaget/18f6f1d625e9d8c87b1440603c4a3920 to your computer and use it in GitHub Desktop.
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 "FastLED.h" | |
const byte pinData = 6; | |
const byte pinSEN0232 = A0; | |
const byte pinPotentiometerMin = A1; | |
const byte pinPotentiometerMax = A2; | |
const float EMA_a = 0.6; | |
const byte ledCount = 24; | |
const byte ledBrightness = 192; | |
struct CRGB ledRing[ledCount]; | |
struct CRGB ledGradient[ledCount]; | |
CHSV gradientHueStart = CHSV(96, 255, 100); | |
CHSV grandientHueEnd = CHSV(0, 255, 100); | |
long timePeakHold = 50; | |
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; | |
timeLastPeak = millis(); | |
} | |
FastLED.clear(); | |
for ( byte i = 0; i <= ledPeak; i++) | |
{ | |
ledRing[i] = ledGradient[i]; | |
} | |
ledRing[ledPeakPrevious] = CHSV(0, 255, 255); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment