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

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, :)

// 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

I redid my other thread because of the format accident...

January 02, 2012
by pcbolt
pcbolt's Avatar

Hi SirThorfinn-

In the "//hours display" section of your code, I would replace it with:

if (H==12) {H = 0;}
lo_digit = (H % 10) + 0x30;
hi_digit = (H / 10) + 0x30;
font_display(hi_digit, offset); offset += font_width(hi_digit)+1; delay_ms(0);
font_display(lo_digit, offset); offset += font_width(lo_digit)+1; delay_ms(0);

And in the "//Min display" section, I would use:

if (M==60) {M = 0; H++;}
lo_digit = (M % 10) + 0x30;
hi_digit = (M / 10) + 0x30;
font_display(hi_digit, offset); offset += font_width(hi_digit)+1; delay_ms(0);
font_display(lo_digit, offset); offset += font_width(lo_digit)+1; delay_ms(0);

Of course you have to declare "lo_digit" and "hi_digit" as uint8_t types. The reason this works is because when you use '0' in your code, the compiler automatically replaces it with its byte code equivalent 0x30 (48 in decimal). Likewise '1' becomes 0x31 etc. The other parts just use integer division and modulus (remainder) operators to dissect the two digit number.

January 02, 2012
by pcbolt
pcbolt's Avatar

Oh and if you still have timing issues, watch the Nerdkit video called "Crystal Real Time Clock". It uses interrupts to accurately use the MCU timer. Good luck.

January 02, 2012
by SirThorfinn
SirThorfinn's Avatar

Thanks, I'll see how it goes

Post a Reply

Please log in to post a reply.

Did you know that binary numbers use base 2 to represent numbers, and these are important for understanding microcontroller registers? Learn more...