Here is your sketch modified to work with the DateTime library. If you run the SetArduinoClock Processing sketch supplied in the DateTime download you can click the window to set the time. You should see the serial output in the Processing serial monitor. Note the default baud rate is 19200 so I have changed the Arduino setup to match.
If the time is not synchronized then the clock will start at midnight Jan 1 2009.
Also note that the Processing sketch defaults to using the first com port on you PC, you may need to change the portIndex variable in the Processing sketch if your arduino is connected to a different port.
#include <DateTime.h>
#include <DateTimeStrings.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 255 // Header tag for serial time sync message
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 60;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
long unsigned int lowIn2;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
/////////////////////////////
//Sensor A
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
/////////////////////////////
//Sensor B
boolean lockLow2 = true;
boolean takeLowTime2;
/////////////////////////////
//Sensor A
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 12; //Extra LED
/////////////////////////////
//Sensor B
int pirPinB = 2; //the digital pin connected to the PIR sensor's output
int ledPinB = 11; //On Board LED
/////////////////////////////
//SETUP
void setup(){
Serial.begin(19200); // changed to 19200 to match the baud rate of the Processing sketch
DateTime.sync(1230768000); // set Jan 1 2009 as the default time
/////////////////////////////
//Sensor A
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
/////////////////////////////
//Sensor B
pinMode(pirPinB, INPUT);
pinMode(ledPinB, OUTPUT);
digitalWrite(pirPinB, LOW);
//give the sensor some time to calibrate
Serial.print("Calibrating PIR Sensors A & B | 60 Seconds ");
for(int i = 0; i < calibrationTime; i++){
Serial.print("*");
delay(1000);
}
Serial.println(" Finished");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
getPCtime(); // set the time if a time sync message is available on the serial port
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
printEvent("Sensor A", "Motion detected", DateTime.now());
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause ){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
printEvent("Sensor A", "Motion ended", DateTime.now() - pause/1000);
delay(50);
}
}
/////////////////////////////////
//Sensor B
if(digitalRead(pirPinB) == HIGH){
digitalWrite(ledPinB, HIGH); //the led visualizes the sensors output pin state
if(lockLow2){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow2 = false;
Serial.println("---");
printEvent("Sensor B", "Motion detected", DateTime.now());
delay(50);
}
takeLowTime2 = true;
}
if(digitalRead(pirPinB) == LOW){
digitalWrite(ledPinB, LOW); //the led visualizes the sensors output pin state
if(takeLowTime2){
lowIn2 = millis(); //save the time of the transition from high to LOW
takeLowTime2 = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow2 && millis() - lowIn2 > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow2 = true;
printEvent("Sensor B", "Motion ended", DateTime.now() - pause/1000);
delay(50);
}
}
}
void printEvent(char * sensorStr, char * eventStr, time_t time){
Serial.print(eventStr);
Serial.print(" on ");
Serial.print(sensorStr);
Serial.print(" at "); //output
printTimeDate(time);
Serial.println();
}
void printTimeDate(time_t time){
byte sec,min,hour,hr,day,wday,month,year;
DateTime.localTime(&time,&sec,&min,&hour,&day,&wday,&month,&year);
// the following converts 24 hour time to 12 hour time
if( hour == 0 )
hr = 12; // 12 midnight
else if( hour > 12)
hr = hour - 12 ;
else
hr = hour ;
Serial.print(hr,DEC);
printDigits(min);
printDigits(sec);
Serial.print(" ");
Serial.print( hour < 12 ? "AM" : "PM");
Serial.print(" ");
Serial.print(day,DEC);
Serial.print(" ");
Serial.print(DateTimeStrings.monthStr(month));
Serial.print(" ");
Serial.print(year + 1900,DEC);
}
void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
}
boolean getPCtime() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
if( Serial.read() == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
char c= Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
DateTime.sync(pctime); // Sync Arduino clock to the time received on the serial port
Serial.print("Clock set at ");
printTimeDate(DateTime.now());
return true; // return true if time message received on the serial port
}
}
return false; //if no message return false
}
a test run produced this output:
Clock set at 11:56:29 AM 9 February 2009
Motion detected on Sensor A at 11:56:34 AM 9 February 2009
Motion ended on Sensor A at 11:56:39 AM 9 February 2009