I started this from a sketch where a PIR detector turns on a light and after 30 seconds the light goes out. This works. Earlier this week I bought a little DS1307 module and hooked it up through I2C to my Arduino Uno. Now I want to only turn on the light in the evening (lets say after 18:00) and if the PIR detector is high. For the DS1307 module I used some code I found here -> http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
I can read the time from the DS1307 (serial monitor).
The only bit that doesn't work is this: "if (pirState == HIGH && (hour, DEC) >= 18)" Do I need to convert the "hour"?
Could someone give me some pointers on how I can accomplish this?
This is the complete sketch:
/*
http://www.bajdi.com
PIR detector turns on light, light switches automatically off after 30 seconds.
Light must only turn on in the evening (after 18:00)
*/
#include <Wire.h>
const int DS1307_I2C_ADDRESS = 0x68;
const int pir = 2; // the number of the PIR pin
const int light = 13; // the number of the light pin
unsigned long on; //start time for pir light on
int pirState = 0; // variable for reading the pushbutton status
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
}
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(light, OUTPUT);
pinMode(pir, INPUT);
}
void loop(){
getDateDs1307();
Serial.println(" ");
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.println(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.println(year, DEC);
delay(2000); //slow down serial monitor for testing
pirState = digitalRead(pir);
if (pirState == HIGH && (hour, DEC) >= 18) {
digitalWrite(light, HIGH);
on = millis();
}
unsigned long currentMillis = millis();
if ((currentMillis - on) > 30000) {
digitalWrite(light, LOW);
}
}