Back to Parent

int ledBlue = D2;
int ledGreen = D3;

int micPin = D8;
int photoPin = A0;
int soilPin = A1;

int micReading = 0;
int photoReading = 0;
int soilReading = 0;
int soilReadingcomp = 0;

int last_published = -1;

void setup() {
    
    pinMode(ledBlue, OUTPUT);
    pinMode(ledGreen, OUTPUT);
    
    pinMode(micPin, INPUT);
    pinMode(photoPin, INPUT);
    pinMode(soilPin, INPUT);
    
    Particle.variable("photo", &photoReading, INT);
    Particle.variable("soil", &soilReading, INT);
    
}

void loop() {
    
    micReading = digitalRead(micPin);
    photoReading = analogRead(photoPin);
    soilReading = analogRead(soilPin);
    
    soilReadingcomp = abs( 2800 - soilReading );
    
    if (soilReadingcomp < 100 ){  // soil sensor has a lot of noise, so trying to code in some separation around threshold

        
        soilReading = soilReading - 200;
        
    }
    
    if ( soilReading < 2800 ){  //if soil moisture is low, turn on blue light

        
        digitalWrite(ledBlue, HIGH);
        digitalWrite(ledGreen, LOW);
        
        soilReading = analogRead(soilPin);
        
        com_soil_reading(soilReading);  //communicate low moisture reading - log and email

        
        if (photoReading > 3700 && micReading == LOW){
            
            //if light is bright and sound is coming in, blink blue light to represent water flowing

            
            for (int i=0; i<10; i++){
                
                digitalWrite(ledBlue, HIGH);
                delay (500);
                digitalWrite(ledBlue, LOW);
                delay (500);
                
            }
            
        } 
        
    } else { //all is well, so let user know everything is OK with green light

            
        digitalWrite(ledGreen, HIGH);
        digitalWrite(ledBlue, LOW);
            
        delay (1000);
            
    }

}

void com_soil_reading( int sensorValue ){

  // check if 1 minute has elapsed

	if( last_published + 60000 < millis() ){
		Particle.publish( "com_soil_reading", String( sensorValue  ) );
		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