Lights Out

Made by Grae Prickett

Found in Home Hack · UNLISTED (SHOWN IN POOLS)

The intention of this IoT project is to help turn off lights within the home when not in use. My roommate tends to leave lights on when she heads to bed or leaves the apartment, so creating a system that understands when no one is in the room and can turn off the lights. It will work by sensing sound and once the particle recognizes that sound has been displayed in over 30 minutes, it will shut off the lights.

0

Intention

Please see attached PDF titled "Creative Assignment #1".

0

Context

Please see attached PDF titled "Creative Assignment #1".

0

Process

Please see attached PDF titled "Creative Assignment #1".

0

Product

Detail what you created. What methods or techniques did you use? What tools and technologies were involved? Include images, code or video.

0

Reflection

Please see attached PDF titled "Creative Assignment #1".

0
// the pin we're reading from
int mic_pin = A0;
const int sampleWindow = 50;
int numPins = 2;
int ledPins[ 2 ] = { D0, D1 };
int piezoPin = D2;
// know if the lights are on or off
bool light_state = false;
// for our sound detection
// store the noise level / reading from the electret
int noise_level = 1000;
// empty house noise level
int noise_threshold = 500;
// when we last heard a noise
long last_loud_noise_at = -1;
int  turn_lights_off_after = 10 * 1000;


// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};

// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };


void setup(){
 // start serial connection
 Serial.begin(9600);
 for( int i = 0 ; i < numPins; i++ ){
   pinMode(ledPins[ i ], OUTPUT);
 }
 light_state = true ;
 for( int i = 0 ; i < numPins; i++ ){
   int led = ledPins[ i ];
   digitalWrite( led, HIGH );
 }

 pinMode(piezoPin, OUTPUT);


 // Share the value as a noise level
 // reading through the cloud
 Particle.variable( "noise", &noise_level, INT );
}
void loop()
{
 noise_level = sampleNoise( );
 Serial.print( "Noise Level:" );
 Serial.println( noise_level );
 checkNoiseLevel();
 checkifShouldTurnLightsOff();
 displayLEDs();

 Serial.print( "Noise At:" );
 Serial.println( last_loud_noise_at );
 delay( 100 );
}
void checkNoiseLevel(){
  if( noise_level > noise_threshold ){
    // this means the house is occupied
    last_loud_noise_at = -1;
    //light_state = true;
  }else{
    if( last_loud_noise_at == -1 )
      last_loud_noise_at = millis();
  }
}
void checkifShouldTurnLightsOff(){
  if( last_loud_noise_at == -1 || light_state == false ){
    // do nothing;
    return;
  }
  if( last_loud_noise_at + turn_lights_off_after < millis() ){

    digitalWrite(D2, HIGH);
    delay(100);
    digitalWrite(D2, LOW);
    // add a beep here
    playNotes();

    light_state = false;


  }
}
void displayLEDs(){
 for( int i = 0 ; i < numPins; i++ ){
   int led = ledPins[ i ];
   if( light_state == true ){
     digitalWrite( led, HIGH );
   }else{
     digitalWrite( led, 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;
}




void playNotes()
{
    // iterate over the notes of the melody:
    for (int thisNote = 0; thisNote < 8; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/noteDurations[thisNote];
      tone(piezoPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:
      noTone(piezoPin);
    }
}
Click to Expand
0

Please see attached PDF titled "Creative Assignment #1", for the final Bill of Materials.

PARTS:

  • Almost any Arduino – I used the popular Arduino Uno
  • Some breadboard cables
  • A breadboard
  • A sound sensor – I used a high sensitivity sound detection sensor module by Keyes
  • At least 1 LED light – I used 7! The more the better!
  • As many LEDs you are using you should have resistors. 333 ohm resistors should do the trick for most LEDs but I used 100 ohm ones.
x
Share this Project

This project is only listed in this pool. Be considerate and think twice before sharing.


Found In
Courses

49-713 Designing for the Internet of Things

· 26 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

The intention of this IoT project is to help turn off lights within the home when not in use. My roommate tends to leave lights on when she heads to bed or leaves the apartment, so creating a system that understands when no one is in the room and can turn off the lights. It will work by sensing sound and once the particle recognizes that sound has been displayed in over 30 minutes, it will shut off the lights.

Created

January 26th, 2017