// Sketch Build 1.04 compiled on Arduino 1.0.3.

// This sketch uses the RTC to control 4 relays at whatever cycle or time interval you wish.

// copy the sketch below and paste it into the Arduino IDE verify, and run the program.

// this sketch was created using code from the adafruit and RTC -libraries



//RTC CONNECTIONS

//Connect the RTC VCC to the Arduino +5 v

// RTC GND to the Arduino GND

// RTC SDA to the Arduino Analog Pin 4

// RTC SCL to the Arduino Analog Pin 5



// LCD DISPLAY CONNECTION

#define sclk 4 //connected to digital PIN 4

#define mosi 5 //connected to digital PIN 5

#define cs 6 //connected to digital PIN 6

#define dc 7 //connected to digital PIN 7

#define rst 8 //connected to digital PIN 8



// RELAY CONNECTION

// GND connected to Arduino GND

int Pin1 = 9; // IN1 connected to digital 9

int Pin2 = 10; // IN2 connected to digital 10

int Pin3 = 11; // IN3 connected to digital 11

int Pin4 = 12; // IN4 connected to digital 12

// VCC connected to Arduino +5v



#include // Core graphics library

#include // Hardware-specific library

#include // library needed for RTC

#include // library needed for RTC

#include "RTClib.h" //RTC Library



RTC_DS1307 RTC;



Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);



void setup(void) {

// DEFINING PINS FOR RELAY CONTROL

pinMode(Pin1, OUTPUT);

pinMode(Pin2, OUTPUT);

pinMode(Pin3, OUTPUT);

pinMode(Pin4, OUTPUT);

Wire.begin();

RTC.begin();



tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab



tft.fillScreen(ST7735_BLACK); // Clear screen

digitalWrite(Pin1, 1 );

digitalWrite(Pin2, 1 );

digitalWrite(Pin3, 1 );

digitalWrite(Pin4, 1 );

tft.setCursor(0,50);

tft.println(" Waiting 10 seconds ");

tft.println(" for RTC to Sync ");

delay(10000);//wait for RTC to Respond, if not set time

tft.fillScreen(ST7735_BLACK); // Clear screen

if (! RTC.isrunning()) {

tft.println(" RTC was NOT running!");

tft.println(" Resetting RTC ");

// following line sets the RTC to the date & time this sketch was compiled

RTC.adjust(DateTime(__DATE__, __TIME__));



tft.fillScreen(ST7735_BLACK); // Clear screen



}

}



void loop() {

tft.setCursor(10,10);

tft.setTextColor(ST7735_WHITE);

tft.setTextSize(1);

tft.println("TIMER CONTROL");



tft.setTextColor(ST7735_YELLOW,ST7735_BLACK);//set text color & size for DATE coming from TinyRTC

tft.setTextSize(2);

tft.setCursor(5,30);

DateTime now = RTC.now(); //GRAB DATE AND TIME FROM RTC

tft.print(now.year(), DEC);

tft.print('/');

tft.print(now.month(), DEC);

tft.print('/');

tft.print(now.day(), DEC);

tft.println(' ');

tft.setCursor(15,70);

tft.setTextColor(ST7735_GREEN,ST7735_BLACK); //set color for TIME

tft.setTextSize(2);//set text size for TIME coming from TinyRTC

tft.print(now.hour(), DEC);

tft.print(':');

if(now.minute() < 10) { //PRINT A 0 IN FRONT OF THE MINUTE IF LESS THAN 10

tft.print('0');

tft.print(now.minute(), DEC);

}

else {

tft.print(now.minute(), DEC);

}

tft.print(':');

if(now.second() < 10) {//PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10

tft.print('0');

tft.print(now.second(), DEC);

}

else {

tft.print(now.second(), DEC);

}

tft.println(" ");

// THIS IS THE DECISION MAKING PORTION OF THE SKETCH, THIS EXAMPLE IS A ONE MINUTE CYCLE

// FOR DEMO PURPOSES: IT TURNS ON RELAY 1 AT 10 SECONDS, THEN OFF AT 20

// IT THEN TURNS RELAY 2 AND 4 ON AT 40 SECONDS AND OFF AT 50,

// AT 50 SECONDS IT TURNS ON RELAY 1 AND 3 THEN OFF AT 0

if(now.second() == 10) {

tft.setCursor(0,100);

tft.setTextColor(ST7735_GREEN,ST7735_BLACK); //set color for TIME ON

tft.setTextSize(1);//set text size

tft.print("Relay 1 is on ");

digitalWrite(Pin1, 0);

}

if(now.second()== 20) {

tft.setCursor(0,100);

tft.setTextColor(ST7735_RED,ST7735_BLACK); //set color for TIME ON

tft.setTextSize(1);//set text size

tft.print("Relay 1 is off ");

digitalWrite(Pin1, 1);

}



if(now.second()== 40){

digitalWrite(Pin1, LOW);

digitalWrite(Pin2, HIGH);

digitalWrite(Pin3, LOW);

digitalWrite(Pin4, HIGH);

tft.setCursor(0,100);

tft.setTextColor(ST7735_RED,ST7735_BLACK); //set color for TIME ON

tft.setTextSize(1);//set text size

tft.setTextColor(ST7735_GREEN,ST7735_BLACK);

tft.println("Relay 1 is on ");

tft.setTextColor(ST7735_RED,ST7735_BLACK);

tft.println("Relay 2 is off ");

tft.setTextColor(ST7735_GREEN,ST7735_BLACK);

tft.println("Relay 3 is on ");

tft.setTextColor(ST7735_RED,ST7735_BLACK);

tft.println("Relay 4 is off ");



}

if(now.second()== 50){

digitalWrite(Pin1, HIGH);

digitalWrite(Pin2, LOW);

digitalWrite(Pin3, HIGH);

digitalWrite(Pin4, LOW);

tft.setCursor(0,100);

tft.setTextColor(ST7735_RED,ST7735_BLACK); //set color for TIME ON

tft.setTextSize(1);//set text size

tft.println("Relay 1 is off ");

tft.setTextColor(ST7735_GREEN,ST7735_BLACK);

tft.println("Relay 2 is on ");

tft.setTextColor(ST7735_RED,ST7735_BLACK);

tft.println("Relay 3 is off ");

tft.setTextColor(ST7735_GREEN,ST7735_BLACK);

tft.println("Relay 4 is on ");

}

if(now.second()== 00){

digitalWrite(Pin1, HIGH);

digitalWrite(Pin2, HIGH);

digitalWrite(Pin3, HIGH);

digitalWrite(Pin4, HIGH);

tft.setCursor(0,100);

tft.setTextColor(ST7735_RED,ST7735_BLACK); //set color for TIME ON

tft.setTextSize(1);//set text size

tft.println("Relay 1 is off ");

tft.println("Relay 2 is off ");

tft.println("Relay 3 is off ");

tft.println("Relay 4 is off ");

}



}