That is good.
Here a code I did. It is a way to do it.
And please go visite this site A LOT... Arduino - Home
It will help you to code the Arduino.
/*
Version 1.0
Size : 1182 Bytes
This program will count 0 to 15 and display the result in binary.
TESTED and WORKING
The hardware is connected to a transistor to use as a switch.
Need : 4 Transistor : Common NPN ex: 2N3904
4 1 K resistor : between the digital pin and the base of the transistor.
4 LED : Any color
4 330 ohms resistor : current limiting for the LED.
And try to power the LEDs circuit seperated. And don't forget to connect the gnd of the
circuit with the Arduino GND.
Created by Serge Desjardins ---> Techone
Toronto, ON CANADA
Idea from "Bit Math Tutorial by CosineKitty" in Arduino Playground.
*/
const int led[4] = {9,10,11,12};
/* Set Output LEDs ---> A B C D MSD --- LSD
Digital pin 12 11 10 9
Common variables
*/
int pin;
int counting;
void setup()
/*
Set the pin 12, 11 , 10 , 9 to OUTPUT
*/
{
for (pin =0; pin < 4; pin++ )
{
pinMode (led[pin], OUTPUT);
}
}
void loop()
/*
Counting routine from 0 to 15 and wait for 1 second
*/
{
for ( counting = 0; counting < 16; counting++)
{
LEDview();
delay (1000); // The One second delay
}
}
void LEDview()
/*
The LED display subroutine. The trick is to brake down the 4 bit data
( 0 to 15 ---> 0000 to 1111, so I need HIGH or LOW ( 1 or 0 ) Humm ... boolean
isolate the proper bit and shift the bit, so it equal in value 1 or 0,
so I use the AND operator & ( to isolate ) and the SHIFT RIGHT >> ( to move at
the proper spot )
My idea came from "Bit Math Tutorial by CosineKitty" in the Arduino playground
From there, you can use a 7447 to count the data ( change 16 to 10 in the counter line ) or
other use if need parallel data, the only limitation is the digital lines do not come out at the same
time, but in this way, it is almost at the same time. Bear in mind the small delay after each digitalWrite
instruction.
*/
{
digitalWrite (led[3], boolean ((counting & 8)>>3));
digitalWrite (led[2], boolean ((counting & 4)>>2));
digitalWrite (led[1], boolean ((counting & 2)>>1));
digitalWrite (led[0], boolean (counting & 1));
}