Back to Parent

// Define pins
int fsrPin = A0;
int fsrReading = 0;
int photoCellPin = A1;
int photoReading = 0;
int piezoPin = D3;
int piezoPitch = 2000;

int redPin = D2;    
int greenPin = D3;  
int bluePin = D4;   

// store the time when you last published
int last_published = -1;
//calculate interval between now and last_published
int interval=-1;
//store the time last fsr
int fsr_press=-1;
//calculate interval between now and fsr_press
int fsr_interval=-1;

int definedInterval=14400000; //4 hours
int maxNoCare=28800000;//8 hours

void setup() {
    
    pinMode( redPin, OUTPUT );
    pinMode( greenPin, OUTPUT );	
    pinMode( bluePin, OUTPUT );
    pinMode(piezoPin, OUTPUT);
    
    pinMode(fsrPin, INPUT); // MUSThave if used with variables other than direct readings
    pinMode(photoCellPin, INPUT);

    Particle.variable("fsr", &fsrReading, INT);
    Particle.variable("light", &photoReading, INT);
    //Particle.variable("r", &redValue );
    //Particle.variable("g", &greenValue );
    //Particle.variable("b", &blueValue);
}

void loop() {
    
    photoReading=analogRead(photoCellPin);
    fsrReading=analogRead(fsrPin); 
    
    interval = millis()-last_published;
    fsr_interval = millis()-fsr_press;
    
    if (fsrReading > 1500) 
    {
        fsr_press=millis();
    }
    
    //light and sound
    
    //Red light: In danger! Light turns red when the owner has not devoted attention to Plet for a long time. (assuming that no watering)
    if (fsr_interval > maxNoCare)
    {
        analogWrite(greenPin, 0);
        analogWrite(bluePin, 0); //make sure the other colors are 0
        for (int i; i<3; i++)
        {
            analogWrite(redPin, 255);
            tone(piezoPin,2000);
            delay(200);
            analogWrite(redPin, 0);
            noTone(piezoPin);
            delay(200);
        }
    }
    
    //Green light: It's growing! The lighting is suitable and Plet receives appropriate attention.
    else if (photoReading < 3500 && photoReading > 2500)
    {
        analogWrite(greenPin, 255);
        analogWrite(redPin, 0);
        analogWrite(bluePin, 0);
    }
    
    //Blue light: Now feeling sad
    else if (photoReading<1000 && fsr_interval > definedInterval)
    {
        analogWrite(bluePin, 255);
        analogWrite(redPin, 0);
        analogWrite(greenPin, 0);
    }
    
    //Purple light: Feels happy. Purple light flickers when the owner tends to Plet and also when the Piezo plays a tune. 
    else if (photoReading>1000 && fsr_interval < definedInterval) 
    {
        analogWrite(redPin, 255);
        analogWrite(bluePin, 255);
        analogWrite(greenPin, 0);
        for (int i; i<3; i++)
        {
            tone(piezoPin,2000);
            delay(100);
            noTone(piezoPin);
            delay(100);
        }
    }
    else //fall back
    {
        analogWrite(redPin, 0);
        analogWrite(bluePin, 0);
        analogWrite(greenPin, 0);
    }
    
    //publish event
    if (fsrReading >1500)
    {
        if (photoReading < 3500 && photoReading > 1500)
        {
            Particle.publish( "fsr_grow", "grow" );
            last_published=millis();
        }
        else
        {
            Particle.publish( "fsr_sad", "sad" );
            last_published=millis();
        }
    }
    else if (photoReading<1000 && interval > definedInterval)
    {
        Particle.publish( "fsr_sad", "sad" );
        last_published=millis();
    }
    else if (photoReading>1000 && interval > definedInterval)
    {
        Particle.publish( "fsr_happy", "happy" );
        last_published=millis();
    }
    
    delay(10000);
 }
Click to Expand

Content Rating

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

0