-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenorah.ino
224 lines (173 loc) · 4.69 KB
/
menorah.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>
http_header_t headers[] = {
{ "Content-Type", "text/html" },
{ NULL, NULL }
};
http_request_t request;
http_response_t response;
HttpClient http;
Timer timer(60000, check_night_of_hanukkah);
SYSTEM_MODE(SEMI_AUTOMATIC);
// PWM pins D0, D1, D2, D3, A4, A5, WKP, RX and TX
int candle1 = D3;
int candle2 = WKP;
int candle3 = D0;
int candle4 = D1;
int candle5 = A5;
int candle6 = TX;
int candle7 = RX;
int candle8 = D4;
int shammash = D2;
int button = D6;
int indicator = D7;
int day = 0;
int manual = 0; //button press triggers manual mode, this will override the automatic candle-lighting
boolean connectToCloud = true;
// Cloud functions must return int and take one String
int setDay(String d)
{
manual = 1;
day = d.toInt();
clear_candles();
light_candles();
return 0;
}
void setup()
{
pinMode(shammash, OUTPUT);
pinMode(candle1, OUTPUT);
pinMode(candle2, OUTPUT);
pinMode(candle3, OUTPUT);
pinMode(candle4, OUTPUT);
pinMode(candle5, OUTPUT);
pinMode(candle6, OUTPUT);
pinMode(candle7, OUTPUT);
pinMode(candle8, OUTPUT);
pinMode(indicator, OUTPUT);
pinMode(button, INPUT_PULLDOWN);
request.hostname = "herstein.dyndns.org";
request.path = "/hebcal/index.php";
bool success = Particle.function("setDay", setDay);
timer.start();
}
void check_night_of_hanukkah()
{
int newday;
if(!manual)
{
http.get(request, response, headers);
if(response.status==200)
{
newday = response.body.toInt();
if(newday!=day)
{
day = newday;
clear_candles();
}
}
else // couldn't get to web site telling us what day it is, so set day to 0 and put into manual mode
{
day = 0;
manual = 1;
}
}
}
void clear_candles()
{
// turn all the candles off
digitalWrite(shammash, LOW);
analogWrite(candle1, 0);
analogWrite(candle2, 0);
analogWrite(candle3, 0);
analogWrite(candle4, 0);
analogWrite(candle5, 0);
analogWrite(candle6, 0);
analogWrite(candle7, 0);
digitalWrite(candle8, LOW);
}
void light_candles()
{
if(day==0)
{
// Just light the shamash
digitalWrite( shammash, HIGH);
}
if(day>0)
{
digitalWrite( shammash, HIGH);
analogWrite(candle1, random(155)+100);
}
if(day>1)
analogWrite(candle2, random(155)+100);
if(day>2)
analogWrite(candle3, random(155)+100);
if(day>3)
analogWrite(candle4, random(155)+100);
if(day>4)
analogWrite(candle5, random(155)+100);
if(day>5)
analogWrite(candle6, random(155)+100);
if(day>6)
analogWrite(candle7, random(155)+100);
if(day>7)
digitalWrite(candle8, HIGH); //Moved to a digital pin, since there aren't enough analog pins!
}
void loop()
{
if(connectToCloud && Particle.connected() == false) {
Particle.connect();
connectToCloud = false;
}
//check if it's connected, if it is, turn on blue indicator LED
if (Particle.connected())
digitalWrite(indicator, HIGH);
else
digitalWrite(indicator, LOW);
if(digitalRead(button) == HIGH)
{
manual = 1; //now in override mode!
day++;
if(day==9)
{
// reset day to zero; turn off menorah
day = 0;
// reset all candles
clear_candles();
}
delay(250);
}
light_candles();
delay(random(100));
}
/*
void printResponse(http_response_t &response) {
Serial.println("HTTP Response: ");
Serial.println(response.status);
Serial.println(response.body);
}
void getRequest() {
request.path = "/photon/time";
request.body = "";
http.get(request, response, headers);
printResponse(response);
}
void putRequest() {
request.path = "/photon/measurements";
request.body = "{\"measurementType\":\"static\", \"value\": 1000}";
http.put(request, response, headers);
printResponse(response);
}
void postRequest() {
request.path = "/photon/measurements";
request.body = "{\"measurementType\":\"static\", \"value\": 2000}";
http.post(request, response, headers);
printResponse(response);
}
void deleteRequest() {
request.path = "/photon/measurements/123";
request.body = "";
http.del(request, response, headers);
printResponse(response);
}
*/