// the pin we're reading from
int mic_pin = xxx;
// store the noise level / reading from the electret
// TODO: CREATE A INTEGER VARIABLE NAMED noise_level
int xxxxxxx
const int sampleWindow = 50;
// TODO: CREATE A INTEGER VARIABLE NAMED numPins and initialize it to 5
int
int ledPins[ 5 ] = { D0, D1, D2, D3, D4 };
void setup(){
// start serial connection
Serial.begin(9600);
for( int i = 0 ; i < numPins; i++ ){
// TODO: Set the pinmode to output
pinMode(ledPins[ i ], XXXXX);
}
}
void loop()
{
noise_level = sampleNoise( );
// TODO: Write the noise_level to serial
// Hint: Serial.println
displayNoiseOnLEDs();
// TODO: Wait for 50 milliseconds before looping again
XXXXXXX
}
void displayNoiseOnLEDs(){
int level = 200;
for( int i = 0 ; i < numPins; i++ ){
int led = ledPins[ i ];
int desiredLevel = 200 * (i+1);
if( noise_level > desiredLevel ){
// TODO: Write the led to HIGH
}else{
// TODO: Write the led to LOW
}
}
}
int sampleNoise( )
{
unsigned long startMillis = millis(); // Start of sample window
int highest_sample = 0;
int lowest_sample = 1000;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
int sample = analogRead( mic_pin );
// invert the range, and convert it to a percent
sample = map( sample, 0, 4095, 1000, 0 );
// now see if the sample is the lowest;
if ( sample > highest_sample ){
highest_sample = sample ;
}
if ( sample < lowest_sample ){
lowest_sample = sample;
}
}
int peakToPeak = highest_sample - lowest_sample;
return peakToPeak;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .