-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLOCK.ino
446 lines (395 loc) · 10.3 KB
/
CLOCK.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
//Authors: Murali K Lingamsetty
#define SEC_IN1 13
#define SEC_IN2 2
#define SEC_IN3 3
#define SEC_IN4 4
#define MIN_IN1 5
#define MIN_IN2 6
#define MIN_IN3 7
#define MIN_IN4 8
#define HRS_IN1 9
#define HRS_IN2 10
#define HRS_IN3 11
#define HRS_IN4 12
#define M0_Pro false
#define AT328_Nano true
#if(M0_Pro == true)
#define One_Tick 1 //For 48Mhz
#elif(AT328_Nano == true)
#define One_Tick 1 //For 16Mhz 1 tick = 4 us; For 8Mhz 1 tick = 8 us;
#endif
#define One_Second (1000000/One_Tick)
class STEPPER
{
const int step_delay = 10;
const int Full_Steps = 64; //for 28BYJ-48 stepper motor
private:
int Blue_Line;
int Pink_Line;
int Yellow_Line;
int Orange_Line;
int state;
int rpm;
public:
STEPPER(int pin1, int pin2, int pin3, int pin4)
{
Blue_Line = pin1;
Pink_Line = pin2;
Yellow_Line = pin3;
Orange_Line = pin4;
state = 2;
pinMode(Blue_Line, OUTPUT);
pinMode(Pink_Line, OUTPUT);
pinMode(Yellow_Line, OUTPUT);
pinMode(Orange_Line, OUTPUT);
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line, HIGH);
}
public: void Full_Step_Clockwise(int steps)
{
for( int i = 0; i < steps; i++)
{
switch (state)
{
case 2:
{ digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 4;
delay(step_delay);
//break;
}
case 4:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,LOW);
state = 6;
delay(step_delay);
//break;
}
case 6:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,HIGH);
state = 8;
delay(step_delay);
//break;
}
case 8:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 10;
delay(step_delay);
//break;
}
case 10:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line, LOW);
state = 2;
delay(step_delay);
break;
}
default:
{
state = 2;
break;
}
}
}
}
public: void Full_Step_CClockwise(int steps)
{
for( int i = 0; i < steps; i++)
{
switch (state)
{
case 2:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line, LOW);
state = 4;
delay(step_delay);
//break;
}
case 4:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 6;
delay(step_delay);
//break;
}
case 6:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,HIGH);
state = 8;
delay(step_delay);
//break;
}
case 8:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,LOW);
state = 10;
delay(step_delay);
//break;
}
case 10:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 2;
delay(step_delay);
break;
}
default:
{
state = 2;
break;
}
}
}
}
public: void Half_Step_Clockwise(int steps)
{
for(int i = 0; i < steps; i++)
{
switch (state)
{
case 2:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 3;
delay(step_delay);
//break;
}
case 3:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,LOW);
state = 4;
delay(step_delay);
//break;
}
case 4:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,LOW);
state = 5;
delay(step_delay);
//break;
}
case 5:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,HIGH);
state = 6;
delay(step_delay);
//break;
}
case 6:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, LOW);
digitalWrite(Orange_Line,HIGH);
state = 7;
delay(step_delay);
//break;
}
case 7:
{
digitalWrite(Blue_Line, HIGH);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 8;
delay(step_delay);
//break;
}
case 8:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, LOW);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 9;
delay(step_delay);
//break;
}
case 9:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line,HIGH);
state = 10;
delay(step_delay);
//break;
}
case 10:
{
digitalWrite(Blue_Line, LOW);
digitalWrite(Pink_Line, HIGH);
digitalWrite(Yellow_Line, HIGH);
digitalWrite(Orange_Line, LOW);
delay(step_delay);
state = 2;
break;
}
default:
{
state = 2;
break;
}
}
}
}
};
class MyClock
{
private:
unsigned int seconds, minutes, hours, days, years;
public:
MyClock(unsigned int s, unsigned int m, unsigned int h, unsigned int d, unsigned int y)
{
seconds = s;
minutes = m;
hours = h;
days = d;
years = y;
}
void increment_seconds(void)
{
//increment seconds
seconds++;
//if seconds is greater than a minute
if(seconds >= 60)
{
//make seconds = 0
seconds = 0;
minutes++;
if(minutes >= 60)
{
minutes = 0;
hours++;
if(hours >= 24)
{
hours = 0;
days++;
if(days >= 364)
{
days = 0;
years++;
}
}
}
}
}
unsigned long getTime(void)
{
unsigned long temp = 0;
temp = days;
temp = temp << 8;
temp |= hours;
temp = temp << 8;
temp |= minutes;
temp = temp << 8;
temp |= seconds;
return temp;
}
unsigned int getMins(void)
{
return minutes;
}
unsigned int getHrs(void)
{
return hours;
}
};
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned int mins_counter = 0;
unsigned int hrs_counter = 0;
STEPPER sec_stepper(SEC_IN1, SEC_IN2, SEC_IN3, SEC_IN4); //digital pins 1, 2, 3, 4
STEPPER min_stepper(MIN_IN1, MIN_IN2, MIN_IN3, MIN_IN4); //digital pins 5, 6, 7, 8
STEPPER hrs_stepper(HRS_IN1, HRS_IN2, HRS_IN3, HRS_IN4); //digital pins 9, 10, 11, 12
MyClock myClock(0, 0, 0, 0, 0);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//Serial.println("I began");
time1 = 0; //micros();
}
void loop()
{
String message = " ";
unsigned long currentTime = 0;
//Get current timestamp
time2 = micros();
// if time difference is greater than 1s( 1s = 1000000 * 1 ms)
if((time2 - time1) >= One_Second)
{
//save timestamp
time1 = time2;
//increment seconds
myClock.increment_seconds();
//run stepper motor clockwise (each full step takes 5 states * 10ms = 50ms delay)
sec_stepper.Full_Step_CClockwise(2);
//get current time
currentTime = myClock.getTime();
//run minutes stepper motor after each minute passes;
if((myClock.getMins() != mins_counter))
{
mins_counter = myClock.getMins();
//run the minutes stepper
min_stepper.Full_Step_Clockwise(1); //(each full step takes 5 states * 10ms = 50ms delay)
//run the hours stepper by half step
hrs_stepper.Half_Step_Clockwise(1); //(each full step takes 10 states * 10ms = 100ms delay)
}
//compose a time message
message = "TIME: ";
message += currentTime >> 24;
message += ":";
message += ((currentTime >> 16) & 0xFF);
message += ":";
message += ((currentTime >> 8) & 0xFF);
message += ":";
message += ((currentTime >> 0) & 0xFF);
//Display on serial port
Serial.println(message);
}
}