Binary Clock

You don't need to use low level direct port i/o to derive binary values and drive pins. The following example uses the Arduino bitRead function to convert from decimal to binary. The advantage of doing this using the arduino pins is that any group of sequential pins can be used for each of the digits.

// bitSet
// demonstrates using the bitSet function to convert a decimal number to Binary


const int nbrHrBits = 4;   // the maximum number of bits in the binary number for hours
const int firstHrPin = 3;  // first pin is connected to the least significant bit of the binary number

const int nbrMinBits = 6;  // minutes
const int firstMinPin = 6;

const int nbrSecBits = 6;  // seconds
const int firstSecPin = 14;  // the analog pins are used as digital to display  seconds

// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)  
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) 

void setup() 
{ 
  for(int i=0; i < nbrHrBits; i++)
    pinMode(i + firstHrPin, OUTPUT);
  for(int i=0; i < nbrMinBits; i++)
    pinMode(i+firstMinPin, OUTPUT);
  for(int i=0; i < nbrSecBits; i++)
    pinMode(i+firstSecPin, OUTPUT);     
} 


void loop()
{
  long time = millis() / 1000;
  int hours = numberOfHours(time);
  int minutes = numberOfMinutes(time);
  int seconds = numberOfSeconds(time);

  showBinary(hours,nbrHrBits,firstHrPin);
  showBinary(minutes,nbrMinBits,firstMinPin); 
  showBinary(seconds,nbrSecBits,firstSecPin);

  delay(1000); 
} 

// this function sets the pins starting from firstPin to the binary value of the given number
void showBinary( int number, int firstPin, int nbrPins)
{
  for(int bit=0; bit < nbrPins; bit++)
  {
    boolean isBitSet =  bitRead(number, bit); // isBitSet will be true if given bit is set i[b][/b]n this channel
    digitalWrite(bit + firstPin, isBitSet);      
  }  
}