Door Open
18 hour(s), 36 minute(s)
Light On // here is where the trouble is I would like it to say Door Open Light On.
18 hour(s), 36 minute(s)
Light On

The reason it's not working the way you want is because OnMinLight and OnMinDoor are initialized to different values. So the first pass through the loop it prints "Door Open" (because OnMinDoor = 20) then drops out of the loop and starts over. Then when OnMinLight = 22 it prints "Light On". Try changing OnMinLight =20 and OnMinDoor = 20.
 
The reason it's not working the way you want is because OnMinLight and OnMinDoor are initialized to different values. So the first pass through the loop it prints "Door Open" (because OnMinDoor = 20) then drops out of the loop and starts over. Then when OnMinLight = 22 it prints "Light On". Try changing OnMinLight =20 and OnMinDoor = 20.

thanks i'll try that.
 
The reason it's not working the way you want is because OnMinLight and OnMinDoor are initialized to different values. So the first pass through the loop it prints "Door Open" (because OnMinDoor = 20) then drops out of the loop and starts over. Then when OnMinLight = 22 it prints "Light On". Try changing OnMinLight =20 and OnMinDoor = 20.

Nevermind, that's not the right answer. You're going to have to figure out a way to put in an ELSE statement for when both door open and light on conditions are true.
 
here is a link to the right library - https://goo.gl/A7ER6U

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
 
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
 
I have a free second so I thought I'd explain the C if-else statement.

if (true){
do some cool stuff;
}
else{
do some different stuff if the above is false;
}

The else statement never gets executed when if is true.

JT
 
So what language is that? I understand programming enough to understand what's going on, but it looks too intuitive (i.e., non-cryptic) to be C.
 
So what language is that? I understand programming enough to understand what's going on, but it looks too intuitive (i.e., non-cryptic) to be C.

The Arduino language is merely a set of C/C++ functions, I assume it's not cryptic because it's just an if else statement and some declarations and not playing with memory and bit shifting :>>.

JT
 

New posts New threads Active threads

Back
Top Bottom