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
Serial.begin(9600);      // open the serial port at 9600 bps: 
}
 
void loop()
{
      val = analogRead(0);    // read the input pin
      Serial.println(val); 
      digitalWrite(2, (val>>2 & B00000001)); // -------X Decimal value 1
      digitalWrite(3, (val>>2 & B00000010)); // ------X- Decimal value 2
      digitalWrite(4, (val>>2 & B00000100)); // -----X-- Decimal value 4
      digitalWrite(5, (val>>2 & B00001000)); // ----X--- Decimal value 8
      digitalWrite(6, (val>>2 & B00010000)); // ---X---- Decimal value 16
      digitalWrite(7, (val>>2 & B00100000)); // --X----- Decimal value 32
      digitalWrite(8, (val>>2 & B01000000)); // -X------ Decimal value 64
      digitalWrite(9, (val>>2 & B10000000)); // X------- Decimal value 128
      delay(500);
}