Back to Parent

// Definitions
int TOUCH_PIN_IN = A1;   // Pin connected to SIG at Touch-Sensor
int LED_PIN = D2;        // LED pin GREEN
int LED_PIN2 = D5;       // LED pin BLUE

int photoCellPin = A0;
int photoCellReading = 0;
int TOUCH_PIN_INReading = 0;


// Create variable to store the LED brightness
int ledBrightness = 25;

int moistPin = A2;
int moistVal = 0; // Create variable to read soil moisture

unsigned long lastmillis = 0;  // time for interation the loop

void setup() 
{
  pinMode(TOUCH_PIN_IN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_PIN2, OUTPUT);
  Serial.begin(9600);
  
  // take readings of variables
     Particle.variable("pressure", &TOUCH_PIN_INReading, INT);
     Particle.variable("daylight", &photoCellReading, INT);
     Particle.variable("waterlevel", &moistVal, INT);
     Particle.function("LED Brightness",brightnessControl);
}
int brightnessControl(String command)
{
    ledBrightness=command.toInt();  // convert toiint val
     //  if( bright > 255 ) return -1; // take values only from 25-255
   //if( bright < 25 ) return -1;
    return 1;
}
void readData() {
    int proximity = digitalRead(TOUCH_PIN_IN); // Read the state of the sensor
    
        if (proximity == HIGH)  // when leaf is touched
        {
            for (int i=25 ; i<255 ; i=i+20) // glow or increase in brightness should be gradual
            {
                if(i<200){
        analogWrite(LED_PIN, i); // Turn the leaf on full glow
        delay(100);}
        else{
            analogWrite(LED_PIN, i);
            delay(2500); // stay on glow for some time
        }
            }
        }
        else if (photoCellReading<1500 && proximity == LOW)     // in absence of daylight, plant should start glowing dim
        {
        analogWrite(LED_PIN, ledBrightness); // Make leaf glow dim
        }
        else 
        {
        analogWrite(LED_PIN, 0); // Leaf does not need to glow during daytime
        }
}


void blinkLED2()    // function to blink leaf Blue 

{  digitalWrite(LED_PIN2, HIGH);
   delay(200);
  digitalWrite(LED_PIN2, LOW);
  delay(200);
}



void loop() 
{
    //Particle.function("LED Brightness",brightnessControl);
    if ((millis() - lastmillis) > 10) { // play with the millis
        lastmillis = millis();
        readData();
    }
    
     photoCellReading = analogRead(photoCellPin);
     TOUCH_PIN_INReading = analogRead(TOUCH_PIN_IN);
     
     //ledBrightness = analogRead(LED_PIN); // reading glow brightness
     moistVal = analogRead(moistPin);   // reading water requirements
     Serial.println(moistVal);

  delay(100);
  
  if (moistVal<2000) {
        digitalWrite(LED_PIN2, LOW);
  }else{
        blinkLED2(); 
  }
}
Click to Expand

Content Rating

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

0