-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydro_switchbox.ino
144 lines (113 loc) · 3.54 KB
/
hydro_switchbox.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
139
140
141
142
143
//#define DEBUG
#include "schedule.h"
#include "timestamp.h"
//this is what time it is when the chip turns on
unsigned long start_time = time_to_milli( 20, 27 );
int next_event_idx = -1;
unsigned long next_event_millis = 0;
int day = 0;
unsigned long next_day_millis = ONEDAY;
unsigned long milli_offset = 0;
Event schedule[] = {
{time_to_milli(6, 0), LIGHTPIN, LOW},
duration_to_event(6, 10, 10 * ONEMINUTE, PUMPPIN),
duration_to_event(9, 10, 10 * ONEMINUTE, PUMPPIN),
duration_to_event(10, 0, 4 * ONEHOUR, FANPIN),
duration_to_event(12, 10, 10 * ONEMINUTE, PUMPPIN),
duration_to_event(13, 45, 5 * ONEMINUTE, PUMPPIN),
duration_to_event(15, 10, 10 * ONEMINUTE, PUMPPIN),
duration_to_event(16, 0, 3 * ONEHOUR, FANPIN),
duration_to_event(18, 10, 10 * ONEMINUTE, PUMPPIN),
duration_to_event(21, 10, 10 * ONEMINUTE, PUMPPIN),
//{time_to_milli(18, 0), LIGHTPIN, HIGH},
{time_to_milli(23, 0), LIGHTPIN, HIGH},
//TEST SCHEDULE
//{time_to_milli(0, 0), LIGHTPIN, LOW},
//{time_to_milli(3, 0), LIGHTPIN, HIGH},
//{time_to_milli(4, 0), LIGHTPIN, LOW},
//{time_to_milli(5, 0), LIGHTPIN, HIGH},
//{time_to_milli(10, 0), LIGHTPIN, LOW},
//{time_to_milli(15, 0), LIGHTPIN, HIGH},
SCHED_SENTINEL
};
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
//pinMode(3, OUTPUT);
digitalWrite( 0, HIGH );
digitalWrite( 1, HIGH );
digitalWrite( 2, HIGH );
//digitalWrite( 3, HIGH );
sortSchedule(&schedule[0]);
start_time = dayOffsetFromString( __TIME__ );
//unsigned long start_off = millis() + start_time;
milli_offset = start_time;
readyNextEvent();
//skip events until the next one is in the future.
while ( milli_offset > next_event_millis )
readyNextEvent();
//start the event that should be currently running
startCurrentEvents( next_event_idx );
}
void loop() {
unsigned long now = safe_millis( millis() );
if ( now > next_day_millis ) {
day++;
next_day_millis += ONEDAY;
}
if ( now > next_event_millis ) {
digitalWrite( schedule[ next_event_idx ].pin, schedule[ next_event_idx ].value );
readyNextEvent();
}
if ( 0 == ( now % TIME_SAVE_INTERVAL ) )
{
updateTimestamp( now );
}
}
//run the events for each pin that should be running if the next event is idx
void startCurrentEvents( int idx ) {
int pins[4] = {HIGH, HIGH, HIGH, HIGH};
int i, started = 0;
for ( i = idx; i != idx || !started; i++ )
{
started = true;
if ( is_sentinel( schedule[ i ] ) )
{
i = -1;
continue;
}
pins[ schedule[i].pin ] = schedule[i].value;
}
for ( i = 0; i < 4; i++ )
{
if ( pins[i] > -1 )
digitalWrite( i, pins[i] );
}
}
void readyNextEvent() {
next_event_idx++;
int event_day = day;
if ( is_sentinel( schedule[ next_event_idx ] ) ) {
next_event_idx = 0;
event_day++;
}
unsigned long prev = next_event_millis;
next_event_millis = ONEDAY*event_day + schedule[ next_event_idx ].time;
//blinkN( 1, next_event_idx + 1 );
if ( prev > next_event_millis ) {
//overflow
//milli_offset = next_event_millis + (((unsigned long)-1) - prev);
//next_event_millis += milli_offset;
//blinkN( 2, 10 );
}
}
void blinkN( int pin, int n ) {
while ( n > 0 ) {
digitalWrite( pin, LOW );
delay( 100 );
digitalWrite( pin, HIGH );
delay( 300 );
n--;
}
}