how to combine PIR Sensor with RTC

Hello, i need help for my project. I am trying to combine coding for pir sensor and rtc (real time clock). The problem is the program does not read the rtc time correctly. i need help! this is an urgent project. help!!
This is the Pir Sensor Code
This is the rtc code
This is the combine code. I know is not that neat and tidy. But there still nothing wrong during the compiling.
Now the problem is as shown in the picture below. it does not detect the correct date and time.
I really need ur help. You may also correct me if there's anything wrong with the coding. Its a process of learning for me. But still i need to finish my project asap! waaaa!!! :'(( I really appreciate ur help!
This is the Pir Sensor Code
- CODE: SELECT_ALL_CODE
int calibrationTime = 30; //time for sensor to calibrate 30s
int ledPin = 13; //choose the pin for the LED
int inputPin = 2; //choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected.
int val = 0; //variable for reading the pin status
int pinSpeaker = 10; // set up speaker
long unsigned int pause = 5000;
void setup(){
pinMode(ledPin, OUTPUT); //declare LED as Output
pinMode(inputPin, INPUT); //declare PIR Sensor as Input
pinMode(pinSpeaker, OUTPUT); //declare Speaker as Output parallel to LED
Serial.begin(9600);
//PIR Sensor will calibrate for 30s first before start using it
Serial.print("Calibrating Sensor");
for(int i=0 ; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.print("done");
Serial.print("SENSOR ACTIVE");
delay(50);
}
void loop(){
val = digitalRead(inputPin); //read Input value
if (val == HIGH){ //check if the Input is HIGH
digitalWrite(ledPin, HIGH); //turn LED ON
playTone(300,160); //turn the Speaker ON
delay (150);
if (pirState == LOW){ //motion detected at what time
Serial.println("Motion detected!");
Serial.println("Motion detected at");
Serial.println(millis()/1000);
Serial.println("sec");
delay(50);
pirState = HIGH;
}
}
else{
digitalWrite(ledPin, LOW); //turn LED OFF
playTone(0,0);
delay(300);
if(pirState == HIGH){ //motion ended at what time
Serial.println("Motion ended!");
Serial.println("Motion ended at");
Serial.println((millis() - pause)/1000);
Serial.println("sec");
delay (50);
pirState = LOW;
}
}
}
//duration in mSecs, freuency in hertz
void playTone(long duration, int freq){
duration *= 1000;
int period = (1.0/freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration){
digitalWrite(pinSpeaker, HIGH);
delayMicroseconds(period/2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period/2);
elapsed_time += (period);
}
}
This is the rtc code
- CODE: SELECT_ALL_CODE
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}
void loop(){
printDate();
delay(1000);
}
void setDateTime(){
byte second = 30; //0-59
byte minute = 16; //0-59
byte hour = 16; //0-23
byte weekDay = 2; //1-7
byte monthDay = 8; //1-31
byte month = 3; //1-12
byte year = 13; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
This is the combine code. I know is not that neat and tidy. But there still nothing wrong during the compiling.
- CODE: SELECT_ALL_CODE
#include "Wire.h"
#include "RTClib.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00;
int calibrationTime = 30;
int ledPin = 13;
int inputPin = 2;
int pirState = LOW;
int val = 0;
int pinSpeaker = 10;
long unsigned int pause = 5000;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
Serial.println("Calibrating Sensor");
for(int i=0 ; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" ");
Serial.println("done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop(){
val = digitalRead(inputPin);
if (val == HIGH){
digitalWrite(ledPin, HIGH);
playTone(300,160);
delay (50);
if (pirState == LOW){
Serial.println("Motion detected!");
Serial.println("Motion detected at");
rtc(); //Serial.print(millis()/1000);
//Serial.println("sec");
delay(50);
pirState = HIGH;
}
}
else{
digitalWrite(ledPin, LOW);
playTone(0,0);
delay(50);
if(pirState == HIGH){
Serial.println("Motion ended!");
Serial.println("Motion ended at");
rtc(); //Serial.print((millis() - pause)/1000);
//Serial.println("sec");
delay (50);
pirState = LOW;
}
}
}
void playTone(long duration, int freq){
duration *= 1000;
int period = (1.0/freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration){
digitalWrite(pinSpeaker, HIGH);
delayMicroseconds(period/2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period/2);
elapsed_time += (period);
}
}
void rtc(){
setDateTime(); //MUST CONFIGURE IN FUNCTION
printDate();
delay(1000);
}
void setDateTime(){
byte second = 30; //0-59
byte minute = 01; //0-59
byte hour = 12; //0-23
byte weekDay = 1; //1-7
byte monthDay = 4; //1-31
byte month = 4; //1-12
byte year = 13; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
Now the problem is as shown in the picture below. it does not detect the correct date and time.
I really need ur help. You may also correct me if there's anything wrong with the coding. Its a process of learning for me. But still i need to finish my project asap! waaaa!!! :'(( I really appreciate ur help!