/* ------------------------------------------
 *  Using the Circuit Playground sound sensor
 */
//-------------------------------------------
// libraries to include:
#include <Adafruit_CircuitPlayground.h>

int ledPin = 13;  // this is what 'LED_BUILTIN' is 
int soundLevel;

// the setup function runs once when you press reset or power the board
void setup() {
  CircuitPlayground.begin();  // initialize Circuit Playground
  
  // initialize digital pin with LED as an output
  pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  soundLevel = CircuitPlayground.mic.soundPressureLevel(10);
//-- Uncomment the next 2 lines after you know the range (low-high) of your sensor
   soundLevel = constrain(soundLevel, 52, 95);
   soundLevel = map(soundLevel, 52, 95, 0, 255); // Scale data to 0-255
   Serial.println(soundLevel);  //-- Sends ASCII
//   Serial.write(soundLevel);  //-- Alternatively, send numbers (not both)
      
  if(soundLevel > 60){
    digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
    } else {                      // wait for a second
     digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW

  delay(100);        
   }
}
