Created
March 14, 2018 01:43
-
-
Save kmicheli/15c530822cc78ef335fd0fbd77787c2f to your computer and use it in GitHub Desktop.
This file contains 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
/* This code has been adapted from the | |
* Example Sound Level Sketch for the | |
* Adafruit Microphone Amplifier | |
*/ | |
#include <Adafruit_NeoPixel.h> | |
#define PIN 6 | |
#define NUM_LIGHTS 25 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); | |
const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz) | |
unsigned int knock; | |
int ledPin = 6; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() | |
{ | |
uint32_t low = strip.Color(0, 0, 0); | |
uint32_t high = strip.Color(255, 255, 255); | |
unsigned long start= millis(); // Start of sample window | |
unsigned int peakToPeak = 0; // peak-to-peak level | |
unsigned int signalMax = 0; | |
unsigned int signalMin = 1024; | |
// collect data for 250 miliseconds | |
while (millis() - start < sampleWindow) | |
{ | |
knock = analogRead(0); | |
if (knock < 1024) //This is the max of the 10-bit ADC so this loop will include all readings | |
{ | |
if (knock > signalMax) | |
{ | |
signalMax = knock; // save just the max levels | |
} | |
else if (knock < signalMin) | |
{ | |
signalMin = knock; // save just the min levels | |
} | |
} | |
} | |
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude | |
double volts = (peakToPeak * 3.3) / 1024; // convert to volts | |
Serial.println(volts); | |
if (volts >=1.0) | |
{ | |
//turn on LED | |
for( int i = 0; i<NUM_LIGHTS; i++){ | |
strip.setPixelColor(i, high); | |
strip.show(); | |
} | |
delay(100); | |
Serial.println("Knock Knock"); | |
} | |
else | |
{ | |
for( int i = 0; i<NUM_LIGHTS; i++){ | |
strip.setPixelColor(i, low); | |
strip.show(); | |
} | |
delay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment