Things used in this project

Hardware components:
Photon new
Particle Photon
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1
Hand tools and fabrication machines:
Lasercutter
Laser cutter (generic)

Code

Sound Code for PhotonArduino
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];
            
        }
    }

    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;
}

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
   
    Particle.variable("snd_avg", snd_avg);
    Particle.variable("snd_max", snd_max);
    Particle.variable("snd_min", snd_min);

}

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

    delay(sample_rate);
}

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

ConnectTheDots with Particle Azure IoT Hub Integration
Intermediate
  • 563
  • 7

Protip

This project will allow you to connect your Particle device into an Azure IoT Hub for viewing data in real-time through an ASP.NET web app!

Other Clocks
Intermediate
  • 186
  • 3

Work in progress

A combination of 3 different clocks in one frame.

Christmas Gift Box
Intermediate
  • 3,602
  • 595

Full instructions

Christmas Gift Box plays music and sends an email when it is opened.

Weather Aware Sprinkler Controller
Intermediate
  • 1,301
  • 16

Full instructions

6 station Photon controller uses the Weather Underground API to prevent watering when windy, rainy, or too cold.

Simple Environmental Monitoring
Intermediate
  • 1,558
  • 13

Full instructions

Particle Photon circuit allowing the display of environmental conditions (light, temperature, humidity, pressure) using the Blynk app.

THDweeter
Intermediate
  • 249
  • 2

Protip

Yet another temperature-humidity sensor publishing to dweeter, with WiFi auto-disconnect and a push button to read daily max and min values.

Add projectSign up / Login