//Package 1/2: Set time manually.

#include <DS3231.h>
DS3231  rtc(SDA, SCL);

void setup()
{
Serial.begin(115200);

rtc.begin();
    
    rtc.setTime(17, 45, 0); // <-- Set time (Hours, Munutes, Seconds)
 
}

void loop()
{
 
  Serial.println(rtc.getTimeStr());
   
  delay (1000);
}

// This code was designed to control relays triggered by alarms (Manually set RTC). The code enables polarity reversion of DC appliances.  
// Author: Eduard Musin

// Package 2/2: After the time was manually set, set relay OPEN/CLOSE time  

#include <DS3231.h>

int Relay1 = 4; // To control Relay channel 1
int Relay2 = 2; // To control Relay channel 2

DS3231  rtc(SDA, SCL);
Time t;


// Note: relay opening duration between OnSec and Offsec should correspond with the time required to power an electronic appliance to execute its action. 
// E.g. car actuator = 1 sec; linear actuator 10 cm = 14-15 sec

// 1. Open time
const int OnHour1 = 17;
const int OnMin1 = 47;
const int OnSec1 = 1; 
const int OffHour1 = 17;
const int OffMin1 = 47;
const int OffSec1 = 2;

// 2. Closing time
const int OnHour2 = 17;
const int OnMin2 =48;
const int OnSec2 = 1;
const int OffHour2 = 17;
const int OffMin2 = 48;
const int OffSec2 = 2;

void setup() {
  Serial.begin(115200);
  rtc.begin();

  pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);

  pinMode(Relay2, OUTPUT);
  digitalWrite(Relay2, HIGH);

}

void loop() {
  t = rtc.getTime();
 
  Serial.print(t.hour);
  Serial.print(" : ");
  Serial.print(t.min);
  Serial.print(" : ");
  Serial.print(t.sec);
  Serial.print(" ");
  Serial.println(" ");
  delay (1000);


  if(t.hour == OnHour1 && t.min == OnMin1 && t.sec == OnSec1){
    digitalWrite(Relay1,LOW);
    Serial.println("POWER ON - LINE I");
    }

    else if(t.hour == OffHour1 && t.min == OffMin1 && t.sec == OffSec1){
      digitalWrite(Relay1,HIGH);
      Serial.println("POWER OFF - LINE I");
    }

  if(t.hour == OnHour2 && t.min == OnMin2 && t.sec == OnSec2){
    digitalWrite(Relay2,LOW);
    Serial.println("POWER ON - LINE II");
    }

    else if(t.hour == OffHour2 && t.min == OffMin2 && t.sec == OffSec2){
      digitalWrite(Relay2,HIGH);
      Serial.println("POWER OFF - LINE II");
    }

}

//Package 1/2: Set time manually.

#include <DS3231.h>
DS3231  rtc(SDA, SCL);

void setup()
{
Serial.begin(115200);

rtc.begin();
    
    rtc.setTime(17, 45, 0); // <-- Set time (Hours, Munutes, Seconds)
 
}

void loop()
{
 
  Serial.println(rtc.getTimeStr());
   
  delay (1000);
}

© Copyrights 2017-2021 eco-honey.com

Apiary "Eco-Honey"

Email: info@eco-honey.com
Instagram: ecohoney_russia