Back to Parent

//Reference: https://diotlabs.daraghbyrne.me

//LED variables declared
int ledPin = D2;
int ledBrightness = 0;

//Soil moisture sensor variables declared
int moistPin1 = A5; // Location of soil moisture sensor on the breadboard
int moistPin2 = A3; //Location of soil moisture sensor on the breadboard
int moistVal1 = 0; //Assigning default/initial value to the sensor reading for moistPin1
int moistVal2 = 0; //Assigning default/initial value to the sensor reading for moistPin2

//Piezo variables declared
int speakerPin = D3; //Location of sound sensor on the breadboard

int melody1[] = {2000,2000,2000,2000,2000,2000,2000,2000}; //tune that plays when the water level goes bellow moistPin1
int noteDurations1[] = {1,1,1,1,1,1,1,1};
int melody2[] = {1000,1500,1000,1500,1000,1500,1000,1500}; //tune that plays when the water level goes bellow moistPin2
int noteDurations2[] = {4,4,4,4,4,4,4,4};

int last_published = -1;


void setup() 
    {
        Serial.begin(9600); 
        pinMode(speakerPin, OUTPUT);
        pinMode(ledPin, OUTPUT);
        
	    Particle.variable( "level1", &moistVal1, INT ); //Detecting reading for moistPin1 in the console 
	    Particle.variable( "level2", &moistVal2, INT ); //Detecting reading for moistPin2 in the console
    }

void loop() 
    {
        moistVal1 = analogRead(moistPin1); //Reading moisture value for moistPin1
        Serial.println(moistVal1); 
        if (moistVal1 > 3000) //if sensor is not submerged in water call function playNotes1
            {
                playNotes1();
                log_to_spreadsheet();
            }
                delay(100); 
        
        moistVal2 = analogRead(moistPin2); //Reading moisture value for moistPin2
        Serial.println(moistVal2);
        if (moistVal2 > 3000 ) //if sensor is not submerged in water call function playNotes2
            {
                playNotes2();
                log_to_spreadsheet();
            }
             delay(100); 
        if (moistVal1 < 3000 && moistVal2 < 3000) //if both sensors are submerged in water flash LED light 
            {
            digitalWrite(ledPin, HIGH);
            delay(100);
            }
        else //Switch off LED 
            {
            digitalWrite(ledPin, LOW); 
            }
            
    }

void playNotes1() //tune/sound function for water level 1
    {
    
        for (int thisNote = 0; thisNote < 8; thisNote++) 
        {

        int noteDuration = 1000/noteDurations1[thisNote];
        tone(speakerPin, melody1[thisNote],noteDuration);

        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
    
        noTone(speakerPin);
        }
    }

void playNotes2() //tune/sound function for water level 2
    {
    
        for (int thisNote = 0; thisNote < 8; thisNote++) 
        {

        int noteDuration = 1000/noteDurations2[thisNote];
        tone(speakerPin, melody2[thisNote],noteDuration);

        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
    
        noTone(speakerPin);
        }
    }
    void log_to_spreadsheet() // recording log in Google Drive  
    {
        if(last_published + 60000 < millis() )
        { 
            Particle.publish("log_to_spreadsheet", String(moistVal1)); 
            Particle.publish("log_to_spreadsheet", String(moistVal2)); 
            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!

0