Back to Parent

Code for plant
int ledYPin = D6;               // Defined a pin for the yellow LED
int ledYState = LOW;            // Create a variable to store the LED brightness.

int photoCellPin = A1;          // Defined a pin for the photo cell, Connected a 10K Ohm                                 resistor to it
int photoCellReading = 0;       // Created a variable to hold the light reading

int ledBPin = D4;               // Defined a pin for the Blue LED - was green
int PIRPin = D5;              // Input pin (for PIR sensor)
int pirState = LOW;             //Started by assuming no motion is detected
int val = 0;                    // variable for reading the pin status
int calibrateTime = 10000;      // Calibration wait time

int fsrAPin = A0;                 // Input pin (for fsr sensor)
int fsrAReading = 0;              // Created a variable to hold the FSR reading

int speakerPin = D2;            // Output pin (for speaker sensor)
int counter = 0;                // Created a counter variable to keep track of the 
                                 amount of times the speaker pin plays a note
int countermod = 0;             // Created a countermod variable that keeps track of 
                                  when the counter reaches 8 played notes

int melody[] = {1760,1976,1047,1175,1319,1397,1568,3520};   // created an array for the                                                                notes in the melody:
int noteDurations[] = {4,8,8,4,4,4,4,4 };                 // note durations: 4 = quarter                                                             note, 8 = eighth note

int ledRPin = D3;               //Defined a pin for the red LED


//
void setup()
{
  
  pinMode(ledYPin, OUTPUT);         // Set the yellow LED for output
  pinMode(photoCellPin, OUTPUT);    // Set the photoCellPin for output

  // Created cloud variables of type integer
  Particle.variable("light", &photoCellReading, INT);   // called 'light' mapped to 
                                                           photoCellReading
  Particle.variable("countermod", &countermod, INT);    // called 'countermod' mapped to                                                           countermod
  Particle.variable("forcea", &fsrAReading, INT);         // called 'force' mapped to                                                                fsrReading
  
  pinMode(ledBPin, OUTPUT);     // Set the blue LED for output
  pinMode(ledRPin, OUTPUT);     // Set the red LED for output
  
  pinMode(PIRPin, INPUT);       // declared PIR sensor as input
  pinMode(speakerPin, OUTPUT);  // declared speakerPin as output
  pinMode(fsrAPin, INPUT);       // declared fsrPin as input
  playNotes();                  //declared playNotes as an output ???
}


void loop() 
{
  
  photoCellReading = analogRead( photoCellPin );    // Use analogRead to read the 
                                                       photo cell reading
 
  if (photoCellReading < 1500)                      //if value of photoCellReading is                                                         less than 1500 
  {
      digitalWrite(ledYPin, HIGH);                  //then, switch on yellow LED pin
      delay(1000);
  }
      digitalWrite(ledYPin, LOW);                   //else, don't switch on the yellow 
                                                      LED pin
  
  if (calibrated())                                 // if the sensor is calibrated
  {                               
    readTheSensor();                                // get the data from the sensor
    reportTheData();                                // report it out, if the state has 
                                                       changed
  }                      
    
    fsrAReading = analogRead(fsrAPin);           // Use analogRead to read the fsr                                                 reading, This gives us a value from 0 to 4095
    delay(100);
    
 if (fsrAReading >1000)
 {
   digitalWrite(ledRPin, HIGH);
   tone(speakerPin, melody[counter%8],800);        //for speaker output
   countermod = counter %8;
   counter++;
   delay(250);
   digitalWrite(ledRPin, LOW);
 }  
 
log_to_spreadsheet();                               //Defined log_to_spreadsheet to log                                                data to google sheets from Piezo trigger
delay(1000);

send_me_a_sms();               //Defined send_me_a_sms to send message "Hope you enjoyed                                 the music" when Piezo is triggered
delay(1000);

}

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

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

void setLED(int state) 
{
    digitalWrite(ledBPin, state);
}

void reportTheData() 
{
  // if the sensor reads high
  // or there is no motion
  if (val == HIGH) {

    // the current state is no motion/just changed
    // particle publish for event is detected
    if (pirState == LOW) {
      // Just turned on
      Particle.publish("designingiot/s19/motion", "detected");
      // Update the current state
      pirState = HIGH;
      setLED( pirState ); }
  }
    else 
    {
    if (pirState == HIGH) {
      // Just turned of
      // Update the current state
      Particle.publish("designingiot/s19/motion", "ended");

      pirState = LOW;
      setLED( pirState );
      
      delay(1000);
    }
}
}
        
        
    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.
      //quarter note = 1000 / 4, eighth note = 1000/8
      int noteDuration = 1000/noteDurations[thisNote];
      tone(speakerPin, melody[thisNote],noteDuration);

      // minimum time set in between to distinguish the notes
      // the note's duration
      int pauseBetweenNotes = noteDuration * 1.00;
      delay(pauseBetweenNotes);
      // stop the tone playing:
      noTone(speakerPin);
    }
    
}


int last_published = -1;            // store the time when last published

void log_to_spreadsheet(  ){        //log in google sheets each time Piezo is triggered

if( last_published + 6000 < millis() ){         // check if 1 minute has elapsed
Particle.publish( "log_to_spreadsheet", String( speakerPin ) );
last_published = millis();
	}
	
}

void send_me_a_sms(  ){                    //send a message each time Piezo is triggered

if( last_published + 6000 < millis() ){     // check if 1 minute has elapsed
Particle.publish( "send_me_a_sms", String( speakerPin ) );
last_published = millis();

	}

}
Tarika Jain Click to Expand

Content Rating

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

0