Code Example

/*--- Binary Counting with 8 bits ---*/

void Dlay(){
  delay(20);
}
 
int led0 = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
 
void setup()
{
DDRD = DDRD | B11111100; //Data Direction Register for Port D
DDRB = DDRB | B00000011; //Data Direction Register for Port B
}
 
void loop()
{
   byte ct; 
 
   for(ct=0; ctdigitalWrite(led0, (ct & B00000001)); // -------X Decimal value 1
      digitalWrite(led1, (ct & B00000010)); // ------X- Decimal value 2
      digitalWrite(led2, (ct & B00000100)); // -----X-- Decimal value 4
      digitalWrite(led3, (ct & B00001000)); // ----X--- Decimal value 8
      digitalWrite(led4, (ct & B00010000)); // ---X---- Decimal value 16
      digitalWrite(led5, (ct & B00100000)); // --X----- Decimal value 32
      digitalWrite(led6, (ct & B01000000)); // -X------ Decimal value 64
      digitalWrite(led7, (ct & B10000000)); // X------- Decimal value 128
      Dlay();
   }

}
// Reference http://diyroboticslab.wordpress.com/2009/06/09/blinking-binary-bits-and-bytes/