So as the title says i need help creating an alarm in my program I have a working lcd clock as of right now using a DS1307 tiny RTC and 1602 lcd both connected via I2c and everything's good but I cant seem to figure out how to create an alarm sequence. I need to have 4 separate alarms I don't need anything fancy I don't need to be able to adjust times or alarms manually or any of that it can all be written in the code and if the alarms need to be changed i can just re-upload the program. I basically just want it to turn pin 8 high and pin 7 low for 30 min and then turn pin 8 back to low and pin 7 back to high at 00:00 06:00 12:00 18:00.
is there an easy way to do this? still a major noob and struggling with my programming so sorry if this is a terribly stupid question.
here's my current code
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Wire.h>
RTC_DS1307 rtc; // Create a realtime clock called rtc
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display // Create an LCD called lcd
DateTime now;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
Wire.begin();
}
void loop(){
now = rtc.now(); // Get the current time
// Refresh the display
updateDisplay();
}
void updateDisplay(){
int h = now.hour(); // Get the hours right now and store them in an integer called h
int m = now.minute(); // Get the minutes right now and store them in an integer called m
int s = now.second(); // Get the seconds right now and store them in an integer called s
lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row...
lcd.print(" The time is: "); // ...with spaces to clear characters from setting alarm.
lcd.setCursor(4, 1); // Move the cursor to column four, lower row
if (h<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(h); // Display the current hour
lcd.setCursor(6, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(7, 1); // Move to the next column
if (m<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(m); // Display the current minute
lcd.setCursor(9, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(10, 1); // Move to the next column
if (s<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(s); // Display the current second
}
int h = now.hour(); // Get the hours right now and store them in an integer called h
int m = now.minute(); // Get the minutes right now and store them in an integer called m
int s = now.second(); // Get the seconds right now and store them in an integer called s
The comments are useless, since they state the obvious. However, they indicate that you, or whoever wrote the code, understand that h.m, and s, contain hours, minutes, and seconds values for now.
To trigger an event when the combination of now and the event time match:
int hEvent = 20;
int mEvent = 10:
int sEvent = 5;
bool triggered = false;
if(h == hEvent && m == mEvent && s == sEvent && !triggered)
{
// Do whatever needs doing
triggered = true;
}
else
triggered = false;
You could, of course, make hEvent, mEvent, and eEvent arrays with multiple start (or are they end?) times, and test them in a loop.
Thanks that's just what I needed but now I have another question which I think is totally stupid but for some reason I can't figure it out and haven't been able to find the info I need by googling.
when the program starts up I have 2 outputs for relays and they both go HIGH but I want one to be LOW and one to be HIGH
once it calls up the first if statement and changes the pins there its fine but until that time both pins are HIGH
here's my current code
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <Wire.h>
RTC_DS1307 rtc; // Create a realtime clock called rtc
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display // Create an LCD called lcd
DateTime now;
const int relay1Pin = 7;
const int relay2Pin = 8;
int relay1PinState = LOW;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
Wire.begin();
pinMode( relay1Pin, OUTPUT);
pinMode( relay2Pin, OUTPUT);
}
void loop(){
now = rtc.now(); // Get the current time
// Refresh the display
updateDisplay();
int h = now.hour(); // Get the hours right now and store them in an integer called h
int m = now.minute(); // Get the minutes right now and store them in an integer called m
int s = now.second(); // Get the seconds right now and store them in an integer called s
int hEvent = 10;
int mEvent = 16;
int hEvent2 = 10;
int mEvent2 = 17;
bool triggered = false;
if(h == hEvent && m == mEvent && !triggered) {
digitalWrite(relay1Pin, HIGH);
digitalWrite(relay2Pin, LOW);
triggered = true;
}
else {
triggered = false;
}
if(h == hEvent2 && m == mEvent2 && !triggered) {
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, HIGH);
triggered = true;
}
else {
triggered = false;
}
}
void updateDisplay(){
int h = now.hour(); // Get the hours right now and store them in an integer called h
int m = now.minute(); // Get the minutes right now and store them in an integer called m
int s = now.second(); // Get the seconds right now and store them in an integer called s
lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row...
lcd.print(" The time is: "); // ...with spaces to clear characters from setting alarm.
lcd.setCursor(4, 1); // Move the cursor to column four, lower row
if (h<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(h); // Display the current hour
lcd.setCursor(6, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(7, 1); // Move to the next column
if (m<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(m); // Display the current minute
lcd.setCursor(9, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(10, 1); // Move to the next column
if (s<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(s); // Display the current second
}
when the program starts up I have 2 outputs for relays and they both go HIGH but I want one to be LOW and one to be HIGH
In addition to writing to them, as AWOL suggests, you may need pulldown resistors, to assure that the relay does not go HIGH before you can control it.