Back to Parent

// define variable to hold tiltPin reading
int tiltpinReading = 0; 

// tilt pen 
int tiltPin = D4;

// Define a pin we'll place a red LED on
int tiltledPin = D2;

// Create a variable to store the red LED brightness.
int tiltledBrightness = 0;

int tiltledState = LOW;       // variable used to store the last LED status, to toggle the light
int tiltpinState;            // the current reading from the input pin
int lasttiltpinState = HIGH; // the previous reading from the input pin

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

//below are for the soil moisture sensor 
int rainPin = A0;
int greenLED = D6;

// you can adjust the threshold value
int thresholdValue = 2500;

int sensorValue = 0;



void setup() {

    // set up tiltpin as input
    pinMode(tiltPin, INPUT);
    
    // Set up the LED for tilt led cell output
    pinMode( tiltledPin, OUTPUT );
    
    //below is for the soil moisture sensor 
    pinMode(rainPin, INPUT);
    
    // set up green light as an output
    pinMode(greenLED, OUTPUT);
    
    //green led light should be off if moisture is detected;
    digitalWrite(greenLED, LOW);
      
    delay(1000);
    
    // defining 'tiltDirection' and 'rain' so I can see feedback in the console 
    Particle.variable("tiltDirection", &tiltpinReading, INT);
     Particle.variable("rain", &sensorValue, INT); 
    
      

}

void loop() {
    
 
 // check to see if the sensor was tilted and you've waited 
  // long enough since the last change to ignore any noise:  

  // If the tilt changed, due to noise or tilting: 
  if (tiltpinReading != lasttiltpinState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    
    // if the button state has changed:
    if (tiltpinReading != tiltpinState){
      tiltpinState = tiltpinReading;
      
    // if the sensor reading is low
      if (tiltpinReading == LOW) {
        // make the status of the ledPin to go on:
        tiltledState = HIGH;   
        // update the LED pin itself:        
        digitalWrite(tiltledPin, tiltledState);
        // dalay the LED on for one second:
        delay(500);
      }
      // otherwise if it is high
      else if (tiltpinReading == HIGH){
        // make the status of the ledPin to stay off:
        tiltledState = LOW;
        // update the LED pin itself:
        digitalWrite(tiltledPin, tiltledState);
      }
    }
   }
  // save the reading.  Next time through the loop,
  // it'll be the lasttiltpinState:
  lasttiltpinState = tiltpinReading; 
  // delay to avoid overloading the serial port buffer:
  delay(100);  
   
   //below is for the soil moisture sensor 
   sensorValue = analogRead(rainPin);
    tiltpinReading = digitalRead(tiltPin);
  

  if(sensorValue < thresholdValue){
    
       // green LED goes on, meaning plant is hydrated
        digitalWrite(greenLED, HIGH);
    }else{
        // digitalWrite(redLED, HIGH);
        digitalWrite(greenLED, LOW);
    }
   
  delay(500);
 
log_to_spreadsheet();  
}

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

void log_to_spreadsheet(  ){

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

		last_published = millis();
	}

}
Click to Expand

Content Rating

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

0