-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino_Clock_Timer_Switch.ino
138 lines (122 loc) · 3.78 KB
/
Arduino_Clock_Timer_Switch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Arduino Clock Timer Switch
// Created by Vasken Dermardiros
// (C) 2015
// Under GNU General Public License v3.0
// Dependencies
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS1307.h>
// Attachments
// SCL on pin A4; SDA on pin A5
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
RTC_DS1307 RTC;
// Variables
double currentHour, timestamp, datestamp;
uint32_t last_press;
byte relayCh1 = 8; // Relay channels 1 and 2
byte relayCh2 = 9;
byte onBoardLED = 13;
// Trigger states
byte relayCh1Cond1 = 0;
byte relayCh1Cond2 = 0;
byte relayCh2Cond1 = 0;
byte onBoardLEDState = 0;
//--------------------------------------------------------------------------------
// USER-DEFINED FUNCTIONS
//--------------------------------------------------------------------------------
// Trigger between a time interval
byte triggerOnHour(byte pinN, double trigHour, double untrigHour, byte prevState)
{
if((currentHour > trigHour) && (currentHour < untrigHour)){
digitalWrite(pinN, HIGH);
if(prevState == 0){
prevState = 1;
//logIt(pinN, prevState);
} }
else {
digitalWrite(pinN, LOW);
if(prevState == 1){
prevState = 0;
//logIt(pinN, prevState);
} }
// Ship new state
return prevState;
}
//--------------------------------------------------------------------------------
// Return current hour
double getHour()
{
DateTime nowPresently = RTC.now(); // Get the time from the RTC
double s = nowPresently.second();
double m = nowPresently.minute();
double h = nowPresently.hour();
double d = nowPresently.day();
double mt = nowPresently.month();
double y = nowPresently.year();
currentHour = h + m/60 + s/3600;
timestamp = 1e4*h + 1e2*m + s;
datestamp = 1e4*y + 1e2*mt + d;
}
//--------------------------------------------------------------------------------
// SETUP
//--------------------------------------------------------------------------------
void setup() {
Wire.begin();
RTC.begin();
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setBacklight(true);
if (RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
//--------------------------------------------------------------------------------
pinMode(relayCh1, OUTPUT);
pinMode(relayCh2, OUTPUT);
pinMode(onBoardLED, OUTPUT);
}
//--------------------------------------------------------------------------------
// LOOP
//--------------------------------------------------------------------------------
void loop() {
getHour();
// Display time
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentHour);
lcd.setCursor(0, 1);
char buffer[32];
char timestampchar[16];
dtostrf(timestamp, 6, 0, timestampchar);
sprintf(buffer,"%s",timestampchar);
lcd.print(timestampchar);
// Toggle channel 1 if any of the conditions are met
relayCh1Cond1 = triggerOnHour(relayCh1, 9.00, 9.10, relayCh1Cond1);
relayCh1Cond2 = triggerOnHour(relayCh1, 21.00, 21.10, relayCh1Cond2);
if (relayCh1Cond1 || relayCh1Cond2 == 1) {
digitalWrite(relayCh1, HIGH);
lcd.setCursor(8, 0);
lcd.print("CH1: ON "); }
else {
digitalWrite(relayCh1, LOW);
lcd.setCursor(8, 0);
lcd.print("CH1: OFF"); }
relayCh2Cond1 = triggerOnHour(relayCh2, 7.00, 19.00, relayCh2Cond1);
if (relayCh2Cond1 == 1) {
lcd.setCursor(8, 1);
lcd.print("CH2: ON "); }
else {
lcd.setCursor(8, 1);
lcd.print("CH2: OFF"); }
// Turn off backlight after 10 seconds of inactivity
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.setBacklight(true);
last_press = millis(); }
if ((millis() - last_press) > 10000) {
lcd.setBacklight(false); }
// A small delay; the flickering LCD is somewhat annoying
delay(500);
}