What a horrible video lol. I like to start with the built in libraries for the Arduino or use the Adafruit libraries. I assume you set the time somehow?


So looking at your code and the door for example will only be on for 1 minute and the light will only be on for 1 minute and not at the same time. I'll work up and example in a bit.

In case you can't tell I'm a morning person lol and don't get on here much after 7am.

JT

yes they have an example library were you unquote time and date and set it.
 
For this example I used the Adafruit RTClib library from this page.
Now a few things to note about your code:
  • You didn't not initialize the output using pinMode()
  • You didn't set the initial state of the output in the setup()
  • You will only see the print line during the minute
  • You didn't have any inputs configured to sense the door is open/closed and turn off the relay
I did a sketch (what a weird name for a program) to demonstrate turning on and off the door relay. In the setup I initialize the output pin and set it's output to LOW (Off). Then in the loop() I have two if statements one to turn on the door relay and one to turn off the relay (not really a practical solution but OK for an example). Then there is a third if statement that reads the state of the output and prints if it is on. When you run this code you can see the state of output no matter what time it is. You could also add an else to say it's off.

Code:
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;
int DoorRelay = 4;
int LightRelay = 5;

const int DoorOpenHour = 6;
const int DoorOpenMinute = 35;
const int DoorCloseHour = 6;
const int DoorCloseMinute = 40;

const int OnHourLight = 6;
const int OnMinLight = 12;
const int OffHourLight = 8;
const int OffMinLight = 28;

void setup () {
Serial.begin(115200);

delay(3000); // wait for console opening

if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// initilize the output and set it's state
pinMode(DoorRelay, OUTPUT);
digitalWrite(DoorRelay, LOW);
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.println(now.minute(), DEC);
if (now.hour() == DoorOpenHour && now.minute() == DoorOpenMinute) {
digitalWrite(DoorRelay, HIGH);
}
if (now.hour() == DoorCloseHour && now.minute() == DoorCloseMinute) {
digitalWrite(DoorRelay, LOW);
}
if (digitalRead(DoorRelay) == HIGH) {
Serial.println("Door Relay On");
}
delay(3000);
}

JT

thanks for doing that i'll have to run that when i have some time.
 

New posts New threads Active threads

Back
Top Bottom