Timer on minutes by RTC . Donne!

I try to make a timer with RTC in minutes, but if I use the data from RTC for example the minutes, they change automatically when time passes

void loop() {
displayTime();
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);

min_read = minute;

if ( (min_on + min_read) >= minute )
{
Serial.print("Light on");
Serial.print(" The value of min_on+min_read is:");
Serial.println(min_on + min_read);
}
else
{
Serial.print("Light off");
Serial.print(" The value of min_off+min_read is:");
Serial.println(min_off + min_read);
}
}

if I use the data from RTC for example the minutes, they change automatically when time passes

Yip, thats how clocks work. Explain what you want to do as your goal is not obvious.

I don't understand your question, but in your code

min_read = minute;

 if ( (min_on + min_read) >= minute )

surely min_on + min_read is always going to be greater than minute unless min_on is negative

Please post a complete sketch that shows the problem

my goal is to create a timer in minutes with rtc, that is, a relay open 3 minutes, and then stopped let say 15 min ...

now the idea came to me, to start from minute 0 and to create with IF all the possible situations during an hour

and bltw tnks for help guys :sweat_smile:

If you want the difference between two clock times it is often easiest to convert the hours minutes and seconds into a total number of seconds since midnight and then subtract one seconds value from the other.

If you need to do the calculation across midnight then calculate the number of seconds starting from (say) 1st January

...R

I'm sorry, my code is messy .... but it works, I finally found out how to make a timer per minute, for example 5 minutes on and 5 off, I hardly realized using a counter you can set the intervals, now I still have to solve the problem when we use for example 50 minutes on and 50 off, thus exceeding the limit of 60 ... I AM REFERRING TO RELAY4.

For small intervals example 1 on 1 of or 10 on 10 off, it works well ... someone more experienced than me could help me ... because I honestly got confused

#include <Wire.h>
#include <SPI.h>

#define DS3231_I2C_ADDRESS 0x68

byte DecToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
byte BcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

int relay4 = 9;

int relay4_start = 10;      // relay 4 is for the fan, it is based on minutes, how many minutes on or how many minutes off
int relay4_off = 20 ;        //

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   pinMode(relay4,OUTPUT);
   digitalWrite(relay4, HIGH);
  //setDS3231time(00,10,12,5,29,1,21);  //secs min hours day of week day of month month year
}

void loop() 
{
  displayTime();
  start_relay4();

}

void start_relay4()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);

int var=0;
int var2=0;  
int i;
int dif=0;

while (var<60)
{
  var = var+relay4_start;
  var2++;
  var=var+relay4_off;
}
  Serial.println(var2);

if( minute <=relay4_start )
  {
    digitalWrite(relay4,LOW);
    Serial.print("0");
    Serial.print(" ON ");
    Serial.println(relay4_start); 
  }
  else if(minute > relay4_start && minute< relay4_start+relay4_off)
  {
    digitalWrite(relay4,HIGH);
    Serial.print(relay4_start+relay4_off);
    Serial.print(" OFF ");
    Serial.println(2*relay4_start+relay4_off);
  }

for(i=1;i<var2;i++)
  {
  if(minute>=i*(relay4_start+relay4_off) && minute<(i*(relay4_start+relay4_off)+relay4_start))
  {
    digitalWrite(relay4,LOW);
    Serial.print(i*(relay4_start+relay4_off));
    Serial.print(" ON ");
    Serial.println(i*(relay4_start+relay4_off)+relay4_start);
  }
    else if(minute>=i*(relay4_start+relay4_off)+relay4_start && minute<(i+1)*(relay4_start+relay4_off))
   {
    digitalWrite(relay4,HIGH);
    Serial.print(i*(relay4_start+relay4_off)+relay4_start);
    Serial.print(" OFF ");
    Serial.println((i+1)*(relay4_start+relay4_off));
   }
  }
}
void displayTime()
  {

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);

  if (hour < 10)
    {
     Serial.print("0");
     Serial.print(hour,DEC); 
    }
    else
    {
     Serial.print(hour,DEC);  
    }
 Serial.print(":");   

    if ( minute < 10 )
    {
      Serial.print("0");
      Serial.print(minute,DEC); 
    }
    else
    {
     Serial.print(minute,DEC);
    }
    Serial.print(":"); 
  if( second < 10 )
    {
      Serial.print("0");
      Serial.print(second,DEC);
    }
    else
    {
      Serial.print(second,DEC);
    }
    Serial.print("  "); 
  switch ( dayOfWeek )
  {
    case 1:
    Serial.print("Mon");
    break;
    case 2:
    Serial.print("Tue");
    break;
    case 3:
    Serial.print("Wed");
    break;
    case 4:
    Serial.print("Thu");
    break;
    case 5:
    Serial.print("Fri");
    break;
    case 6:
    Serial.print("Sat");
    break;
    case 0:
    Serial.print("Sun");
    break;
  }
 Serial.print(",");
 if ( dayOfMonth < 10)
  {
    Serial.print("0");
    Serial.print(dayOfMonth,DEC);
  }
  else
  {
    Serial.print(dayOfMonth,DEC);
  }
 Serial.print("-");
 
 switch ( month )
  {
    case 1:
    Serial.print("Jan");
    break;
    case 2:
    Serial.print("Feb");
    break;
    case 3:
    Serial.print("Mar");
    break;
    case 4:
    Serial.print("Apr");
    break;
    case 5:
    Serial.print("May");
    break;
    case 6:
    Serial.print("Jun");
    break;
    case 7:
    Serial.print("Jul");
    break;
    case 8:
    Serial.print("Aug");
    break;
    case 9:
    Serial.print("Sep");
    break;
    case 10:
    Serial.print("Oct");
    break;
    case 11:
    Serial.print("Nov");
    break;
    case 12:
    Serial.print("Dec");
    break;
  }
  Serial.print("-");
  Serial.println(year,DEC);

  Serial.println(" ");
  delay(1045);
}

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);
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  *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());
}

Make it 10 times easier and use a DS3231 library. I recommend JeeLabs or derivative.

aarg:
Make it 10 times easier and use a DS3231 library. I recommend JeeLabs or derivative.

I have no problems with the library, but with doing timers with RTC .... for example 40 minutes on and 50 minutes off ....

I will still look at this library, thank you

ciorneimihai:
I have no problems with the library, but with doing timers with RTC .... for example 40 minutes on and 50 minutes off ....

What problems ?

At its crudest just count changes of the minute value, but there are better ways

UKHeliBob:
What problems ?

At its crudest just count changes of the minute value, but there are better ways

could you please give me a small example? I know, because how I solved the problem is complicated ... there must be a much simpler solution ... only I don't see it ...

if (minute() != oldMinute)
{
++minutesElapsed;
oldMinute = minute();
}
1 Like

OMG I finally managed a somewhat clean code, thanks to you. Thanks a lot, I think the best POST title should be changed to Timer with RTC by minutes

#include <Wire.h>
#include <SPI.h>

#define DS3231_I2C_ADDRESS 0x68

byte DecToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
byte BcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
int oldMinute = 0;
int minutesElapsed = -1;
int on_min = 2;
int off_min = 1;

int relay1 = 12;

void setup() 
{
Serial.begin(9600);
   Wire.begin();
pinMode(relay1,OUTPUT);
digitalWrite(relay1, HIGH);
}

void loop() 
{
 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);

if (minute != oldMinute)
{
++minutesElapsed;
oldMinute = minute;
}

Serial.print(" Min passed :");
Serial.println(minutesElapsed);

if(digitalRead(relay1)==HIGH && minutesElapsed<on_min)
  {
    digitalWrite(relay1,LOW);
  }
  else if(digitalRead(relay1==LOW) && minutesElapsed<on_min)
    {
      digitalWrite(relay1,LOW);
    }
      else if(digitalRead(relay1)==LOW && minutesElapsed>=on_min && minutesElapsed <= on_min+off_min)
          {
            digitalWrite(relay1,HIGH);
          }
          else if(digitalRead(relay1)==HIGH && minutesElapsed>=on_min+off_min)
            {
              digitalWrite(relay1,HIGH);
              minutesElapsed = 0;
            }

delay(1000);
}


void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  *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());
}

aarg:

if (minute() != oldMinute)

{
++minutesElapsed;
oldMinute = minute();
}

Tnk s man,so simple and at the same time useful.

I think the best POST title should be changed to Timer with RTC by minutes

If you edit your original post in the topic then you can change the topic title

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.