//This allows the use of Time (throughout the day)
#include "application.h"
// Define a pin to place the photoresistor on
int photoCellPin = A0;
// Create a variable to hold the light reading
int photoCellReading = 0;
// Define a pin to place an LED on
int ledPin1 = D2;
// Create a variable to store the LED brightness.
int ledBrightness1 = 0;
// Create a variable to store the photoresistor reading
int brightnessReading = 0;
// Define a pin to place the FSR on
int flexPin = A1;
// Create a variable to hold the FSR reading
int flexReading = 0;
// Create a variable to store the LED brightness.
int ledBrightness2 = 0;
// Create a vairable for speaker
int speakerPin = D4;
// Create boolean variables to define if brightness has changed for the first time or has been consistently dark/bright
bool brightFrequency = false;
bool songPlaying = false;
//Define notes for melody
const int NOTE_C4 = 262;
const int NOTE_G4 = 392;
const int NOTE_A4 = 440;
// Define melody
int melody[] = {
NOTE_C4, NOTE_G4, NOTE_A4 };
//Define note durations
int noteDurations[] = {
2, 2, 1};
void setup()
{
// Set up the LED for output
pinMode(ledPin1, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
// Create a cloud variable of type integer
// called 'force' mapped to flexReading
Particle.variable("force", &flexReading, INT);
pinMode( speakerPin, OUTPUT );
}
void loop()
{
// Use analogRead to read the photo cell reading
// This gives a value from 0 to 4095
photoCellReading = analogRead(photoCellPin);
// Map this value into the PWM range (0-255)
// and store as the led brightness
brightnessReading = map(photoCellReading, 0, 4095, 0, 255);
ledBrightness1 = 0;
//Set time zone to Eastern USA daylight saving time
Time.zone(-4);
if (Time.hour() > 2 and Time.hour() < 18){
//Make LED turn on if there is no sufficient light between 7:00 am and 5:59 pm
//Make it turn off if there is sufficient light
//Turn it off otherwise, between 6:00 pm and 6:59 am
if( brightnessReading > 0 and brightnessReading <= 127.5){
ledBrightness1 = 255;
} else if (brightnessReading > 127.5 and brightnessReading >= 255){
ledBrightness1 = 0;
}
}
// fade the LED to the desired brightness
analogWrite(ledPin1, ledBrightness1);
if (Time.hour() > 2 and Time.hour() < 18){
//during 7 am and 6 pm: if there is no sufficient light, turn on light melody (ONCE)
// if there is sufficient light turn on dark melody (ONCE)
if(brightFrequency== false and brightnessReading <= 127.5){
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
songPlaying = true;
// 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/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.5;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
//set variables as true/false when room is consistently dark
songPlaying = false;
brightFrequency = true;
}else if (brightnessReading > 127.5){
// reset variable when the room gets dark
brightFrequency = false;
}
}
// wait 1 second and then loop
delay(1000);
// Use analogRead to read from the sensor
// This gives a value from 0 to 4095
flexReading = analogRead(flexPin);
plant_status();
}
// send notification when plant starts to wilt
void plant_status(){
// if flex sensor bends slightly then send message warning
if( flexReading >500 and flexReading <= 800){
Particle.publish("plant_status", "Uh oh! It looks like the plant is starting to wilt.");
}
// if flex sensor bends more, send another more urgent warning
else if( flexReading > 300 and flexReading <= 500){
Particle.publish("plant_status", "Oh no! We need help! The plant is wilting");
}
// if flex sensor bends drasticaly, send warning that plant stem has broken off
else if(flexReading > 0 and flexReading <= 300){
Particle.publish("plant_status", ":O It's too late! The plant stem has broken off!");
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .