Back to Parent

//Light Sensor
int photoCellPin = A0;
int photoCellReading = 0;

//Tilt Sensor
int tiltPin = D6;
int LEDstate = HIGH;
int tiltreading;
int tiltprevious = LOW; 
long notetime = 0;
long debounce = 50;

//Monitoring LED 
int ledPin = D2;

//Amount of light RGB LED
int redPin = D3;    
int greenPin = D4;  
int bluePin = D5;  
#define COMMON_ANODE

//Time monitoring
int second = -1;
int totalday = 0;
int nolightday = 0;
int status = 0;
int hronvacation =0;
bool alreadyannounce_needvaca = FALSE;
bool alreadyannounce_backtowork = TRUE;
bool alreadyannounce_fall = FALSE;


void setup() {
    
    //Variable display
    Particle.variable("Light", &photoCellReading, INT);
    Particle.variable("CurrentSecond", &second, INT);
    
    Particle.variable("TotalDays", &totalday, INT);
    Particle.variable("NoLightDaysInARoll", &nolightday, INT);
    Particle.variable("PlantStatus", &status, INT);
    Particle.variable("HourOnVacation", &hronvacation, INT);
    Particle.variable("Tilt", &tiltreading, INT);
    
    //Set up LED to be output
    pinMode(ledPin, OUTPUT);
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    
    //tilt sensor setup
    pinMode(tiltPin,INPUT);
    digitalWrite(tiltPin, HIGH);


    digitalWrite(ledPin, LOW);  //turn off monitoring LED
    setColor(0, 0, 0); //turn off rgb led
    

}

void loop() {
    
//LIGHT MONITORING
    
photoCellReading = analogRead(photoCellPin);
second = Time.second();

    //Monitoring Light level through a week
    if(status == 0){
        
        if(alreadyannounce_backtowork == FALSE){
            Particle.publish( "Backtowork" );
            delay(500);
            alreadyannounce_backtowork = TRUE;
        }  
        
    
        //pretend that every 15 seconds = 1 day   
        if (Time.second() == 0 || Time.second() == 15 || Time.second() == 30 || Time.second() == 45){
            digitalWrite(ledPin, HIGH); //Monitoring LED blinks everytime it reads
            delay(1500);
            totalday++;
        
            if(photoCellReading < 1500){
                nolightday++;    
            }else{
                nolightday = 0;
            }
        
        }else{
            setColor(0, 0, 0); //turn off rgb led
            delay(500);
        }
    
        if(nolightday >= 2){
        status = 1;
        alreadyannounce_needvaca = FALSE;
        }
    }


    //The plant says I need a vacation
    if(status == 1){
    
        if(alreadyannounce_needvaca == FALSE ){
            Particle.publish( "NeedVacation" );
            delay(500);
            alreadyannounce_needvaca = TRUE;
        }    
        
        blink();
    
        if(photoCellReading <= 1500){
            setColor(0, 0, 255); //blue means too dim
            delay(500);
    
        }else if(photoCellReading > 1500 && photoCellReading < 3000){
            setColor(0, 255, 0); //green means proper amount the plant needs :-)
            hronvacation ++;
            delay(1500);
        
            if(hronvacation >=5){
            status = 0;
            alreadyannounce_backtowork = FALSE;
            hronvacation = 0;
            nolightday = 0;
            }
        
        }else if(photoCellReading >= 3000){
            setColor(255, 0, 0); //red means too strong light
            delay(500);
    
        }
    }


//TILT MONITORING
int switchstate;
 
  tiltreading = digitalRead(tiltPin);
 
  // If the switch changed, due to bounce or pressing...
  if (tiltreading != tiltprevious) {
        // reset the debouncing timer
        notetime = millis();
  } 
 
  if ((millis() - notetime) > debounce) {
        // whatever the switch is at, its been there for a long time
        // so lets settle on it!
        //switchstate = tiltreading;
 
        // Now invert the output on the pin13 LED
        if (tiltreading == HIGH){
            blink();
            
            if(alreadyannounce_fall == FALSE){
            Particle.publish( "Fall" );
            delay(500);
            alreadyannounce_fall = TRUE;
            } 
            
        }else{
            digitalWrite(ledPin, LOW);
            alreadyannounce_fall = FALSE;
        }
  }
  // Save the last reading so we keep a running tally
  tiltprevious = tiltreading;

}



void blink (){
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
}



void setColor(int red, int green, int blue){
    #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
    #endif
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);  
}
Click to Expand

Content Rating

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

0