Breann MitchellBrian TaitJosh Skawski
Published

LoT Sound Detector Data Collection

This sound sensor allows you to measure the minimum average, the maximum average, and the overall average values of sound over a timeframe.

BeginnerFull instructions provided1 hour972
LoT Sound Detector Data Collection

Things used in this project

Hardware components

Photon
Particle Photon
×1
Breadboard (generic)
Breadboard (generic)
×1
SparkFun Sound Detector
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Sound Enclosure Caseplans for Group 401

Protects wiring with opening for power source and sound sensor to stick out to detect sound

Schematics

Fritzing Diagram for Sound Detector

Code

LoT Sound Detector

Arduino
This code allows you to get a reading on the minimum, maximum and average amplitude values.
const long sample_rate = 50; // time between samples (in miliseconds)
const int array_size = 1200; // 1000/50=20 * 60=1200
int snd_array[array_size] = {};
int snd_max, prev_max = 0;
int snd_min, prev_min = 4096;
double snd_avg = 2048;

const int blink_thresh = 2048;

unsigned long broadcast_interval = 3000;
unsigned long last_broadcast = 0;

void averageReading(int value) {
    // Shift all the values right by 1
    for(int i = array_size-1; i >= 1; i--) 
    {
        snd_array[i] = snd_array[i-1]; 
        if((snd_array[i] < snd_min) && (snd_array[i] != 0))
        {
            snd_min = snd_array[i];
            
        }
        if(snd_array[i] > snd_max)
        {
            snd_max = snd_array[i];
            
        }
    }
   Particle.variable("snd_min", snd_min); 
   Particle.variable("snd_max", snd_max); 
    
    snd_array[0] = value; 

    // Average array
    float avg_sum = 0; 
    int size = 0 ;
    for (int a=0; a <= array_size; a++) 
    {
        if(snd_array[a] > 0)
        {
            size++ ;
            avg_sum  = avg_sum + snd_array[a];
        }
    }
    snd_avg = avg_sum / size;
    
   Particle.variable("snd_avg", snd_avg); //pull to google sheets

}

void blinkMic(int reading) {
    if(reading > blink_thresh) {
        digitalWrite(D7, HIGH);
    } else {
        digitalWrite(D7, LOW);
    }
}


void checkBroadcast() {
    unsigned long now = millis();
    if((now - last_broadcast) > broadcast_interval) {
        Serial.print("Avg: "); Serial.println(snd_avg);
        Serial.print("Min: "); Serial.println(snd_min);
        Serial.print("Max: "); Serial.println(snd_max);
        snd_avg = 0;
        snd_min = 4096;
        snd_max = 0;
        snd_array[array_size] = {};
        last_broadcast = now;
    }
}

void setup() {
    Serial.begin(9600);
    pinMode(A0, INPUT); // mic AUD connected to Analog pin 0
    pinMode(D7, OUTPUT); // flash on-board LED
}

void loop() {
    int mic_reading = analogRead(0); //Serial.println(mic_reading);
    blinkMic(mic_reading);
    averageReading(mic_reading); 
    checkBroadcast();

    delay(sample_rate);
}

Pulling Data into Google Sheets

JavaScript
This code allows you to pull data from your node into Google Sheets to analyze. Adjust code for different photon, sensors and variables
function collectCurrent() {
  "use strict";
  try
  {
    //Replace the device ID below with your Photon's unique device ID
    var deviceID = "2d002a000e47343432313031";
    
    //Replace the access token below with your Photon's unique access token
    var accessToken = "bd8fd33ef366f8c26afa147119d1001bb5b675dd"; 
    
    //Replace the value below with you group ID
    var groupID = 401;
    
    //Replace the room number below with location of the Photon
    var room = 302;
    
    var sheet = SpreadsheetApp.getActiveSheet();

    // Fetch the value of the testValue variable from the Spark Cloud API.
    // The name of your variable in Particle's cloud must match the variable in the URL below.
    //var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/reading?access_token=" + accessToken); //changed to reading

    // Parse the JSON and extract the testValue.
    //var jsonResponse = JSON.parse(response.getContentText());
    //var light = jsonResponse.result;
    
    var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_min?access_token=" + accessToken); // changed var to snd_min
    jsonResponse = JSON.parse(response.getContentText());
    var min = jsonResponse.result; 
    
    var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_max?access_token=" + accessToken); // changed var to snd_max
    jsonResponse = JSON.parse(response.getContentText());
    var max = jsonResponse.result; 

    var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/" + deviceID + "/snd_avg?access_token=" + accessToken); // changed var to snd_avg
    jsonResponse = JSON.parse(response.getContentText());
    var avg = jsonResponse.result;    

    // Create a timestamp.
    var timeStamp = new Date();

    // Append the timestamp and the temperature to the sheet.
    sheet.appendRow([timeStamp, groupID, room, min, max, avg]); //added min, max, avg
  } catch (e)
  {	
    // If something doesn't work, log it, then rethrow the error.
	Logger.log(e.name + ": " + e.message);
	throw (e);
  }
}

Credits

Breann Mitchell

Breann Mitchell

-1 projects • 0 followers
Brian Tait

Brian Tait

-1 projects • 0 followers
Josh Skawski

Josh Skawski

-1 projects • 0 followers
hey

Comments