Code
//Want to set a variable for photoCell and set it to map to A0
int photoCellPin = A0;
//Create a varibale to store readings from the photoCell
int photoCellReading = 0;
//Set a varible for LED that maps to D0
int ledPin = D0;
//threshold of being covered
int threshold = 3000;
//Create a varible that will track and store brightness of the LED
int ledBrightness = 0;
//Define LED on pin D1
int ledPIR = D1;
//Define the PIR sensor on D7;
int PIR = D7;
//store PIR readings
int PIRReading = 0;
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int calibrateTime = 1000; // wait for calibration
//track publishing process
bool published;
long publishTimeout = 60000;
long lastPublishTime = -60000;
void setup ()
{
//Configure D0 pin as output
pinMode (ledPin, OUTPUT);
//create a cloud variable interger called light that is mapped to the photoCellReading
Particle.variable("light", &photoCellReading, INT);
Serial.begin(9600);
// set ledPIR pin to output
pinMode(ledPIR, OUTPUT);
//set ledPIR pin to input
pinMode(PIR, INPUT);
//ledPIR is on
digitalWrite(ledPIR, HIGH);
//ledPir is off
digitalWrite(ledPIR, LOW);
// set the timezone to EST!!!
Time.zone( -5 );
Serial.begin( 9600) ;
}
void loop ()
{
//Use the analogRead to provide photoCellReading
photoCellReading = analogRead(photoCellPin);
if(photoCellReading < threshold){
digitalWrite(ledPin, HIGH);
}else{
digitalWrite(ledPin, LOW);
}
Serial.print("Analog reading = ");
Serial.println(photoCellReading);
//Make LED brighter, the darker the sensor is
photoCellReading = 4095 - photoCellReading;
//ledBrightness = map( photoCellReading, 0, 4095, 0 , 255 );
//fade LED to desired brightness
//analogWrite(ledPin, ledBrightness);
//wait for x seconds and then loop
//delay(100);
//motion detection, then turn LED on as an indicator
// if the sensor is calibrated
if ( calibrated() )
{
Serial.println( "calibrated and reading" ) ;
// get the data from the sensor
readTheSensor();
// report it out, if the state has changed
reportTheData();
}
//wait for 5 seconds delay before reading again
delay(500);
}
void readTheSensor() {
val = digitalRead(PIR);
}
bool calibrated() {
return millis() - calibrateTime > 0;
}
void reportTheData() {
// if the sensor reads high
// or there is now motion
if (val == HIGH) {
Serial.println( "there's motion" ) ;
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an eent
if (pirState == LOW) {
Serial.println( "changed" ) ;
// we have just turned on
// Update the current state
pirState = HIGH;
if (Time.hour()>=6 && Time.hour()<=18 && (lastPublishTime + publishTimeout) < millis() ){
Serial.println( "announcing" ) ;
Particle.publish("kpanchma/s16/motion", "You've Got Mail");
lastPublishTime = millis();
}
}
} else {
if (pirState == HIGH) {
Serial.println( "used to be high" ) ;
// we have just turned of
// Update the current state
pirState = LOW;
}
}
digitalWrite( ledPIR , pirState );
}
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. .