Notifications
Clear all

Getting and reseting RTC without using the DS3231 library on the fly

8 Posts
4 Users
0 Likes
4,352 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

   
Quote
jscottbee
(@jscottbee)
Member
Joined: 5 years ago
Posts: 107
 

@pugwash nice.

Yeah, the DS3231 is one of the nice and easy i2c devices to play with and learn i2c protocol coding with.  I had a pic based outdoors lighting controller I did back in 2005 or 2006. The board is probably in a box somewhere now. 

Good coding,

Scott


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

This has whetted my appetite to start some bit bashing in the alarm registers!


   
ReplyQuote
(@abdulhak33m)
Member
Joined: 3 years ago
Posts: 7
 

@jscottbee Thanks. i have a problem using ds3231 module. i want it to trigger a relay for instance on monday January13, 2022 at 5.30am and put it off at 6.20pm. i modified a sketch from Eric Ayars library on github but im only able to get the hour functionality and nothing else. i cant program for years, months, days of week, minutes and seconds. kindly assist as i can provide you with my code


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@abdulhak33m 

Since you acknowledge that your code is the problem... please provide it along with your questions... that may encourage people to chime in and help, because they know the problem.

Cheers.


   
ReplyQuote
(@abdulhak33m)
Member
Joined: 3 years ago
Posts: 7
 

@frogandtoad Thank you so much for the response. I will post the code in the next slide along with the library and sketch i modified to arrive at this code. Please understand that the lines i commented were originally for the first project i did using this library. The task was to put off a fridge at 9:pm and put it on at 7:am. it worked perfectly. And because it was aimed at repeating this operation every day, there was nothing to test for other parameters like minutes, day of week, month of year and even seconds. So i assumed the code was fine. 

I then tried to adapt the same code for an irrigation system that turns on a dc pump on specific dates and times using a cropping calendar as a criteria. That was when i discovered that the code does not respond to the functions i mentioned; months, weeks, minutes and second.


   
ReplyQuote
(@abdulhak33m)
Member
Joined: 3 years ago
Posts: 7
 

/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal Lcd(10,9,5,4,3,2);

DS3231 clock;
bool century = false;
bool h12Flag;
bool pmFlag;
byte alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits;
bool alarmDy, alarmH12Flag, alarmPmFlag;
int relay1 =12;
int redled = 13;
int blueled = 11;
int greenled = 10;

void setup() {
// Start the I2C interface
Wire.begin();
pinMode(relay1, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
// Start the serial interface
Serial.begin(57600);

Lcd.begin(16,2);
Lcd.setCursor(0,0);
Lcd.print("Quickfix Eng ltd");

}

void loop() {
// send what's going on to the serial monitor.

// Start with the year
Serial.print("2");
if (century) { // Won't need this for 89 years.
Serial.print("1");
} else {
Serial.print("0");
}
Serial.print(clock.getYear(), DEC);
Serial.print(' ');

// then the month
Serial.print(clock.getMonth(century), DEC);
Serial.print(" ");

// then the date
Serial.print(clock.getDate(), DEC);
Serial.print(" ");

// and the day of the week
Serial.print(clock.getDoW(), DEC);
Serial.print(" ");

// Finally the hour, minute, and second
Serial.print(clock.getHour(h12Flag, pmFlag), DEC);
Serial.print(" ");
Serial.print(clock.getMinute(), DEC);
Serial.print(" ");
Serial.print(clock.getSecond(), DEC);

// Add AM/PM indicator
if (h12Flag) {
if (pmFlag) {
Serial.print(" PM ");
} else {
Serial.print(" AM ");
}
} else {
Serial.print(" 24h ");
}

for (clock.getYear()==21; clock.getYear()<=50; clock.getYear()==clock.getYear()+1){ //for all years before 2050
/* for(clock.getMonth(century)==1; clock.getMonth(century)<=12; clock.getMonth(century)== clock.getMonth(century)+1){ // for all 12 months
for(clock.getDoW()==1; clock.getDoW()<=7; clock.getDoW()== clock.getDoW()+1){ */ // for all days in a week

if(clock.getMonth(century)==9 || clock.getMonth(century)==10 || clock.getMonth(century)==11 || clock.getMonth(century)==12){
if(clock.getDoW()=="7"){
if(clock.getHour(h12Flag,pmFlag) == 01){
// if((clock.getMinute(), DEC)== 45){

digitalWrite(relay1, HIGH);
digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
Lcd.setCursor(0,1);
Lcd.print(" ");
Lcd.setCursor(0,1);
Lcd.print("WATERING");

}
}
}
}

 

 

if(clock.getMonth(century)==9 || clock.getMonth(century)==10 || clock.getMonth(century)==11 || clock.getMonth(century)==12){
if(clock.getDoW()=="7"){
if(clock.getHour(h12Flag,pmFlag) == 02){
// if((clock.getMinute(), DEC)== 47){

digitalWrite(relay1, LOW);
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
Lcd.setCursor(0,1);
Lcd.print(" ");
Lcd.setCursor(0,1);
Lcd.print("NOT WATERING");

}
}
}
}

 

 

 

/*if (clock.getHour(h12Flag,pmFlag) == 22 | ){
digitalWrite(relay1, HIGH);
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
Lcd.setCursor(0,1);
Lcd.print(" ");
Lcd.setCursor(0,1);
Lcd.print("FRIDGE OFF TIME");
}*/

/* if (clock.getHour(h12Flag,pmFlag) == 7 ||clock.getHour(h12Flag,pmFlag)==8 || clock.getHour(h12Flag,pmFlag)==9 || clock.getHour(h12Flag,pmFlag)==10 || clock.getHour(h12Flag,pmFlag)== 11 ||clock.getHour(h12Flag,pmFlag)== 12 || clock.getHour(h12Flag,pmFlag)== 13 || clock.getHour(h12Flag,pmFlag)== 14 || clock.getHour(h12Flag,pmFlag)== 15 || clock.getHour(h12Flag,pmFlag)==16 || clock.getHour(h12Flag,pmFlag)== 17 || clock.getHour(h12Flag,pmFlag)== 18 || clock.getHour(h12Flag,pmFlag)== 19 || clock.getHour(h12Flag,pmFlag)== 20){
digitalWrite(relay1, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
Lcd.setCursor(0,1);
Lcd.print(" ");
Lcd.setCursor(0,1);
Lcd.print("FRIDGE ON TIME");
}*/

// if (clock.getHour(h12Flag,pmFlag) >= 21 ||clock.getHour(h12Flag,pmFlag)<=7){
//digitalWrite(redled, HIGH);
//digitalWrite(greenled, LOW);
// digitalWrite(blueled, LOW);
// }

//if (clock.getHour(h12Flag,pmFlag) >= 7 && clock.getHour(h12Flag,pmFlag)<=17){
// digitalWrite(redled, LOW);
// digitalWrite(greenled, HIGH);
// digitalWrite(blueled, LOW);
// }

// if (clock.getHour(h12Flag,pmFlag) >= 17 && clock.getHour(h12Flag,pmFlag)<=21){
// digitalWrite(redled, LOW);
// digitalWrite(greenled, LOW);
// digitalWrite(blueled, HIGH);


   
ReplyQuote
(@abdulhak33m)
Member
Joined: 3 years ago
Posts: 7
 

The library used was by Eric Ayars. it has examples for set time, test time, echo time, and oscillator test. 

i will upload the different codes for these tasks here

 

This post was modified 2 years ago by abdulhak33m

   
ReplyQuote