Problem: Our washer & dryer are in the basement, so we can't hear when it's done. We're on the second floor and my roommates often walk downstairs multiple times to check when their laundry is done.
Goal: Build a device that sends them a notification when their laundry is done.
// 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
I remixed the code to send a push notification after the sound passed a certain threshold. I switched from SMS because I didn't want to overload my SMS while debugging. It started a little too sensitive.
I also added a switch and status light so that I could control when it was publishing events. I added a "notifyLed" that would light up whenever an event published. This was primarily for debugging.
// the pin we're reading from
int mic_pin = A0;
// store the noise level / reading from the electret
int noise_level = 0;
const int sampleWindow = 50;
int notifyLed = D5;
int switchPin = D1;
int statusLed = D0;
void setup(){
// start serial connection
pinMode(notifyLed, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(statusLed, OUTPUT);
Serial.begin(9600);
// Share the value as a noise level
// reading through the cloud
Particle.variable( "noise", &noise_level, INT );
}
void loop()
{
noise_level = sampleNoise( );
int buttonState = digitalRead( switchPin );
Serial.println( noise_level );
delay( 100 );
if (buttonState == LOW){
digitalWrite(statusLed, LOW);
}
else {
digitalWrite(statusLed, HIGH);
if (noise_level > 5500 ){
Particle.publish("wash_status", "done");
digitalWrite(notifyLed, HIGH);
}
else {
digitalWrite(notifyLed, LOW);
}
}
}
int sampleNoise( )
{
unsigned long startMillis = millis(); // Start of sample window
int highest_sample = 0;
int lowest_sample = 2000;
// 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, 6000, 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
Unfortunately, prior to documentation, my mic stopped giving a reading. I tried re-soldering but couldn't get it online. In the name of improvisation, I switched my device to a motion detector that would notify me when it detected motion. This would be great for my dog, who constantly breaks into the kitchen. I used our tutorial code: http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
I used the same switch and status functionality I had in the previous device.
int statusLed = D0; // choose the input pin (for PIR sensor)
int notifyLed = D5; // LED Pin
int pirState = LOW;
int switchPin = D1;
int inputPin = D6; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int calibrateTime = 10000; // wait for the thingy to calibrate
void setup()
{
pinMode(switchPin, INPUT_PULLUP);
pinMode(statusLed, OUTPUT);
pinMode(inputPin, INPUT);
pinMode( notifyLed, OUTPUT ); // declare sensor as input
}
void loop()
{
int buttonState = digitalRead( switchPin );
// if the sensor is calibrated
if (buttonState == HIGH)
// starts the function and turns on the light to detect motion
{
digitalWrite(statusLed, HIGH);
if ( calibrated() )
{
// get the data from the sensor
readTheSensor();
// report it out, if the state has changed
reportTheData();
}
}
else {
digitalWrite(statusLed, LOW);
}
}
void readTheSensor() {
val = digitalRead(inputPin);
}
bool calibrated() {
return millis() - calibrateTime > 0;
}
void reportTheData() {
// if the sensor reads high
// or there is now motion
if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an eent
if (pirState == LOW) {
// we have just turned on
Particle.publish("motion_detect", "done");
// Update the current state
pirState = HIGH;
setLED( pirState );
}
} else {
if (pirState == HIGH) {
// we have just turned of
// Update the current state
pirState = LOW;
setLED( pirState );
}
}
}
void setLED( int state )
{
digitalWrite( notifyLed, state );
}
Click to Expand
Reflection
I was a little bit worried about this project because of my limited experience programming and nonexistent experience with circuits. I was happy to find the basics were fairly accessible, particularly when using IFTTT. While I was disappointed my mic stopped working, I was happy I could hack together a new device. Now that I know I can do this, I look forward to working on more projects!
A hands-on introductory course exploring the Internet of Things and connected product experiences.
A device that notifies my roommates when their laundry is done. (I'm not sure if my previous project posted so trying again)
February 2nd, 2017