Code Example

int val = 0;           // variable to store the value read
 
void setup()
{
DDRD = DDRD | B11111100; //Data Direction Register for Port D
DDRB = DDRB | B00000011; //Data Direction Register for Port B
}
 
void loop()
{
      val = analogRead(0);    // read the input pin
      digitalWrite(2, (val & B00000001)); // -------X Decimal value 1
      digitalWrite(3, (val & B00000010)); // ------X- Decimal value 2
      digitalWrite(4, (val & B00000100)); // -----X-- Decimal value 4
      digitalWrite(5, (val & B00001000)); // ----X--- Decimal value 8
      digitalWrite(6, (val & B00010000)); // ---X---- Decimal value 16
      digitalWrite(7, (val & B00100000)); // --X----- Decimal value 32
      digitalWrite(8, (val & B01000000)); // -X------ Decimal value 64
      digitalWrite(9, (val & B10000000)); // X------- Decimal value 128
      delay(20);
   }

}