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

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 NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...