Back to Parent

Completed code for Pluto the Plant - Particle
//FSR detection
int fsrPin = A0;
int fsrReading = 0;

//moisture detection
int moistPin = A1;
int moistVal = 0;

//sunlight detection
int photoCellPin = A2;
int photoCellReading = 0;

//Define the pins for LED light
int redPin = D4;
int greenPin = D5;

//Create variables to store LED brightness.
int redLedBrightness = 0;
int greenLedBrightness = 0;

//Define the pin for piezo
int speakerPin = D3;

//Define melody1
int melody1[] = {2637,2093,2637,2093,2794,2093,2637,2093}; 
int noteDurations1[] = {2,2,2,2,2,2,2,2 };

//Define melody2
int melody2[] = {2637,1568,2093,2349,2794,2349,2637,2093}; 
int noteDurations2[] = {4,4,4,4,4,4,4,4};

//Define melody3
int melody3[] = {2637,1568,2093,1568,2093,1568,2093,2349,2794,3136,2794,2349,2637,2349,2637,2093}; 
int noteDurations3[] = {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};

void setup()
{
  //set up a cloud function for phone calls processed via DialogFlow
  Particle.function("callPlant", phoneCall);
  
  //set up LED as output
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  //set up speaker as output
  pinMode( speakerPin, OUTPUT );
  
  //create cloud variables of type integer
  Particle.variable("force", &fsrReading, INT);
  Particle.variable("light", &photoCellReading, INT);
  
  Serial.begin(9600);
  Particle.variable( "soil", moistVal );
}

void loop()
{
  //use analogRead to read sensor readings
  //this gives us a value from 0 to 4095
  fsrReading = analogRead(fsrPin);
  moistVal = analogRead(moistPin);
  photoCellReading = analogRead(photoCellPin);
  Serial.println(moistVal);
  
  //map moisture level into the PWM range (0-255) and use it to decide the color of LED light
  redLedBrightness = map(moistVal, 0, 150, 0, 255);
  greenLedBrightness = 255 - redLedBrightness;

  //detect pressing force on FSR sensor as the trigger to show information
  if (fsrReading >= 4000){
    analogWrite(redPin, 255);
    analogWrite(greenPin, 255);
  }
  else {
    // fade the LED to the desired brightness
    analogWrite(redPin, redLedBrightness);
    analogWrite(greenPin, greenLedBrightness);
    delay(100);
    
    //detect level of sunlight to decide the melody, which represents plant's mood
    if (photoCellReading <= 700) {
        playNotes1();
        delay(100);
    }
    else if (photoCellReading >700 && photoCellReading <=1200){
      playNotes2();
      delay(100);
    }
    else{
        playNotes3();
        delay(100);
    }
  }

// wait 1/10th of a second and then loop
delay(100);
}

//lowest status: calm mood; low level of sunlight
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/noteDurations1[thisNote];
      tone(speakerPin, melody1[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);
    }
}

//mid status: normal mood; medium level of sunlight
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/noteDurations2[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);
    }
}

//highest status: happiest mood; highest level of sunlight
void playNotes3()
{
    //iterate over the notes of the melody:
    for (int thisNote = 0; thisNote < 16; 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/noteDurations3[thisNote];
      tone(speakerPin, melody3[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 to operate when a row is added in the Google Sheets by DialogFlow
int phoneCall(String command)
{
    //take the input from user to decide the color of the light
    if (command == "red"){
      analogWrite(redPin, 0);
    }
    else {
      analogWrite(greenPin, 0);
    }
    
    //turn on the light for 10 sec for demo purpose
    //should set to a longer period of time (e.g. 10~30 min) for practical use 
    delay(10000);
}
Melody Her (2019) Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0