NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » LED Array Clock

January 01, 2012
by SirThorfinn
SirThorfinn's Avatar
1
2
3
Hi, I'm relatively new to the Nerdkits and I just finished the LED Array and decided to create my own clock with it like i saw in the demonstration video. I don't know if there is allready a code for this or not and i'm definitely sure the way I coded it is not the most efficient way but i'm still learning with this and in my High schools computer science course. I took the most basic (in my opinion) approach to codeing it by essentially creating a ton of nested If and else/if statements. Also once I had the code and ran the clock after a while i noticed it lags behind real time and is slow by about 2 min every 15 min.
 
I noticed that the code might be causeing the controller to take extra time to read the code then wait 1000 milliseconds so I wen't back to the code and changed the delay from 1000 milliseconds to 871 after doing some simple calculations. I'm still tring to find the perfect delay because its barley to fast now. If anyone can show me how to find a good clock delay or at least a less lengthy coding would be greatly appreciated, Thanks.

Here is my code. Oh and this is a method i created inside the template so if you want to try it out you need the method declared in the main method but i'm sure you guys are way ahead of me there, :)

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
// MY MEATHOD!!!
 
void clock() {
 //H = Hours
 //M = Min
 //AM = Display AM or PM
 //blink = used in ":" blink
 int H=10, M=36, S=0, AM=0, blink=0;
  
 int8_t offset=0;
 ledarray_blank();
  
 while(1){
 ledarray_blank();
 offset=0;
  
 //Hours display
  if (H==0){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (H==1){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (H==2){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (H==3){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (H==4){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (H==5){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (H==6){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (H==7){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (H==8){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (H==9){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (H==10){
  font_display('1', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (H==11){
  font_display('1', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (H==12){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  H=0;
  if (AM=0){
   AM++;
   }
  else {
   AM=0;
   }
  }
   
 //End of hours
  
 //Blinking ":"
  if (blink==0){
  font_display(':', offset); offset += font_width(':')+1; delay_ms(0);
  blink=1;
  }
  else{
  font_display(' ', offset); offset += font_width(':')+1; delay_ms(0);
  blink=0;
  }
  
 //Min Display
  if (M==0){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==1){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==2){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==3){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==3){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==4){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==5){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==6){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==7){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==8){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==9){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==10){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==11){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==12){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==13){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==14){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==15){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==16){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==17){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==18){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==19){
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==20){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==21){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==22){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==23){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==24){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==25){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==26){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==27){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==28){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==29){
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==30){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==31){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==32){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==33){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==34){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==35){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==36){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==37){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==38){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==39){
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==40){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==41){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==42){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==43){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==44){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==45){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==46){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==47){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==48){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==49){
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==50){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  }
  else if (M==51){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('1', offset); offset += font_width('1')+1; delay_ms(0);
  }
  else if (M==52){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('2', offset); offset += font_width('2')+1; delay_ms(0);
  }
  else if (M==53){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('3', offset); offset += font_width('3')+1; delay_ms(0);
  }
  else if (M==54){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('4', offset); offset += font_width('4')+1; delay_ms(0);
  }
  else if (M==55){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  }
  else if (M==56){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('6', offset); offset += font_width('6')+1; delay_ms(0);
  }
  else if (M==57){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('7', offset); offset += font_width('7')+1; delay_ms(0);
  }
  else if (M==58){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('8', offset); offset += font_width('8')+1; delay_ms(0);
  }
  else if (M==59){
  font_display('5', offset); offset += font_width('5')+1; delay_ms(0);
  font_display('9', offset); offset += font_width('9')+1; delay_ms(0);
  }
  else if (M==60){
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  font_display('0', offset); offset += font_width('0')+1; delay_ms(0);
  M=0;
  H++;
  }
  offset += font_width('0');
  //end min
   
  //AM or PM Display
  if (AM==1){
  font_display('A', offset); offset += font_width('A')+1; delay_ms(0);
  }
  else{
  font_display('P', offset); offset += font_width('P')+1; delay_ms(0);
  }
   
  //Checking if to add a Min
  if (S==60){
  S=0;
  M++;
  }
   
  //Delay set at 871 because of the small amout of time the
  //Micro Controller takes to proccess.
  //This is not a perfect delay, still perfecting.
  delay_ms(871);
  S++;
  
 }
}

And sorry again for the crazy lengthy code, lol.

January 01, 2012
by SirThorfinn
SirThorfinn's Avatar

Sorry, Format mess up on accident! I'm on an iPad and it's stupid at times

January 02, 2012
by pcbolt
pcbolt's Avatar

I posted a response on the other listing for this topic. Check it out and let me know if it helps.

Post a Reply

Please log in to post a reply.

Did you know that a thermometer can be made "faster" by using a bit of math? Learn more...