// Project ideated and created by Shrey Agrawal
// References: http://diotfall2019.daraghbyrne.me/assignments/internet-of-plants/
// Project code starts ***************
int buzzer = D5; //define output pin for peizo
int fsrledPin = D0; //define pin for the red LED
int redPin = D2; //to represent if the plant feels hot
int bluePin = D4; //to represent if the plant feels cold
int greenPin = D3; //to represent the optimim temperature for the plant
int brightness = 0; //variable required to activate the RGB LED
int soilledPin = D6; //define the pin for LED of soil sensor
int tempPin = A5; //define the pin for the temperature sensor
int soilPin = A3; //define pin for soil moisture sensor
int fsrPin = A1; //define pin for photosensor
int fsrReading = 0; //declare a variable to store the value of photosensor
int soilReading = 0; //declare a value to store the value of soil moisture sensor
double temperature = 0.0; //Variable to store the value of temperature.
// ***************Code for defining the notes and song************************
// change this to make the song slower or faster
int tempo = 144;
// notes of the melody followed by the duration.
int melody[] = {
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
// Hedwig's theme fromn the Harry Potter Movies
REST, 2, NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_G4, 2, NOTE_D5, 4,
NOTE_C5, -2,
NOTE_A4, -2,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
NOTE_F4, 2, NOTE_GS4, 4,
NOTE_D4, -1,
NOTE_D4, 4,
NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4, //10
NOTE_G4, 2, NOTE_D5, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_B4, 4,
NOTE_G4, -1,
NOTE_AS4, 4,
NOTE_D5, 2, NOTE_AS4, 4,//18
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_DS5, 2, NOTE_D5, 4,
NOTE_CS5, 2, NOTE_A4, 4,
NOTE_AS4, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_D4, 4,
NOTE_D5, -1,
REST,4, NOTE_AS4,4,
NOTE_D5, 2, NOTE_AS4, 4,//26
NOTE_D5, 2, NOTE_AS4, 4,
NOTE_F5, 2, NOTE_E5, 4,
NOTE_DS5, 2, NOTE_B4, 4,
NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
NOTE_CS4, 2, NOTE_AS4, 4,
NOTE_G4, -1,
};
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in ms (60s/tempo)*4 beats
int wholenote = (60000 * 4) / tempo;
int d = 0, noteDuration = 0;
// ********************* Tune code end************************************
//defining the function playnotes. This function is called whenever the force sensor senses force above some threshold.
void playNotes() {
// iterate over the notes of the melody.
// Remember, the array is twice the number of notes (notes + durations)
//we need the music to stop is we are not pressing the force sensor.
for (int thisNote = 0; (thisNote < notes * 2), fsrReading>1000; thisNote = thisNote + 2) {
fsrReading = analogRead(fsrPin);// to ensure that the latest value of the foce is updated, include it in the loop;
if (fsrReading>200){
// calculates the duration of each note
d = melody[thisNote + 1];
if (d > 0) {
// regular note, just proceed
noteDuration = (wholenote) / d;
} else if (d < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(d);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, melody[thisNote], noteDuration*0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
}
//******************* Defining Function Ends *****************
// ************* defining the input and output pins********************
void setup() {
pinMode (buzzer, OUTPUT);
pinMode(soilledPin, OUTPUT);
pinMode(fsrledPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(tempPin, INPUT);
Particle.variable("force", &fsrReading, INT);
Particle.variable("Moisture", &soilReading, INT);
Particle.variable("temperature", &temperature, DOUBLE);
}
void loop() {
//Determining the temperature from the voltage obtained from the sensor
int reading = analogRead(tempPin);
double voltage = (reading * 3.3) / 4095.0;
int temperaturetemp = (voltage - 0.5) * 100;
int temp = 0;
for (int i=0;i<10;i++){
temp = temp + temperaturetemp;
}
temperature = temp/10;
//input the voltage reading for the photocell
fsrReading = analogRead(fsrPin);
// ***************** Code to react to temperature change*****************
// if the plant feels hot then Red LED glows
// if the plant feels cold then Blue LED glows
// if the plant feels good then Green LED glows.
if (temperature<-30){
analogWrite(bluePin, 0);
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
}
else if ( -30 <= temperature && temperature <-20 ){
analogWrite(bluePin, 255);
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
}
else if (temperature > -20) {
analogWrite(bluePin, 255);
analogWrite(redPin, 0);
analogWrite(greenPin,255);
}
//*********************** end temperature********************************
//***************** Code to play music when watering the plant******************
if (fsrReading < 200){
digitalWrite(fsrledPin, LOW); // if the light is not sufficient, then the led is set to high
} else {
digitalWrite(fsrledPin, HIGH);// if the light is sufficient, turn off the LED
playNotes();
}
//****************** To check the moisture level of the soil ******************
soilReading = analogRead(soilPin);
if (soilReading < 500){
digitalWrite(soilledPin, HIGH); // if the soil moisture level is low, LED glows
} else {
digitalWrite(soilledPin, LOW);// when there is sufficient water in the soil, the LED goes off
}
delay(100);
log_to_message( );
log_to_spreadsheet( );
}
//*********************** end ******************************
//*************************** END OF THE CODE THANK YOU*******************
//***************** doclaring all the notes to play the song****************//
//defining all the notes for the song
int NOTE_B0 = 31;
int NOTE_C1 = 33;
int NOTE_CS1= 35;
int NOTE_D1 = 37;
int NOTE_DS1= 39;
int NOTE_E1 = 41;
int NOTE_F1 = 44;
int NOTE_FS1= 46;
int NOTE_G1 = 49;
int NOTE_GS1 =52;
int NOTE_A1 =55;
int NOTE_AS1= 58;
int NOTE_B1 = 62;
int NOTE_C2 = 65;
int NOTE_CS2= 69;
int NOTE_D2 = 73;
int NOTE_DS2= 78;
int NOTE_E2 = 82;
int NOTE_F2 = 87;
int NOTE_FS2= 93;
int NOTE_G2 = 98;
int NOTE_GS2= 104;
int NOTE_A2 = 110;
int NOTE_AS2 =117;
int NOTE_B2 =123;
int NOTE_C3 =131;
int NOTE_CS3 =139;
int NOTE_D3 =147;
int NOTE_DS3 =156;
int NOTE_E3 =165;
int NOTE_F3 =175;
int NOTE_FS3 =185;
int NOTE_G3 =196;
int NOTE_GS3 =208;
int NOTE_A3 = 220;
int NOTE_AS3= 233;
int NOTE_B3 = 247;
int NOTE_C4 = 262;
int NOTE_CS4= 277;
int NOTE_D4 =294;
int NOTE_DS4 =311;
int NOTE_E4 =330;
int NOTE_F4 =349;
int NOTE_FS4 =370;
int NOTE_G4 =392;
int NOTE_GS4 =415;
int NOTE_A4 =440;
int NOTE_AS4 =466;
int NOTE_B4 =494;
int NOTE_C5 =523;
int NOTE_CS5 =554;
int NOTE_D5 =587;
int NOTE_DS5 =622;
int NOTE_E5 =659;
int NOTE_F5 =698;
int NOTE_FS5 =740;
int NOTE_G5 =784;
int NOTE_GS5 =831;
int NOTE_A5 =880;
int NOTE_AS5 =932;
int NOTE_B5 =988;
int NOTE_C6 =1047;
int NOTE_CS6 =1109;
int NOTE_D6 =1175;
int NOTE_DS6 =1245;
int NOTE_E6 =1319;
int NOTE_F6 =1397;
int NOTE_FS6 =1480;
int NOTE_G6 =1568;
int NOTE_GS6 =1661;
int NOTE_A6 =1760;
int NOTE_AS6 =1865;
int NOTE_B6 =1976;
int NOTE_C7 =2093;
int NOTE_CS7 =2217;
int NOTE_D7 =2349;
int NOTE_DS7 =2489;
int NOTE_E7 =2637;
int NOTE_F7 =2794;
int NOTE_FS7 =2960;
int NOTE_G7 =3136;
int NOTE_GS7 =3322;
int NOTE_A7 =3520;
int NOTE_AS7 =3729;
int NOTE_B7 =3951;
int NOTE_C8 = 4186;
int NOTE_CS8= 4435;
int NOTE_D8 = 4699;
int NOTE_DS8= 4978;
int REST =0;
int last_published1 = -1;
int last_published = -1;
void log_to_spreadsheet( ){
// check if 1 minute has elapsed
if( last_published + 60000 < millis() ){
Particle.publish( "log_to_spreadsheet", String( soilReading) );
last_published = millis();
}
}
void log_to_message( ){
// check if 1 minute has elapsed
if( last_published1 + 60000 < millis() ){
Particle.publish( "log_to_message", String( soilReading) );
last_published1 = 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. .