Sound Reactive Hat Code

/****************************************
Example Sound Level Sketch for the 
Adafruit Microphone Amplifier
modified for GEMMA sequin masquerade mask
****************************************/
//Modifications by Julie Barrett for the sound reactive hat project

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int led = 1;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is

void setup() 
{
   //Serial.begin(9600);
     pinMode(led, OUTPUT);
}


void loop() 
{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(1);
      //Serial.println(sample);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = (signalMax - signalMin)6;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 3.3) / 1024;  // convert to volts
    int brightness = map(peakToPeak, 0, 1023, 0, 255);
    analogWrite(led, brightness);
   //Serial.println(volts);
}

Filed under: Code            
10/19/2020 12:26:00 PM