Skip to content

Instantly share code, notes, and snippets.

@damontgomery
Created April 24, 2018 18:03
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 damontgomery/4783158b70f5027d179c8eed7c0b05f5 to your computer and use it in GitHub Desktop.
Save damontgomery/4783158b70f5027d179c8eed7c0b05f5 to your computer and use it in GitHub Desktop.
Speaker Booster
#include <IRLibSendBase.h>
#include <IRLib_P01_NEC.h>
#include <IRLibCombo.h>
IRsend mySender;
// Configuration.
int micPin = 0;
int micVolumeThreshold = 3;
int sampleDelay = 50;
int signalDelay = 250;
bool speakerBoosted = false;
int micHistoryCount = 200;
int micHistoryIndex = 0;
// Raw value history.
int micRawHistory [200];
int micRawInitialValue = 732;
long micRawTotal = long(micHistoryCount) * micRawInitialValue;
int micRawAverage;
// Volume history.
int micVolumeHistory [200];
long micVolumeTotal = 0;
int micVolumeAverage;
int micWarmup = 0;
int micWarmupThreshold = 2;
bool micWarmed = false;
void setup() {
pinMode(micPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
analogReference(INTERNAL);
Serial.begin(9600);
// Initialize histories.
for (int i = 0; i < micHistoryCount; i++) {
micRawHistory[i] = micRawInitialValue;
micVolumeHistory[i] = 0;
}
}
void loop() {
int micRaw;
micRaw = analogRead(micPin);
// Initial readings are bad, so throw them out.
if (micWarmed == false) {
micWarmup++;
if (micWarmup == micWarmupThreshold) {
micWarmed = true;
}
delay(sampleDelay);
return;
}
// Record new raw values.
micRawTotal = micRawTotal - micRawHistory[micHistoryIndex] + micRaw;
micRawHistory[micHistoryIndex] = micRaw;
micRawAverage = micRawTotal / micHistoryCount;
// Calculate volume.
int volume = 0;
int micVolume = int(abs(micRawAverage - micRaw));
if (micVolume >= micVolumeThreshold) {
volume = micVolume;
}
// Record new volume values.
micVolumeTotal = micVolumeTotal - micVolumeHistory[micHistoryIndex] + volume;
micVolumeHistory[micHistoryIndex] = volume;
micVolumeAverage = micVolumeTotal / micHistoryCount;
// Find the max volume in the history.
int maxVolumeInHistory = 0;
// Initialize histories.
for (int i = 0; i < micHistoryCount; i++) {
maxVolumeInHistory = max(maxVolumeInHistory, micVolumeHistory[i]);
}
// Debug print to serial monitor.
Serial.println(maxVolumeInHistory);
// light the LED when the volume is "high".
if (maxVolumeInHistory >= micVolumeThreshold) {
if (speakerBoosted == false) {
digitalWrite(LED_BUILTIN, HIGH);
// Boost speaker.
speakerBoosted = true;
mySender.send(NEC,0xff827d, 0);
delay(signalDelay);
mySender.send(NEC,0xff827d, 0);
}
}
else {
if (speakerBoosted == true) {
digitalWrite(LED_BUILTIN, LOW);
// Lower speaker.
speakerBoosted = false;
mySender.send(NEC,0xffa25d, 0);
delay(signalDelay);
mySender.send(NEC,0xffa25d, 0);
}
}
// Increment counter.
micHistoryIndex++;
if (micHistoryIndex == micHistoryCount) {
micHistoryIndex = 0;
}
delay(sampleDelay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment