//declare pins to be used
//for servo motor
int servoPin = A3;
Servo myServo;
int servoPos = 0;
//variables for soil moisture
int soilMoistureLed = D3;
int moistPin = A1;
int moistVal = 0;
//variable to store piezo value
int speakerPin = A4;
//melody tune array
int melody[] = {2000,0,2000,0,2000,0,2000,0};
//duration of each of the notes
int noteDurations[] = {4,8,8,4,4,4,4,4};
//set counter for SMS function
int last_published = -1;
void setup() {
pinMode (soilMoistureLed, OUTPUT);
Serial.begin(9600);
//declare particle variable to track and use value on cloud
Particle.variable( "soil", moistVal );
// attaches the servo on the A3 pin to the servo object
myServo.attach( A3 );
//Register our Particle to control the servo
//Particle.function("servo", servoControl);
// Keep a cloud variable for the current position
//Particle.variable( "servoPos" , &servoPos , INT );
}
void loop() {
moistVal = analogRead(moistPin);
Serial.println(moistVal);
//log_to_spreadsheet( );
//extreme condition - water content is extremely low. Send alert to user to take action.
//servo arm moves 90 degrees to open valve
if (moistVal > 3000){
myServo.write(45);
digitalWrite (soilMoistureLed, HIGH);
delay (50);
digitalWrite (soilMoistureLed, LOW);
delay (50);
sendSMS();
//moderate condition - water content is low. Water the plant but don't send alert to user
//servo arm moves 90 degrees to open valve
}else if (moistVal > 2000 and moistVal <3000){
myServo.write(45);
digitalWrite (soilMoistureLed, HIGH);
delay (500);
digitalWrite (soilMoistureLed, LOW);
delay (500);
}else {
digitalWrite (soilMoistureLed, LOW);
//servo arm moves -90 degrees to close valve
myServo.write(0);
}
}
//function for piezo - symbolizes Amazon Echo
void alexa()
{
// 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);
}
}
//function for sending SMS
void sendSMS( ){
Particle.publish( "SendSMS", String( moistVal ) );
last_published = millis();
//only send message once to user. Increase delay between messages even if condition is met.
//delay(1000);
}
//function to log values to spreadsheet
void log_to_spreadsheet( ){
// check if 1 minute has elapsed
if( last_published + 1000 < millis() ){
Particle.publish( "Log", String( moistVal ) );
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. .