Created
June 5, 2021 09:51
-
-
Save systembolaget/3b06509f9fdb19f3c29bd6501496a394 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 <Wire.h> | |
#include "FastLED.h" | |
const byte pinData = 3; | |
const byte pinSEN0232 = A0; | |
const byte pinPotentiometerMin = A1; | |
const byte pinPotentiometerMax = A2; | |
const byte ledCount = 24; | |
const byte ledBrightness = 192; | |
struct CRGB ledRing[ledCount]; | |
struct CRGB ledGradient[ledCount]; | |
const int intervalPeakHold = 900; | |
const int intervalPeakDecay = 30; | |
const float EMA_a = 0.9; | |
const byte addressSlave = 7; // Added for I2C | |
CHSV gradientHueStart = CHSV(96, 255, 96); | |
CHSV grandientHueEnd = CHSV(0, 255, 192); | |
unsigned long timeSEN0232 = 0; | |
long dBValue = 0; | |
int dBValueEMA = 0; | |
byte dBMin = 0; | |
byte dBMax = 0; | |
int newPeak = 0; | |
int previousPeak = 0; | |
unsigned long timePeak = 0; | |
byte brightnessPeak = 0; | |
bool decay = false; | |
void setup() | |
{ | |
Wire.begin(); // Added for I2C | |
Serial.begin(115200); // Added for I2C | |
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 = ledBrightness; | |
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; | |
} | |
// | |
Wire.beginTransmission(addressSlave); // Added for I2C | |
Wire.write(dBValueEMA); // Added for I2C | |
Wire.endTransmission(); // Added for I2C | |
} | |
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); | |
Serial.println(dBValueEMA); // Added for I2C | |
} | |
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