-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoCode
More file actions
356 lines (289 loc) · 8.93 KB
/
Copy pathArduinoCode
File metadata and controls
356 lines (289 loc) · 8.93 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <Time.h>
#include <TimeLib.h>
#include <WiFiUdp.h>
#include <TimeAlarms.h>
#include <MultiStepper.h>
#include <AccelStepper.h>
#include <Firebase_Arduino_WiFiNINA.h>
#include <SPI.h>
#include <WiFiNINA.h>
FirebaseData firebaseData;
AccelStepper minuteStepper(AccelStepper::FULL4WIRE, 2, 3, 4, 5);
AccelStepper hourStepper(AccelStepper::FULL4WIRE, 6, 7, 8, 9);
WiFiUDP Udp;
IPAddress timeServer(216, 239, 35, 0); // Google NTP Server
const int hourSteps = 60;
const int minuteSteps = 12;
const int timeZone = +;
unsigned long oldTime = 1551880287;
unsigned long actualTime;
unsigned long previousTime = 0;
unsigned int localPort = 2390;
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
bool isTimerRunning = false;
String clockMode = "rest";
int timerStart;
int timeToGo;
// A UDP instance to let us send and receive packets over UDP
void setup()
{
Serial.begin(115200);
delay(4000);
hourStepper.setMaxSpeed(600);
hourStepper.setAcceleration(100);
minuteStepper.setMaxSpeed(600);
minuteStepper.setAcceleration(100);
pinMode (A7, INPUT);
pinMode (11, INPUT);
pinMode (LED_BUILTIN, OUTPUT);
Serial.println("Connecting to Wi-Fi");
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED)
{
status = WiFi.begin("[YOUR SSID]", "[PASSWORD]");
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected to Wi-fi IP : ");
Serial.println(WiFi.localIP());
Firebase.begin("[FIREBASE URL", "[FIREBASE SECRET]", "[SSID]", "[PASSWORD]");
Firebase.reconnectWiFi(true);
/*if (!Firebase.beginStream(firebaseData, "/washer/Timer/timerRemainingSec"))
{
//Could not begin stream connection, then print out the error detail
Serial.println(firebaseData.errorReason());
} */
Serial.println("\nStarting connection to server...");
Udp.begin(localPort);
// Hall Sensor is LOW when a magnet is close.
int hallSensorValue1 = analogRead(A7);
int hallSensorValue2 = analogRead(A6);
int initialHoming = -1;
Serial.print("Hall Sensor is : ");
Serial.print(hallSensorValue1);
Serial.println(hallSensorValue2);
/* while (hallSensorValue1 > 1000) {
hourStepper.moveTo(initialHoming);
initialHoming--;
hourStepper.run();
hallSensorValue1 = analogRead(A7);
}
initialHoming = -1;
while (hallSensorValue2 > 1000) {
minuteStepper.moveTo(initialHoming);
initialHoming--;
minuteStepper.run();
hallSensorValue2 = analogRead(A6);
}*/
hourStepper.setCurrentPosition(0);
minuteStepper.setCurrentPosition(0);
}
void loop()
{
Serial.println("-------------------------");
timerStart = getFirebaseTimer();
actualTime = getActualTime();
clockMode = getMode();
delay(500);
long hallSensorValue1 = analogRead(A7);
if(hallSensorValue1 < 1000) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
int actualHour = hour(actualTime);
int actualMinute = minute(actualTime);
// Display actual time.
char formattedCurrentTime[100];
sprintf(formattedCurrentTime, "Actual time : %d:%d:%d", hour(actualTime), minute(actualTime), second(actualTime));
Serial.println(formattedCurrentTime);
if (timerStart > 0)
{
// Asked for new timer,
// show it :
displayTimer(timerStart);
// then come back on current time :
delay(5000);
displayActualTime(actualHour, actualMinute);
// Maybe move this :
previousTime = actualTime;
timeToGo = timerStart;
// Serial.print("timeToGo after init : ");
// Serial.println(timeToGo);
isTimerRunning = true;
zeroFirebaseTimer();
}
else if (clockMode.equals("pasta"))
{
// "pasta" mode = we want to check timer.
Serial.println("MODE : Timer asked for");
Serial.print("timeToGo before use : ");
Serial.println(timeToGo);
int remainingTime = (previousTime + timeToGo) - actualTime;
char output[100];
sprintf(output, "Remaining time = (%d + %d) - %d = %d", previousTime, timeToGo, actualTime, remainingTime);
Serial.println(output);
if (remainingTime > 0)
{
Serial.println("MODE : Timer asked for - Display timer");
displayTimer(remainingTime);
}
else
{
Serial.println("MODE : Timer asked for - Remaining time = 0");
remainingTime = 0;
isTimerRunning = false;
displayActualTime(actualHour, actualMinute);
}
}
else if (clockMode.equals("rest"))
{
Serial.println("MODE : Let's display time !");
Serial.print("Desired hour : ");
Serial.println(actualHour);
displayActualTime(actualHour, actualMinute);
}
delay(4000);
}
void displayTimer(int seconds)
{
Serial.println("Displaying Timer...");
int minutes = seconds / 60;
hourStepper.moveTo(minutes * minuteSteps);
minuteStepper.moveTo(minutes*minuteSteps);
// minuteStepper.moveTo(minutes*minuteSteps);
moveNeedles();
}
int getFirebaseTimer()
{
int firebaseTimer;
if (Firebase.getInt(firebaseData, "/washer/Timer/timerRemainingSec"))
{
if (firebaseData.dataType() == "int")
{
firebaseTimer = firebaseData.intData();
Serial.println(firebaseTimer);
}
}
return firebaseTimer;
}
unsigned long sendNTPpacket(IPAddress &address)
{
//Serial.println("1");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
//Serial.println("3");
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
//Serial.println("4");
Udp.write(packetBuffer, NTP_PACKET_SIZE);
//Serial.println("5");
Udp.endPacket();
//Serial.println("6");
}
unsigned long getActualTime()
{
unsigned long epoch;
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket())
{
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into everyday time:
//Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
epoch = secsSince1900 - seventyYears + timeZone * 3600;
// print Unix time:
//Serial.println(epoch);
//Serial.println(minute(oldTime));
//if (minute(epoch) != minute(oldTime)) {
//Serial.println("hours are different");
//oldTime = epoch;
//}
/*Serial.println(hour(epoch));
Serial.println(minute(epoch));
Serial.println(second(epoch));*/
}
//delay(5000);
return epoch;
}
String getMode()
{
String firebaseMode;
if (Firebase.getString(firebaseData, "/washer/Modes/needles"))
{
if (firebaseData.dataType() == "string")
{
firebaseMode = firebaseData.stringData();
Serial.println(firebaseMode);
}
}
return firebaseMode;
}
void zeroFirebaseTimer()
{
if (Firebase.setInt(firebaseData, "/washer/Timer/timerRemainingSec", 0))
{
if (firebaseData.dataType() == "int")
{
Serial.print("Timer in database set to : ");
Serial.println(firebaseData.floatData());
}
else
{
//Failed, then print out the error detail
Serial.println(firebaseData.errorReason());
}
}
}
void displayActualTime(int hours, int minutes)
{
// Serial.print("Heure recue : ");
// Serial.println(hours);
// Serial.print("Distance to go : ");
// Serial.println(hourStepper.distanceToGo());
Serial.println("Displaying Actual Time...");
// Serial.println((hours % 12) * hourSteps);
hourStepper.moveTo((hours % 12) * hourSteps);
minuteStepper.moveTo(minutes*minuteSteps);
// minuteStepper.moveTo(minutes*minuteSteps);
moveNeedles();
}
void moveNeedles()
{
while (hourStepper.distanceToGo() != 0 || minuteStepper.distanceToGo() != 0)
{
if(hourStepper.distanceToGo() != 0){
hourStepper.run();
}
if(minuteStepper.distanceToGo() != 0){
minuteStepper.run();
}
}
}