Back to Parent

int PIRPin = D2;
//define the pin we will connect the PIR sensor to
int PiezoPin = D2;
//define the pin we will connect the piezo sensor to
Servo myservo;
// create servo object to control a servo
int pos = 0;    
// variable to store the servo position
int ledPin = D4; 
//define the pin we will connect the LedPin to
int PIRReading = 0;
//initially assign PIR sensor as ZERO. This line of code is optional too.
int PiezoReading = 0;
//initially assign piezo as ZERO. This line of code is optional too.
int ledBrightness = 0;
//initially assign fsrReading as ZERO. This line of code is optional too.  
int i=0;
//declare a integer i to ZERO so that we can use it later
// store the time when you last published
int melody[] = {1000,1500,2000,2000,750,0,200,1700}; 
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int noteDurations[] = {4,8,8,4,4,4,4,4 };
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.
int last_published = -1;
int hour= Time.hour();
//get the hour value of the local time. 
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);
    }
}
void log_to_spreadsheet(  )
{
  // check if 1 minute has elapsed
	if( last_published + 60000 < millis() )
	{
		Particle.publish( "log_to_spreadsheet", String( PIRReading ) );
		last_published = millis();
	}
}

void setup()
{
    
  pinMode(PIRPin, INPUT);
  //setup the PIR Pin for input
  pinMode(PiezoPin, OUTPUT);
  //setup the flexPin for input too
  pinMode(ledPin, OUTPUT);
  // Set up the LED for output
  Particle.variable("Proximity", &PIRReading, INT);
  // Create a cloud variable of type integer called 'proximity' mapped to PIR Reading
  myservo.attach(D3);   
  // attach the servo on the D0 pin to the servo object
  Time.zone(-5);
  //Set the timezone to local timezone
  Serial.begin(9600);
  //
  Particle.syncTime();
  //sync the current local time with the particle board.
}
void loop()
{ 
  delay(86400000);
  //This is the delay time for opening the perfume box next day. 
  //The user can  manipulate this number to change the frequency of engagement with the plant in a day. Right now, the value is for 24 hours  
  myservo.write(100);    
  // open the perfume box hole. The servo rotates to the mentioned value. 
  delay(1000);
  //This delay is the time need to sense the PIR sensor before triggering 
  PIRReading = analogRead(PIRPin); 
  //get the analog value from the Photoresistor Pin
  ledBrightness = map (PIRReading, 0, 4095, 0, 255);
  //}
  if (PIRReading>200) 
  {
      digitalWrite(ledPin, HIGH);
      //having a digitalWrite instead of analogWrite would make the LED blink at full brightness  
      playNotes();
      //we are calling the playnotes function
      myservo.write(50);
      //close the perfue box. The servo now closes the perfume box by a whole rotation
      digitalWrite(ledPin, LOW);
      //turn off the LED after the servo closes the box
      delay(1000);
  } 
  else 
    { 
      digitalWrite(ledPin, LOW);
    }
    log_to_spreadsheet(  );
  delay(100);
  //wait for 1/10 of the second and then loop
}
Click to Expand

Content Rating

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

0