-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWatchWinder.ino
612 lines (547 loc) · 17.3 KB
/
WatchWinder.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*----------------------------------------------------------------------------
-Arduino IDE 1.0.5
-Hardware
-Arduino Mega 2560 R3
-NEMA 14 0.9deg (400 step/rev) 11Ncm stepper motor
-SilentStepStick stepper motor driver
-Adafruit Ultimate GPS Breakout [ID:746]
-Adafruit RGB backlight negative LCD 20x4 + extras (RGB on black) [ID:498]
-Adafruit i2c / SPI character LCD backpack[ID:292]
FUNCTIONS
-Adjustable number of turns per day (TPD)
-increments of 5
-Adjustable direction
-clockwise
-counterclockwise
-both
-Adjustable turn frequency
-turn the watch ever n number of minutes?
-calculate number of turns per cycle, depending on cycle frequency
-Full wind
-run continuously, to wind a watch from a dead stop. 800 turns.
-GPS receiver
-get time for clock to display on screen.
-Turn counter
-displays number of turns since midnight.
----------------------------------------------------------------------------*/
#include <TimerOne.h>
#include <Wire.h>
//INSERT THE NEXT 4 INCLUDES AFTER INCLUDING WIRE LIBRARY
#include <LiquidTWI2.h>
#include <buttons.h>
#include <MENWIZ_LiquidTWI2.h>
#include <EEPROM.h>
//---------------------------------------------------
#include <AccelStepper.h>
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Time.h>
//////////////////////////////////////////////////////////
// CONSTANTS //
//////////////////////////////////////////////////////////
const int ESCAPE_BUTTON = 9; //Escape Button
const int CONFIRM_BUTTON = 10; //Confirm Button
const int DOWN_BUTTON = 12; //Down Button
const int UP_BUTTON = 11; //Up Button
const int w1ACT = 8; //button to activate/deactivate watch slot
const int w1SLEEP = 24; //Driver Sleep Pin
const int w1LED = 43; //feedback light on w1ACT
const long REV = 6400; //number of steps in 1 revolution of motor
const long RPM = 8; //Speed at which the motor will spin
const long MINUTES_IN_DAY = 1440; //minutes in 24 hours
const int w1FULL_WIND = 800; //Revolutions to fully wind from stop
char* w1ACTIVE_STAT[]={"STATUS: OFF", "STATUS: ON"}; //Used for Status screen.
//////////////////////////////////////////////////////////
// GLOBAL VARIABLES //
//////////////////////////////////////////////////////////
int w1TPD = 650; //Turns per day
int w1DIR = 0; //0=CW, 1=CCW, 2=BOTH used in menu
int w1DIRECTION = 1; //Used in functions to determine direction
int w1CYCLE_TIME = 15; //Minutes between cycles
int w1CYCLE_TIME_S = w1CYCLE_TIME*60; //Seconds between cycles
int w1CYCLE_TIME_LAST = w1CYCLE_TIME; //Used to determine change in CYCLE_TIME
long w1STEPS ; //Use in WIND_NOW() function & full wind, and loop cleanup
int w1FULL_WIND_STATUS = 0; //0=OFF, 1=unidirectional, 2=bidirectional
int w1FULL_WIND_COUNT = 1; //Used to determine when to switch direction
boolean w1ACTIVE = 1; //Is this slot turned on or off?
boolean w1ACT_STATE = 0; //Used for determining w1ACTIVE
boolean w1ACT_LAST_STATE = 0; //Used for determining w1ACTIVE
int w1TURN_COUNTER = 0; //Counts number of turns so far, since midnight
long w1COUNTER_START = 0; //Used for turn counter to count steps
int LAST_HOUR = 0; //Used to reset turn counter
boolean DST = 1; //Is DST in effect or not?
boolean DST_LAST = DST;
int ZONE_OFFSET = -5; //time zone offset
int ZONE_OFFSET_LAST = ZONE_OFFSET; //time zone offset
boolean TIME_SET_FLAG = false; //Check if time has been synced to GPS
int TIME_HOUR = 0; //For clock on statusScreen screen
int TIME_MIN = 0;
int TIME_SEC = 0;
unsigned long SEC_COUNTER = 0; //Used to see if a second has passed
//////////////////////////////////////////////////////////
// OBJECTS //
//////////////////////////////////////////////////////////
menwiz menu;
LiquidTWI2 lcd(0x20);
//CREATE ACCELSTEPPERS
AccelStepper w1STEPPER(1, 23, 22); //1 pin=driver, 23=step pin, 22=direction pin
//GPS
HardwareSerial GPSSerial = Serial1;
Adafruit_GPS GPS(&GPSSerial);
//////////////////////////////////////////////////////////
// SETUP //
//////////////////////////////////////////////////////////
void setup()
{
Serial.begin(19200);
SEC_COUNTER = now();
// SET UP LCD
lcd.setMCPType(LTI_TYPE_MCP23008);
lcd.begin(20, 4);
//CHECK IF SETTINGS ARE STORED IN MEMORY & LOAD IF SO
if (EEPROM.read(0) == 1)
{
w1TPD = EEPROM.read(1) * 256 + EEPROM.read(2);
w1DIR = EEPROM.read(3);
w1CYCLE_TIME = EEPROM.read(4);
ZONE_OFFSET = EEPROM.read(5) - 12;
ZONE_OFFSET_LAST = ZONE_OFFSET;
DST = EEPROM.read(6);
DST_LAST = DST;
}
//CONFIG PINS NOT PART OF MENU NAV
pinMode(w1ACT, INPUT_PULLUP);
pinMode(w1LED, OUTPUT);
digitalWrite(w1LED, w1ACTIVE);
pinMode(w1SLEEP, OUTPUT);
digitalWrite(w1SLEEP, HIGH);
//ACCELSTEPPERS
w1STEPPER.setMaxSpeed((REV*RPM)/60.0); //APPROX 8RPM
w1STEPPER.setAcceleration((REV*RPM)/60.0); //takes 1 sec to reach full speed
//GPS
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
////////////////////////////////////////////////
// MENU STRUCTURE STARTS HERE //
////////////////////////////////////////////////
_menu *r,*s1,*s2;
_var *v;
//INIZIALIZE THE MENU OBJECT (20 COLUMS X 4 ROWS)
menu.begin(&lcd,20,4);
menu.setBehaviour(MW_MENU_INDEX,false);
//ROOT
r=menu.addMenu(MW_ROOT,NULL,F(" SETTINGS")); //create a root menu at first (required)
//TPD
s1=menu.addMenu(MW_VAR,r,F("TPD")); //add a terminal node (that is "variable");
s1->addVar(MW_AUTO_INT,&w1TPD,50,2000,10); //create a variable of type auto int
//DIR
s1=menu.addMenu(MW_VAR,r,F("DIRECTION"));
s1->addVar(MW_LIST,&w1DIR);
s1->addItem(MW_LIST,F("CW")); //add option to the OPTION LIST
s1->addItem(MW_LIST,F("CCW")); //add option to the OPTION LIST
s1->addItem(MW_LIST,F("BOTH")); //add option to the OPTION LIST
//CYCLE TIME
s1=menu.addMenu(MW_VAR,r,F("FREQUENCY")); //add a terminal node (that is "variable");
s1->addVar(MW_AUTO_INT,&w1CYCLE_TIME,1,60,1); //create a variable of type auto int...
//FULL WIND
s1=menu.addMenu(MW_VAR,r,F("FULL WIND")); //SPIN FOR 800 REVS NONSTOP
s1->addVar(MW_ACTION,fullWind);
s1->setBehaviour(MW_ACTION_CONFIRM,true);
//CLOCK
s1=menu.addMenu(MW_SUBMENU,r,F("CLOCK"));
//DST
s2=menu.addMenu(MW_VAR,s1,F("DST")); //DST on/off?
s2->addVar(MW_BOOLEAN,&DST);
//TIME ZONE
s2=menu.addMenu(MW_VAR,s1,F("TIME ZONE")); //Change ZONE_OFFSET var for UTC offset
s2->addVar(MW_AUTO_INT,&ZONE_OFFSET,-12,14,1);
//SAVE SETTINGS
s1=menu.addMenu(MW_VAR,r,F("SAVE SETTINGS")); //save TPD/Direction/Freq/DST/zone to EEPROM
s1->addVar(MW_ACTION,saveSettings);
s1->setBehaviour(MW_ACTION_CONFIRM,true);
//DECLARE NAVIGATION BUTTONS (REQUIRED)
menu.navButtons(UP_BUTTON,DOWN_BUTTON,ESCAPE_BUTTON,CONFIRM_BUTTON);
//(optional)create a user define screen callback to activate after 5 secs (5000 millis) since last button push
menu.addUsrScreen(statusScreen,5000);
//Timer1 INTERRUPTS - fires stepper.run() & reads GPS every 250 microseconds
//using an interrupt to prevent menu.draw() from slowing down stepper motion.
Timer1.initialize(250);
Timer1.attachInterrupt(runWinder);
}
//////////////////////////////////////////////////////////
// LOOP //
//////////////////////////////////////////////////////////
void loop()
{
//DRAW MENU AND MONITOR BUTTONS FOR MENU NAV
menu.draw();
//IF STEPPER NEEDS TO MOVE, WAKE IT UP
if (w1STEPPER.distanceToGo() != 0)
{
digitalWrite(w1SLEEP, LOW);
}
//ACTIVATION BUTTON CHECK w1
w1ACT_STATE = digitalRead(w1ACT);
if (w1ACT_STATE == LOW && w1ACT_STATE != w1ACT_LAST_STATE)
{
w1ACTIVE = !w1ACTIVE; //Toggle on/off w1
digitalWrite(w1LED, w1ACTIVE); //button feedback
}
w1ACT_LAST_STATE = w1ACT_STATE;
//IF CYCLE TIMES CHANGE, UPDATE APPROPRIATE TIMER
if (w1CYCLE_TIME != w1CYCLE_TIME_LAST)
{
w1ChangeCycleTime();
}
//IF TIME ZONE OFFSET CHANGE, UPDATE CLOCK
if (ZONE_OFFSET != ZONE_OFFSET_LAST)
{
changeTimeOffset();
}
if (DST != DST_LAST)
{
changeDSTOffset();
}
//STEPPER MOTION CLEANUP AND SLEEP
if (w1STEPPER.distanceToGo() == 0)
{
//FINISHED FULL WIND EITHER CW OR CCW
if (w1FULL_WIND_STATUS == 1)
{
w1FULL_WIND_STATUS = 0;
}
//FINISHED WITH FULL WIND IN BOTH DIRECTIONS
if (w1FULL_WIND_COUNT == w1FULL_WIND/10)
{
w1FULL_WIND_STATUS = 0;
w1FULL_WIND_COUNT = 1;
}
//NOT FINISHED WITH FULL WIND IN BOTH DIRECTIONS
if (w1FULL_WIND_STATUS == 2)
{
w1FULL_WIND_COUNT = w1FULL_WIND_COUNT + 1;
w1DIRECTION = -1*w1DIRECTION;
w1STEPS = 10*REV;
w1STEPPER.move(w1DIRECTION*w1STEPS);
}
if (w1STEPPER.distanceToGo() == 0)
{
digitalWrite(w1SLEEP, HIGH);
}
}
//TURN COUNTER
if (w1COUNTER_START - abs(w1STEPPER.distanceToGo()) >= REV)
{
w1COUNTER_START = abs(w1STEPPER.distanceToGo());
w1TURN_COUNTER = w1TURN_COUNTER + 1;
}
//SECOND COUNTER
if (now() > SEC_COUNTER)
{
SEC_COUNTER = now();
everySecTimer();
}
//WAIT FOR GPS TO SYNC CLOCK ON STARTUP
if (GPS.fix == 1 && TIME_SET_FLAG == false)
{
TIME_SET_FLAG = true;
timeSync();
}
//IF NEW DATA WAITING FROM GPS, PARSE IT
if (GPS.newNMEAreceived())
{
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return;
}
}
///////////////////////////////////
// CLOCK //
///////////////////////////////////
void clock()
{
//INCREMENT 1 SEC
TIME_SEC = TIME_SEC + 1;
//END OF CURRENT MINUTE.
if (TIME_SEC == 60)
{
TIME_SEC = 0;
TIME_MIN = TIME_MIN + 1;
}
//END OF CURRENT HOUR.
if (TIME_MIN == 60)
{
//IF TURN OF MIDNIGHT
if (TIME_HOUR == 23)
{
w1TURN_COUNTER = 0;
}
timeSync();
}
}
///////////////////////////////////
// COUNTDOWN //
///////////////////////////////////
void countdown()
{
//IF SLOT IS ACTIVE AND NOT CURRENTLY IN FULL WIND MODE...
if(w1ACTIVE == 1 && w1FULL_WIND_STATUS == 0)
{
w1CYCLE_TIME_S = w1CYCLE_TIME_S - 1;
if (w1CYCLE_TIME_S < 0)
{
w1WindNow();
}
}
}
///////////////////////////////////
// CYCLE TIME WAS CHANGED. //
// UPDATE TIMERS WITH NEW VALUE //
///////////////////////////////////
void w1ChangeCycleTime()
{
w1CYCLE_TIME_LAST = w1CYCLE_TIME;
w1CYCLE_TIME_S = w1CYCLE_TIME*60;
}
///////////////////////////////////
// TIME ZONE OFFSET WAS CHANGED //
///////////////////////////////////
void changeTimeOffset()
{
TIME_HOUR = TIME_HOUR+(ZONE_OFFSET-ZONE_OFFSET_LAST);
ZONE_OFFSET_LAST = ZONE_OFFSET;
}
//CAN/SHOULD I COMBINE THIS INTO ABOVE?
void changeDSTOffset()
{
if (DST)
{
if (TIME_HOUR != 23)
{
TIME_HOUR = TIME_HOUR+1;
}
else
{
TIME_HOUR = 0;
}
}
if (!DST)
{
if (TIME_HOUR != 0) //add this if/else to deal with changing DST setting after clock is midnight. Currently changes hour to -1 which is no good.
{
TIME_HOUR = TIME_HOUR-1;
}
else
{
TIME_HOUR = 23;
}
}
DST_LAST=DST;
}
///////////////////////////////////
// EVERY SEC TIMER //
///////////////////////////////////
void everySecTimer()
{
countdown();
clock();
}
///////////////////////////////////
// FULLY WIND //
///////////////////////////////////
void fullWind()
{
if (w1FULL_WIND_STATUS != 0)
{
w1FULL_WIND_STATUS = 0;
w1STEPPER.stop();
w1FULL_WIND_COUNT = 1;
return;
}
if (w1FULL_WIND_STATUS == 0)
{
switch (w1DIR)
{
//CLOCKWISE FULL WIND
case 0:
w1FULL_WIND_STATUS = 1;
w1DIRECTION = -1;
w1STEPS = w1FULL_WIND*REV;
break;
//COUNTER CLOCKWISE FULL WIND
case 1:
w1FULL_WIND_STATUS = 1;
w1DIRECTION = 1;
w1STEPS = w1FULL_WIND*REV;
break;
//BOTH DIRECTION FULL WIND
case 2:
w1FULL_WIND_STATUS = 2;
w1DIRECTION = -1*w1DIRECTION;
w1STEPS = 10*REV;
break;
}
w1STEPPER.move(w1DIRECTION*w1STEPS);
}
}
///////////////////////////////////
// RUN STEPPER //
///////////////////////////////////
void runWinder()
{
//COLLECT DATA FROM GPS
GPS.read();
//RUN WINDER IF STEPS ARE WAITING
if (w1STEPPER.distanceToGo() != 0)
{
w1STEPPER.run();
}
}
///////////////////////////////////
// SAVE SETTINGS //
///////////////////////////////////
void saveSettings()
{
//WRITE INDICATION INFO IS SAVED
EEPROM.write(0,1);
//SPLIT w1TPD INTO BYTE SIZED VARS
int a = w1TPD/256;
int b = w1TPD%256;
EEPROM.write(1,a);
EEPROM.write(2,b);
//STORE w1DIR
EEPROM.write(3,w1DIR);
//STORE FREQUENCY
EEPROM.write(4,w1CYCLE_TIME);
//STORE TIME ZONE
int UZO = ZONE_OFFSET + 12;
EEPROM.write(5,UZO);
//STORE DST
EEPROM.write(6,DST);
}
///////////////////////////////////
// STATUS SCREEN //
///////////////////////////////////
void statusScreen()
{
static char STATS[80];
STATS[0]=0;
static char buf_turn[7];
static char buf_min[3];
static char buf_sec[3];
static char buf_timeh[3];
static char buf_timem[3];
static char buf_times[3];
//STATUS
strcat(STATS,w1ACTIVE_STAT[w1ACTIVE]);
strcat(STATS,"\n");
//TURNS
strcat(STATS," TURNS: ");
strcat(STATS,itoa(w1TURN_COUNTER,buf_turn,10));
strcat(STATS,"\n");
//NEXT CYCLE
int COUNTDOWN_MIN = w1CYCLE_TIME_S/60;
int COUNTDOWN_SEC = w1CYCLE_TIME_S%60;
int COUNTDOWN_MIN_TENS = COUNTDOWN_MIN/10;
int COUNTDOWN_MIN_ONES = COUNTDOWN_MIN%10;
int COUNTDOWN_SEC_TENS = COUNTDOWN_SEC/10;
int COUNTDOWN_SEC_ONES = COUNTDOWN_SEC%10;
strcat(STATS," NEXT: ");
strcat(STATS,itoa(COUNTDOWN_MIN_TENS,buf_timem,10)); //minutes 10s place
strcat(STATS,itoa(COUNTDOWN_MIN_ONES,buf_timem,10)); //minutes 1s place
strcat(STATS,":");
strcat(STATS,itoa(COUNTDOWN_SEC_TENS,buf_times,10)); //seconds 10s place
strcat(STATS,itoa(COUNTDOWN_SEC_ONES,buf_times,10)); //seconds 1s place
strcat(STATS,"\n");
//TIME
int TIME_MIN_TENS = TIME_MIN/10;
int TIME_MIN_ONES = TIME_MIN%10;
int TIME_SEC_TENS = TIME_SEC/10;
int TIME_SEC_ONES = TIME_SEC%10;
strcat(STATS," TIME: ");
if (TIME_SET_FLAG == false)
{
strcat(STATS,"WAITING...");
}
if (TIME_SET_FLAG == true)
{
//HOUR AM
if (TIME_HOUR <= 12)
{
if (TIME_HOUR != 0)
{
strcat(STATS,itoa(TIME_HOUR,buf_timeh,10));
}
if (TIME_HOUR == 0)
{
strcat(STATS,itoa(12,buf_timeh,10));
}
}
//HOUR PM
if (TIME_HOUR > 12)
{
int TIME_HOUR_PM = TIME_HOUR - 12;
strcat(STATS,itoa(TIME_HOUR_PM,buf_timeh,10));
}
strcat(STATS,":");
strcat(STATS,itoa(TIME_MIN_TENS,buf_timem,10)); //minutes 10s place
strcat(STATS,itoa(TIME_MIN_ONES,buf_timem,10)); //minutes 1s place
strcat(STATS,":");
strcat(STATS,itoa(TIME_SEC_TENS,buf_times,10)); //seconds 10s place
strcat(STATS,itoa(TIME_SEC_ONES,buf_times,10)); //seconds 1s place
strcat(STATS," ");
//AM
if (TIME_HOUR < 12)
{
strcat(STATS,"AM");
}
//PM
if (TIME_HOUR >= 12)
{
strcat(STATS,"PM");
}
strcat(STATS,"\n");
}
menu.drawUsrScreen(STATS);
}
///////////////////////////////////
// TIME SYNC //
///////////////////////////////////
time_t timeSync()
{
TIME_HOUR = (GPS.hour+ZONE_OFFSET+DST+24)%24;
TIME_MIN = GPS.minute;
TIME_SEC = GPS.seconds;
}
/////////////////////////////////////////////
// w1WindNow CALC WIND STEPS AND DIRECTION //
/////////////////////////////////////////////
void w1WindNow()
{
if (w1ACTIVE == true)
{
switch (w1DIR)
{
//CLOCKWISE
case 0:
w1DIRECTION = -1;
break;
//COUNTER CLOCKWISE
case 1:
w1DIRECTION = 1;
break;
//BOTH
case 2:
w1DIRECTION = -1*w1DIRECTION;
break;
}
w1STEPS = (w1TPD*REV)/(MINUTES_IN_DAY/w1CYCLE_TIME);
noInterrupts(); //prevents Timer1 interrupt from moving motor and changing remaining steps for step counting calcs
w1STEPPER.move(w1DIRECTION*w1STEPS);
//TURN STEP COUNTER
w1COUNTER_START = w1COUNTER_START + abs(w1STEPPER.distanceToGo());
interrupts(); //reinitalize interrupts now that w1COUNTER_START is set to accurate value for calcs
}
//RESET COUNT DOWN
w1CYCLE_TIME_S = w1CYCLE_TIME*60-1;
}