Led Clock Coding Tutorial

Jan 30, 2012 No Comments by

As you might know, or will know in three seconds, I have made a Binary Led Clock.  I posted a tutorial on how to build it with physical materials, by soldering and swearing. This time I will walk you through the code, and guess what, the code have hidden features that I have not implemented suport for in the current physical version of the clock.

Okay, lets start of with the pin declaration:

int sekData = 11, minData = 8, hourData = 5;
int sekClock = 12, minClock = 9, hourClock = 6;
int sekLatch = 13, minLatch = 10, hourLatch = 7;

As you can see, I use nine pins, three for each shift register. Then I also have to define them as output pins in the Arduino enviroment.

  pinMode(sekData, OUTPUT); 
  pinMode(sekClock, OUTPUT ); 
  pinMode(sekLatch, OUTPUT);

  pinMode(minData, OUTPUT); 
  pinMode(minClock, OUTPUT ); 
  pinMode(minLatch, OUTPUT);

  pinMode(hourData, OUTPUT); 
  pinMode(hourClock, OUTPUT ); 
  pinMode(hourLatch, OUTPUT);

Then I must assign a variable to store the time in minutes and hours, notice that I don’t do anything about the seconds, this is because I don’t feel that I need the functionality to set the seconds.

  int i = 13;
  int j = 43;

i is for hours and j is for minutes.

Then the first thing I want to do in the setup method after making the pinMode statements is to flush all the shift registers to avoid having old data in them. This is done by calling three methods I have written. Also I’m attaching two interrupts, and we have code that looks like this:

  //Flasher sekund ledsa
  updateSek(0);
  //Flusher min og time registeret
  updateMin(j);
  updateHour(i);
  //Setter interrup handlere
  attachInterrupt(0, setMin, RISING);
  attachInterrupt(1, setTime, RISING);

As you might see the two interrupts are calling on two methods called setMin and setTime, which basically means set minute and set hour. Don’t mind my Norwegian coding style ;) It is mainly to confuse myself ;)
But anyways, these two methods are defined like this:

void setMin(){
  j++;
  if(j == 60)
  {
    j = 0;
  }
  digitalWrite(minLatch, LOW);
  shiftOut(minData, minClock, MSBFIRST, j);
  digitalWrite(minLatch, HIGH);
}

void setTime(){
  i++;
  if(i == 24){
    i = 0;
  }
  digitalWrite(hourLatch, LOW);
  shiftOut(hourData, hourClock, MSBFIRST, i);
  digitalWrite(hourLatch, HIGH);
}

Ok, now it is time to start up the clock with the loop method, and this method is really simple, it waits one second, and updates the seconds counter, and if necessary, the minutes and hours as well. And it all looks like this:

void loop(){

  //løper gjennom alle sekundene
    for(int k = 0; k < 61; k++){
       if(k == 60){
        k = 0;
        j++;
         if(j == 60){
          j = 0;
          i++;
          if(i == 24){
            i = 0;
          }
         } 
       }
      // Serial.print(k);
      // Serial.print("-");
      // Serial.print(j);
      // Serial.print("-");
      // Serial.println(i);
       updateSek(k);
       updateMin(j);
       updateHour(i);
       delay(1000);
    }
}

And then, at last the three methods that updates the shift register

void updateSek(int value){
  digitalWrite(sekLatch, LOW);
  shiftOut(sekData, sekClock, MSBFIRST, value);
  digitalWrite(sekLatch, HIGH);
}

void updateMin(int value){
  digitalWrite(minLatch, LOW);
  shiftOut(minData, minClock, MSBFIRST, value);
  digitalWrite(minLatch, HIGH);
}

void updateHour(int value){
  digitalWrite(hourLatch, LOW);
  shiftOut(hourData, hourClock, MSBFIRST, value);
  digitalWrite(hourLatch, HIGH);
}

And now, at last I will put all this code together for you to enjoy in case you didn’t manage to put it together in the right fashion yourself

//Setter pins på shift registerne
int sekData = 11, minData = 8, hourData = 5;
int sekClock = 12, minClock = 9, hourClock = 6;
int sekLatch = 13, minLatch = 10, hourLatch = 7;

//Setter bare litt tilfeldig starttid på klokka
  int i = 13;
  int j = 43; 

void setup(){

  //PinMode på alle shiftregisterne
 pinMode(sekData, OUTPUT); 
 pinMode(sekClock, OUTPUT ); 
  pinMode(sekLatch, OUTPUT);

 pinMode(minData, OUTPUT); 
 pinMode(minClock, OUTPUT ); 
  pinMode(minLatch, OUTPUT);

 pinMode(hourData, OUTPUT); 
 pinMode(hourClock, OUTPUT ); 
  pinMode(hourLatch, OUTPUT);
  Serial.begin(9600);
  //Flasher sekund ledsa
  updateSek(0);
    //Flusher min og time registeret
  updateMin(j);
  updateHour(i);
  //Setter interrup handlere
  attachInterrupt(0, setMin, RISING);
  attachInterrupt(1, setTime, RISING);

}
//To metoder for stillig av klokka
void setMin(){
  j++;
  if(j == 60)
  {
    j = 0;
  }
  digitalWrite(minLatch, LOW);
  shiftOut(minData, minClock, MSBFIRST, j);
  digitalWrite(minLatch, HIGH);
}

void setTime(){
  i++;
  if(i == 24){
    i = 0;
  }
  digitalWrite(hourLatch, LOW);
  shiftOut(hourData, hourClock, MSBFIRST, i);
  digitalWrite(hourLatch, HIGH);
}

void loop(){

  //løper gjennom alle sekundene
    for(int k = 0; k < 61; k++){
       if(k == 60){
        k = 0;
        j++;
         if(j == 60){
          j = 0;
          i++;
          if(i == 24){
            i = 0;
          }
         } 
       }
     //  Serial.print(k);
      // Serial.print("-");
      // Serial.print(j);
      // Serial.print("-");
      // Serial.println(i);
       updateSek(k);
        updateMin(j);
        updateHour(i);
       delay(1000);
    }
}

//The f do u think?
void updateSek(int value){
  digitalWrite(sekLatch, LOW);
  shiftOut(sekData, sekClock, MSBFIRST, value);
  digitalWrite(sekLatch, HIGH);
}

void updateMin(int value){
  digitalWrite(minLatch, LOW);
  shiftOut(minData, minClock, MSBFIRST, value);
  digitalWrite(minLatch, HIGH);
}

void updateHour(int value){
  digitalWrite(hourLatch, LOW);
  shiftOut(hourData, hourClock, MSBFIRST, value);
  digitalWrite(hourLatch, HIGH);
}

Happy thrill.

Arduino, Code

About the author

Sometime I write, sometime I don't. But most of the time I program, build a curcuit or just do something completely different.
No Responses to “Led Clock Coding Tutorial”

Leave a Reply