Back to Parent

// led1 in bathroom
int ledPin1 = D0;
// led2 outside (maybe in living room)
int ledPin2 = D1;
// to read from photoresistor
int photoCellPin= A0;
int photoCellReading=0;
// set threshold for turning on bathroom light
int threshold= 3000;
bool lightOn= false;
// store the time value since bathroom light was first switched on
long timeSinceThreshold = 0 ;
const int speakerPin = A4;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
// Beats is an array of values for each note and rest- quater, half and full notes
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int play = 0;
int flag=0;
int m=0;
int frequency(char note)
{
  // returns corresponding frequency in Hz for the tone() function.
  int i;
  const int numNotes = 8;  // number of notes

  // The following arrays hold the note characters and their corresponding frequencies.
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // search through the letters in the array, and if found,it returns the frequency for that note.

  for (i = 0; i < numNotes; i++)
  {  // Step through the notes
    if (names[i] == note)
    {
      return(frequencies[i]);
    }
  }
  return(0);
}

int tempo = 150;
void setup()
{
  //led as output
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode( speakerPin , OUTPUT );
  //connect photoCellReading to cloud
  Particle.variable("light", &photoCellReading, INT);
  //Particle.variable("music",&m, INT );
  //to input/output through the usb - for checking
  Serial.begin(9600);

}
int timeIsUp()
{
  /*if difference between current time and start time is greater than 10 minutes, return true*/
  if ((millis()- timeSinceThreshold)> 600000)
   return 1;
  else
   return 0;
}
void loop()
{
  // read from sensor
  photoCellReading= analogRead(photoCellPin);
  Serial.print ("Sensor reading is: ");
  Serial.println(photoCellReading);
  //see if bathroom light is switched on
  //if light is on, blink inside led once to signal and then start counter for 10min
  //switch outside led on
  int timesUp = timeIsUp();
  if (photoCellReading>threshold && lightOn != true && timesUp == 0)
  {
    //start counter
    play=1;
    flag=1;
    Serial.println ("Flag ");
    timeSinceThreshold = millis();
    lightOn = true;
    //switch on outside led
    digitalWrite(ledPin2, HIGH);
    // Blink led in bathroom
    digitalWrite(ledPin1, HIGH);
    delay(3000);
    digitalWrite(ledPin1, LOW);
  }
  else if (photoCellReading>threshold && lightOn == true && timesUp == 0)
  {
    pinMode(speakerPin, OUTPUT);
    digitalWrite(ledPin2, HIGH);
    play=1;
    int i, duration;
    //Serial.println( "playing melody");
    for (i = 0; i < songLength; i++) // step through the song arrays
    {
      duration = beats[i] * tempo;  // length of note/rest in ms
      if (notes[i] == ' ')          // is this a rest?
      {
        //Serial.println( "rest melody");
        delay(duration);            // then pause for a moment
      }
      else                          // otherwise, play the note
      {
        //Serial.println( "play note melody");
        //Serial.println( duration );
        tone(speakerPin, frequency(notes[i]), duration);
        delay(duration);            // wait for tone to finish
        noTone( speakerPin );
      }
      delay(tempo/10);              // brief pause between notes
    }
  }

  else if (photoCellReading>threshold && lightOn == true && timesUp == 1)
  {
    play=0;
    for(int k=0;k<3;k++)
   {
     digitalWrite(ledPin1, HIGH);
     digitalWrite(ledPin2, LOW);
     delay(500);
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, HIGH);
   }
 }
 if (photoCellReading<threshold)
 {
   lightOn=false;
   play=0;
   Serial.println ("checking flag ");
   digitalWrite(ledPin2, LOW);
   digitalWrite(ledPin1, LOW);
   int m =((millis()- timeSinceThreshold)/60000);
   if (flag==1){
     Particle.publish("Bathroom usage today:", String(m));
      flag = 0;
   }
 }
 Particle.variable("music",&play, INT );

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0