-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPersTime_pde.ino
363 lines (273 loc) · 7.71 KB
/
PersTime_pde.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
#include <Time.h>
#include <Wire.h>
#include <MCP79412RTC.h>
#include <Shieldbot.h>
#define BLACK LOW
#define WHITE HIGH
#define LEFT 1
#define RIGHT 0
#define DEBUG 1;
enum State_type {
Advance,
Wait_Advance,
Advance_Turn_right,
Rotate_to_time,
Wait_Rotate,
backup
};
Shieldbot shieldbot = Shieldbot();
int S1,S2,S3,S4,S5; //values to store state of sensors
int pre_S1,pre_S2,pre_S3,pre_S4,pre_S5;
int inc_minutes;
int counter;
int Secs, Mins, Hrs;
int last_side;
int hour_pos, min_pos;
time_t myTime;
int pre_secs;
State_type State = Advance;
void setup(){
// int Secs, Mins, Hrs;
Serial.begin(9600);
shieldbot.setMaxSpeed(255);//255 is max
// readCommand();
myTime = now(); //RTC.get()
Secs = second(myTime);
Mins = 24; ////minute(myTime);
Hrs = 12; //////////////////hour(myTime);
last_side = LEFT;
hour_pos = 0;
min_pos = 0;
counter = 0;
State = Advance;
pre_secs = Secs;
}
void loop(){
pre_S1 = S1;
pre_S2 = S2;
pre_S3 = S3;
pre_S4 = S4;
pre_S5 = S5;
S1 = shieldbot.readS1();
S2 = shieldbot.readS2();
S3 = shieldbot.readS3();
S4 = shieldbot.readS4();
S5 = shieldbot.readS5();
pre_secs = Secs;
// get time
myTime = now();
//Mins = minute(myTime);
Secs = second(myTime);
// Hrs = hour(myTime);
if (Secs != pre_secs)
{
Serial.print(Hrs, DEC);
Serial.print(":");
Serial.print(Mins, DEC);
Serial.print(":");
Serial.print(Secs, DEC);
Serial.println();
/* Serial.print(pre_secs, DEC);
Serial.print(Secs, DEC);
Serial.println();
*/
counter++;
if (counter>10)
{
Mins++;
// Serial.print(Secs, DEC);
// Serial.println();
if (Mins == 49)
Mins = 0;
counter = 0;
}
}
//drive fwd with staying centered
switch (State)
{
case Advance: //drive fwd while staying centered
if (is_centered() == true) // if aligned to go forward
{
// look for hour marker on correct side
if (last_side == RIGHT) // if last saw hour marker on the right so look on left
{
if ((S5 == BLACK) & (pre_S5 == WHITE)) // look for transition on left
{
last_side = LEFT;
hour_pos++;
if(hour_pos < Hrs)
State = Wait_Advance;
else
State = Rotate_to_time;
}
else // if cannot see hour marker yet then go forward
goforward();
}
else // last saw hour marker on left so look on right
{
if (S1 == BLACK & pre_S1 == WHITE) // look for transition on right
{
last_side = RIGHT;
hour_pos++;
if(hour_pos < Hrs)
State = Wait_Advance;
else
State = Rotate_to_time;
}
else // if cannot see hour marker yet then go forward
goforward();
}
}
else // if not aligned to go forward then realign
{
if (S2 == WHITE) // if off black to the right
goleft();
else if (S4 == WHITE) // if off black on the left
goright();
}
break;
case Wait_Advance:
delay(500);
State = Advance;
break;
case Rotate_to_time: // once lined up with hours mark rotate to the minutes
if (min_pos < Mins) // if time to advance minutes
{
if(S2 != pre_S2) // rotate until see transition
{
// found next mark
inc_minutes = 0; // clear flag that need to update minutes
min_pos++; // increment internal count of minutes position
State = Wait_Rotate;
if(is_centered() & (min_pos > 12)) // if rotated to 0 minutes advance hours
{
Hrs++; // advance the hours
if (Hrs > 12) // if hours rolls-over from 12 to 1
{
Hrs = 1; // reset the hours to match roll-over
State = backup; // change state so returning to 1
}
else
State = Advance; // change state so advancing
min_pos = 0; // set internal count of minutes position to 0
}
}
else // if don't yet see transition
goright_big(); // then continue rotating
}
break;
case Wait_Rotate:
delay(1000);
State = Rotate_to_time;
break;
case backup:
if (is_centered() == true) // if aligned to go forward
{
// look for hour marker on correct side
if (last_side == RIGHT) // if last saw hour marker on the right so look on left
{
if ((S5 == BLACK) & (pre_S5 == WHITE)) // look for transition on left
{
last_side = LEFT;
hour_pos--;
if(hour_pos == Hrs)
State = Rotate_to_time;
}
else // if cannot see hour marker yet then go forward
goback();
}
else // last saw hour marker on left so look on right
{
if (S1 == BLACK & pre_S1 == WHITE) // look for transition on right
{
last_side = RIGHT;
hour_pos--;
if(hour_pos == Hrs)
State = Rotate_to_time;
}
else // if cannot see hour marker yet then go forward
goback();
}
}
else // if not aligned to go forward then realign
{
if (S2 == WHITE) // if off black to the right
goleft();
else if (S4 == WHITE) // if off black on the left
goright();
}
break;
default:
break;
// if nothing else matches, do the default
// default is optional
}
delay(40); //50);
botstop();
// Serial.println();
}
boolean is_centered()
{
if (S3 == BLACK & S4 == BLACK & S2 == BLACK)
return true;
else
return false;
}
void goright(){
shieldbot.drive(32,0);
// Serial.print("Right turn");
}
void goright_big(){
shieldbot.drive(18,-18);
// Serial.print("Right turn");
}
void goleft(){
shieldbot.drive(0,32);
// Serial.print("Left turn");
}
void goforward()
{
shieldbot.drive(32,32);
// Serial.print("go fwd");
}
void goback()
{
shieldbot.drive(-32,-32);
// Serial.print("go ba");
}
void botstop()
{
shieldbot.drive(0,0);
}
//Read command from the Arduino serial monitor to set the RTC.
//Case-sensitive and must be entered exactly as (24-hour clock):
// Set yyyy-mm-dd hh:mm:ss
void readCommand(void)
{
int i;
char inChar;
Serial.println("You have 20 seconds to set the time");
Serial.println("format hh:mm:ss");
tmElements_t tmSet;
time_t tSet;
// wait 20 sec for command to set time
for (i=0;i<10;i++)
{
delay(1000);
while (Serial.available()>0) {
tmSet.Hour = Serial.parseInt();
tmSet.Minute = Serial.parseInt();
tmSet.Second = Serial.parseInt();
Serial.print(tmSet.Hour, DEC);
Serial.print(tmSet.Minute, DEC);
Serial.print(tmSet.Second, DEC);
Serial.println();
if (Serial.read() == '\n') {
tSet = makeTime(tmSet); //convert to time_t
setTime(tSet); //set the system time
// RTC.set(now()); //set the rtc
Serial.println("RTC set!");
}
}
}
}