//Code for the DioT_SmartPlant
int ledPinR = D2; // define a variable to display low moisture level
int ledPinG = D3; // define a variable to display optimum moisturelevel
int ledPinB = D4; // define a variable to display water-logged level
int ledBrightness = 0; // define a variable to read analog value; intial value=0
int redPin = A2; // RED pin of the RGBLED
int greenPin = A3; // GREEN pin of the RGBLED
int bluePin = A4; // BLUE pin of the RGBLED
int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int moistPin = A1; // define a variable to detect the soil moisture
int moistVal = 0; // define a variable to read the analog value of moisture sensor
int speakerPin = D5; // variable for the piezo pin
int melody[] = {100,200,100,200,100,300,500,300,500,100,200,100,200,100,300,500,300,500,100}; // create an array for the notes in the melody 1:
int melody2[] = {2500,3000,5000,4000,3000,2500,2000,3000,4000,4000,3000,2000,2500,3000,4000,5000,3000,2500};//{2500,2500,3000,5000,4000,3000,2500,2500,1900,3000,1000,1000,3000,1900,2500,2500,3000,4000,5000,3000,2500,2500}; // create an array for the notes in the melody 2:
int noteDurations[] = {4,4,6,6,6,6,8,8,8,8,8,8,6,6,6,6,4,4}; //create an array for the duration of notes.
int inputPin = D6; // variable for the input pin (for PIR sensor)
int pirState = LOW; // to start, assuming no motion detected
int val = 0; // variable for reading the PIR pin status
int fsrPin = A5; // define a variable to detect the fsr force
int fsrReading = 0; // define a variable to read the analog value of fsr
int toggle=0; // variable for switch button for fsr
int toggle_2=0; // variable for second switch button for fsr
int calibrateTime = 5000;// wait for the thingy to calibrate
int last_published = -1;
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPinR, OUTPUT); // declare red pin as output for detecting moisture
pinMode(ledPinG, OUTPUT); // declare green pin as output for detecting moisture
pinMode(ledPinB, OUTPUT); // declare green pin as output for detecting moisture
pinMode(inputPin, INPUT_PULLUP); // declare PIR sensor as input
Particle.variable( "soilMoisture", moistVal ); // put the soil moisture value on the cloud
pinMode(redPin, OUTPUT); // declare red pin as output for RGB
pinMode(bluePin, OUTPUT); // declare blue pin as output for RGB
pinMode(greenPin, OUTPUT); // declare green pin as output for RGB
Particle.variable("fsrForce", &fsrReading, INT); // put the fsr force value on the cloud
// turn them all off
analogWrite( redPin, redValue);
analogWrite( greenPin, greenValue);
analogWrite( bluePin, blueValue);
}
void loop()
{
// if the sensor is calibrated
if (calibrated())
{
// get the data from the sensor
readTheSensor();
// report it out, if the state has changed
reportTheData();
}
moistVal = analogRead(moistPin); // read the analog value from the soil moisture sensor
fsrReading = analogRead(fsrPin); // to read the analog value from the fsr sensor
}
void readTheSensor() {
val = digitalRead(inputPin); // to detect the motion
}
bool calibrated() {
return millis() - calibrateTime > 0; // condition to start the process
}
void setRedLED() {
digitalWrite(ledPinR, HIGH); // turn red led pin on
}
void setGreenLED2(){
digitalWrite(ledPinG, HIGH); // turn green led pin on
}
void setBlueLED3(){
digitalWrite(ledPinB, HIGH); // turn blue led pin on
}
void setLEDoff()
{
// turn all of them off...
digitalWrite(ledPinR,LOW);
digitalWrite(ledPinG,LOW);
digitalWrite(ledPinB,LOW);
}
void reportTheData() {
if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an event
if (pirState == LOW)
{
// we have just turned on
Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
// Update the current state
if(moistVal>3600 && fsrReading <100)
{
toggle_2=0;
setLEDoff();
setRedLED();
if(toggle==0)
playNotes1();
}
else if (moistVal>0 && moistVal < 1900 && fsrReading <100)
{
toggle_2=0;
toggle=0;
setLEDoff();
setBlueLED3();
}
else if (moistVal>1900 && moistVal <3600 && fsrReading <100)
{
setLEDoff();
setGreenLED2();
if(toggle_2==0)
playNotes2();
toggle=0;
}
pirState = HIGH;
}
else if (fsrReading >100)
{
noTone(speakerPin);
toggle=1;
toggle_2=1;
redValue=rand()%255+1; //getting random value for red between 0 and 255(including the limits)
greenValue=rand()%255+1; //getting random value for green between 0 and 255(including the limits)
blueValue=rand()%255+1; //getting random value for blue between 0 and 255(including the limits)
analogWrite(redPin, redValue); //output the red led
analogWrite(greenPin, greenValue);//output the green led
analogWrite(bluePin, blueValue);//output the blue led
// turn them all off...
delay(100);
}
analogWrite( redPin, 255);
analogWrite( greenPin, 255);
analogWrite( bluePin, 255);
}
else {
//digitalWrite(ledPin, LOW);
if (pirState == HIGH) {
// we have just turned of
// Update the current state
Particle.publish("PhotonMotion", "Off", PRIVATE);
pirState = LOW;
noTone(speakerPin);
}
}
}
//void reportTheData() {
// if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an event
// if (pirState == LOW) {
// we have just turned on
// Particle.publish("PhotonMotion", "Motion Detected", PRIVATE);
// Update the current state
// pirState = HIGH;
// if(moistVal>1500) {
// setRedLED(pirState);
// playNotes1();
//}else if (moistVal <900);{
//setBlueLED3(pirState);
//playNotes1();
// } }else if (900 < moistVal <1500 ){
// setGreenLED2(pirState);
// playNotes2();
//}
// } else {
//digitalWrite(ledPin, LOW);
// if (pirState == HIGH) {
// we have just turned of
// Update the current state
// Particle.publish("PhotonMotion", "Off", PRIVATE);
// pirState = LOW;
// setRedLED(pirState);
// }
// }
//}
void playNotes1()
{
// 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);
}
}
void playNotes2()
{
// 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, melody2[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);
}
}
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. .