int redPin = D2; // RED pin of the LED to PWM pin **d2**
int greenPin = D3; // GREEN pin of the LED to PWM pin **d3**
int bluePin = D4; // BLUE pin of the LED to PWM pin **D4**
int fsrPin=A2; //Fsr pin initialization
int fsrReading=0; //variable for reading fsr sensor
int moistPin=A1;//initializing moisture sensor pin
int moistVal=0;//variable for reading moist sensor
int moistMax=1500;//variable for reading maximum moisture in the soil
int moistMin=800;//variable for reading minimum moisture in the soil
int redValue=255; // Brightness of red LED
int greenValue=255; // Brightness of green LED
int blueValue=255; // Brightness of blue LED
Servo heartbeat;//define the servo motor
int moisture=0;//Moisture value to be logged in the google sheets
void setup() {
Particle.variable( "SoilReading", moistVal );//Display actual soil moisture sensor reading
Particle.variable( "Moisture", moisture );//Display the moisture value mapped from the actual sensor reading
Particle.variable( "Button", fsrReading );//Display the FSR button reading
heartbeat.attach( D5 );
pinMode( redPin, OUTPUT ); //pin mode initialization for red pin
pinMode( greenPin, OUTPUT ); //pin mode initialization for green pin
pinMode( bluePin, OUTPUT ); //pin mode initialization for blue pin
analogWrite(redPin, 255);//Initializing red pin to off state
analogWrite(greenPin, 255);//Initializing green pin to off state
analogWrite(bluePin, 255);//Initializing blue pin to off state*/
Particle.function("MaxMoistLevel", setmoistMax);//Get user input for moistMax variable from console
Particle.function("MinMoistLevel", setmoistMin);//Get user input for moistMin variable from console
}
int setmoistMax( String command )
{
// get the maximum moisture level from console
moistMax= command.toInt();
return 1;
}
int setmoistMin( String command )
{
// get the maximum moisture level from console
moistMin= command.toInt();
return 1;
}
void setRGBLED(int red,int green,int blue) //function for setting the RGB LED color
{
analogWrite(redPin, red); //output the red led
analogWrite(greenPin, green);//output the green led
analogWrite(bluePin,blue);//output the blue led
}
void loop() {
fsrReading= analogRead(fsrPin);
moistVal = analogRead(moistPin);
moisture=map(moistVal,0,4095,0,100);
if(moistVal<=moistMax&&moistVal>=moistMin)//check if moisture is in the optimum range
{
setRGBLED(255,0,255);//set green color
heartbeat.write(180);
delay(300);
heartbeat.write(145);
delay(300);
}
if(moistVal>moistMax)//check if the moisture is very high
{
setRGBLED(0,255,255);//set red color
heartbeat.write(180);
delay(120);
heartbeat.write(155);
delay(120);
}
if(moistVal<moistMin && moistVal>30)//check if the moisture is very low
{
setRGBLED(255,255,0);//set blue color
heartbeat.write(180);
delay(1000);
heartbeat.write(145);
delay(1000);
}
if(moistVal<=30)//check if no moisture i.e., the plant is dead
{
setRGBLED(255,255,255);//switch off the LED
delay(10000);
}
if(fsrReading>3000)//check if fsr is pressed
log_to_spreadsheet();//log the moisture value in the spreadsheet
}
int last_published = -1;
void log_to_spreadsheet( )
{
// check if 1 minute has elapsed
if( last_published + 60000 < millis() ){
Particle.publish("Moisture_Value",String(moisture));//publish the moisture value
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!
You must login before you can post a comment. .