RTC 1302 with Relay

#include <virtuabotixRTC.h>                                                                           

// Creation of the Real Time Clock Object Inisialisasi pin (CLK, DAT, RST)
virtuabotixRTC myRTC(6, 7, 8);
const int relay1 = 5;
const int relay2 = 6;

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

  pinMode(relay1, OUTPUT);
  digitalWrite(relay1,HIGH);
  digitalWrite(relay2,HIGH);

  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  //myRTC.setDS1302Time(00, 40, 22, 3, 1, 5, 2025);
}

void loop()  {                                                                                           
  // This allows for the update of variables for time or accessing the individual elements.                
  myRTC.updateTime();                                                                                    
  // Start printing elements as individuals                                                                
  Serial.print("Current Date / Time: ");
  imprime_dia_da_semana(myRTC.dayofweek);
  Serial.print(", ");
  Serial.print(myRTC.dayofmonth);                                                                        
  Serial.print("/");                                                                                     
  Serial.print(myRTC.month);                                                                             
  Serial.print("/");                                                                                     
  Serial.print(myRTC.year);                                                                              
  Serial.print("  ");                                                                                    
  Serial.print(myRTC.hours);                                                                             
  Serial.print(":");                                                                                     
  Serial.print(myRTC.minutes);                                                                           
  Serial.print(":");                                                                                     
  Serial.println(myRTC.seconds);                                                                                    

  // Delay so the program doesn't print non-stop                                                           
  delay( 1000);
  //relay1
  if(myRTC.hours>=18 && myRTC.hours<=24 || myRTC.hours>=0 && myRTC.hours<=5){
    hidup();
    }
  else{
    mati();
    }
  //relay2
  if(myRTC.hours>=19 && myRTC.hours<=21 || myRTC.hours>=23 && myRTC.hours<=24 || myRTC.hours>=3 && myRTC.hours<=5){
    digitalWrite(relay1,LOW);
    }
  else{
    digitalWrite(relay1,HIGH);
    }
}                                                                                                        
void hidup(){
  digitalWrite(relay2,LOW);
  }
void mati(){
  digitalWrite(relay2,HIGH);
  }
void imprime_dia_da_semana(int dia)
{
  switch (dia)
  {
    case 0:
      Serial.print("MON");
      break;
    case 1:
      Serial.print("TUE");
      break;
    case 2:
      Serial.print("WED");
      break;
    case 3:
      Serial.print("THU");
      break;
    case 4:
      Serial.print("FRI");
      break;
    case 5:
      Serial.print("SAT");
      break;
    case 6:
      Serial.print("SUN");
      break;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *