Triggering DS 3231 clock

Thank You guys upfront you have help me along the way on this project. This is a great forum with very knowledgeable members

I have two relays that I need to trigger on and off at various times during the day. The clock os working and I am reading thur the serial port. Both relays are working on and off. I am using an esp32 as a micro controller and the Arduino IDE as the language. I am having trouble getting the triggers in the code I am really lost on this. Please help me set these triggers up. Thank You for the help

Sprinkfitter :slight_smile:

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68

const int relay1= 2;//Digital pin that the Relay is connected
const int relay2= 5;//Digital pin that the Relay is connected

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);

digitalWrite(relay1,LOW); //OFF
digitalWrite(relay2,LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second

digitalWrite(relay1,HIGH); //turns the relay OFF
delay(3000); //waits for a second
digitalWrite(relay1,LOW); //turns the relay ON 
delay(3000); //waits for a second

digitalWrite(relay2,HIGH); //turns the relay OFF 
delay(3000); //waits for a second
digitalWrite(relay2,LOW); //turns the relay ON 
delay(3000); //waits for a second
}

So is what you have set up there for relay1 and relay2 just a relay test? You can detect a change in time unit by comparing it with a previous read to see if it has changed. Thus you can detect a change of second, minute, hour and so on.

This was just a test to make sure the relays were working. I need to call I guess an if and when statement to set the relay to trigger at a certain hour min and sec. The relay will stay close for hours and at a certain time it opens and at other time the second relay comes on and stays open for some hours and then closes. This is a pump with 2 speed and I want to control the speeds at certain times. I hope that helps and thanks for the feedback.

sprinkfitter

Well then, why not start simple? Test to see when the minute has changed, and operate one of the relays once a minute. When you get that working you can see how it works and finish the fancy stuff.

Pseudocode:
Read the minute.
If the minute is different than the previous minute,
{
record the previous minute (make it now).
toggle the relay
}
Repeat.

What "coding errors", and what do you mean by that?

... and WTH is this?

{else}

Where on earth have you ever seen that in C code? Really, you just need to study C/C++ syntax.

Thank You for making that obvious point. Very new to this will go back over the my coding. Thanks for your help.

Sprinkfitter

I am still getting coding errors.... Maybe tomorrow will be better. I can not find any libraries RTC with the esp32. Thanks for all the help

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 12;
const int OnMin1 = 25;
const int OffHour1 = 12;
const int OffMin1 = 25;


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, LOW); //OFF
  digitalWrite(relay2, LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second


  if (readDS3231time (byte hour) == Onhour1 && (readDS3231time (byte min) == OnMin1)
  {
    digitalWrite (Relay1, HIGH);
  }
  else if (readDS3231time byte hour) == OffHour1 && (readDS3231time byte min == OffMin)
  {
    digitalWrite (Relay1, HIGH);
  }

}
)
)
)

You still have not explained what you mean by "coding errors". It is not a common term. You say you are new to coding. Take some time to play with some simpler programs such as the IDE examples so that you can begin to grasp the meaning of what you are typing. Although you call that suggestion "obvious", you don't seem eager to pursue it.

Learn to see "obvious" things like the abandoned ")" characters at the end of your code that are completely outside of C syntax.

Coding errors = compile/verified errors.
exit status 1
expected primary-expression before 'hour'
I chose this project knowing it would be a tough one but I thought the rewards would be many.
Yes I am a beginner and very much aware that I have a lot to learn. I have been given help in other forums and was very much appreciative when someone shares their knowledge with me.
This is my hobby thank god I am not doing this for money. I am a retired Fire Protection Engineer and find these little devices fascinating. Yes I have not completely pick up the jargon from this field.
I do make errors with where the () and ;; and other little what nots go. LOL Thanks for all your help..

Sprinkfitter

I am having trouble getting the triggers in the code I am really lost on this.

The above quote is extracted from the OP.

I have been reading this line since the post is made; unfortunately, I have not yet understood what the OP has wanted to mean by that sentence. May I request to have clarification of it.

  if (readDS3231time (byte hour) == Onhour1 && (readDS3231time (byte min) == OnMin1)

The readDS3231time() function takes 7 arguments - addresses of existing variables that it can write to - NOT 1 new variable.

I need to address each argument by a separate function. byte hour, byte min, byte sec and so on.

What you need is to set up 7 variables for the 7 parameters to readDS3231time(). Then you call readDS3231time() passing it the names of your variables and readDS3231time() puts all the current values into the variables for you.

And then you use those variables in your if statements.

Steve

GolamMostafa:
The above quote is extracted from the OP.

I have been reading this line since the post is made; unfortunately, I have not yet understood what the OP has wanted to mean by that sentence. May I request to have clarification of it.

Triggers = To write the relay HIGH by using the hours and minutes from the ds3231 clock in the Arduino IDE. Lost = Stumbling around my office and scratching my head trying to understand how to structures this Arduino Code and making it work. I hope this helps clarified what I am doing and trying to do

Sprinkfitter

Lost = Stumbling around my office and scratching my head trying to understand how to structures this Arduino Code and making it work.

If you KNOW that the readDS3231time() function needs 7 arguments, why are you trying to call it with one? How is it supposed to know which of the 7 values you want?

byte m, h, s, w, mo, d, y; 
readDS3231time(m, h, s, w, mo, d, y);
if(h == Onhour1 && m == OnMin1)
{
   // Hey, lookee there, it's party time.

Why is the M in OnMin1 capitalized when the h in Onhour1 isn't?

PaulS:
If you KNOW that the readDS3231time() function needs 7 arguments, why are you trying to call it with one? How is it supposed to know which of the 7 values you want?

byte m, h, s, w, mo, d, y; 

readDS3231time(m, h, s, w, mo, d, y);
if(h == Onhour1 && m == OnMin1)
{
  // Hey, lookee there, it's party time.




Why is the M in OnMin1 capitalized when the h in Onhour1 isn't?

Now Paul that looks to easy... I wonder why I could not see that??? Has to be wrong!!! But hell Paul called party time going to get the good stuff out. Hello Woodford Reserve!!!! I little Kentucky Bourbon will help I am sure.

Why is the M in OnMin1 capitalized when the h in Onhour1 isn't? Maybe to much KY Bourbon.
Thanks for the help.

Sprinkfitter

PaulS:

byte m, h, s, w, mo, d, y; 

readDS3231time(m, h, s, w, mo, d, y);
if(h == Onhour1 && m == OnMin1)
{
  // Hey, lookee there, it's party time.

I guess you're right PaulS, at some point it's easier just to write the code for 'em rather than carry on trying to explain things.

Steve

slipstick:
I guess you're right PaulS, at some point it's easier just to write the code for 'em rather than carry on trying to explain things.

Steve

Now that is not nice. Steve. Humm must had a back channel conversation going on about the new guy. Oh well I am very glad I got some help on my project. I kinda thought that what makes the forums great. Hell you never know the old new guy could help someone in the future.
A little background.. My little 6 year old grandson helps with the soldering task and the 12 year old is learning programming too. So you never know how many people you are helping.
The wealth of knowledge on this forum is staggering. Again I thank you for your time and knowledge.

Sprinkfitter

The working sketch
Thanks Big

#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 15;
const int OnMin1 = 6;
const int OffHour1 = 15;
const int OffMin1 = 8;


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, LOW); //OFF
  digitalWrite(relay2, LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)

{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  if (hour == OnHour1 && minute == OnMin1) {
    digitalWrite (relay1, HIGH);
  }
  else if (hour == OffHour1 && minute == OffMin1) {
    digitalWrite (relay1, LOW);
  }

}




[b][/b]