Back to Parent

int last_published = -1;//event publish record 




//Initialize variables and set pins

int thresholdUp = 3000; 
int thresholdDown = 350;
int sensorPin = A0;
int sensorValue=0;


// Sketch for Particle Photon - PIR Sensor / Motion Detection
int inputPin = D4;              // choose the input pin (for PIR sensor)
int ledPin = D0;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 1000;      // wait for the thingy to calibrate
bool shouldLedOn=false;


int speakerPin = D2;

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

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

int dryDay=0;//record the number of days that I haven't water the plant

void setup(){
 pinMode(sensorPin, INPUT);
 Particle.variable("moisturemoisture level", &sensorValue,INT);
  Particle.variable("drydays", &dryDay,INT);

 //digitalWrite(sensorVoltage, HIGH);
 pinMode(ledPin, OUTPUT);
    pinMode(inputPin, INPUT);     // declare sensor as input
    }


// Sensor Readings

void loop()

{
// if the sensor is calibrated
  if (calibrated()) {
  // get the data from the sensor
    readTheSensor();
    // report it out, if the state has changed
    reportTheData();
    }

updateMoisture();

}


void updateMoisture(){
    
    sensorValue = analogRead(sensorPin);
    
    if (sensorValue > thresholdUp){
        	if( last_published + 60000 < millis() ){//use one minute to simulate a day
    Particle.publish("SoilStatus","Dry! Water me!!",60,PUBLIC);//for IFTTT to email me a reminder
    dryDay++;
    if( dryDay>=3){
        Particle.publish("SoilStatus","Haven't water for three days!!",60,PUBLIC);
        //for IFTTT to create a post on my personal website announcing how I don't take care of my plant for 3 days
        dryDay=0;
    }
    	last_published = millis();
        	}
        	digitalWrite(ledPin, 200);
    shouldLedOn=true;//turn on the LED
   // delay(5000);
     }
    

 else if (sensorValue <= thresholdDown){//too much water for the plant
	if( last_published + 60000 < millis() ){
    Particle.publish("SoilStatus", "Too much water!",60,PUBLIC);
    last_published = millis();
    dryDay=0;        	}
        	 digitalWrite(ledPin, 0);
  shouldLedOn=false;//turn off the LED


     } 

  else {//enough water for the plant

   // digitalWrite(LEDs, LOW);
	if( last_published + 60000 < millis() ){
    Particle.publish("SoilStatus", "No Water Needed!",60, PUBLIC);
     last_published = millis();
       dryDay=0;    
        	}
        	 digitalWrite(ledPin, 0);
shouldLedOn=false;


  }



  
}

void readTheSensor() {
    val = digitalRead(inputPin);
}

bool calibrated() {
    return millis() - calibrateTime > 0;
}



void setLED(int state) {
    shouldLedOn=state;
    }


void reportTheData() {
    // for the piezo to playNotes based on the PIR sensor feedback(when the plant needs water, play notes when someone appraoch)
    if (val == HIGH) {
        // the current state is no motion
        // i.e. it's just changed
        // announce this change by publishing an event
        if (pirState == LOW) {
          // we have just turned on
          Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
          // Update the current state
          pirState = HIGH;
         
if(shouldLedOn){
   
           playNotes();
    }
        }
    } else {
        if (pirState == HIGH) {
          // we have just turned of
          // Update the current state
          Particle.publish("PhotonMotion", "Off", PRIVATE);
          pirState = LOW;
         
          
          
    

          
        }
    }
}

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(speakerPin, 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(speakerPin);
    }
}
Click to Expand

Content Rating

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

0