This is a simple counter realized with 3 LEDs and an Arduino UNO.
I connected every LED to a digital pin. Every LED has a 220ohm resistor in series with him.
There is an image (sorry for not perfect quality; I did it with a Nokia 6630):
To write the program I made a project of a logic circuit with three states.
I used the Karnaugh map to minimize the boolean functions of every state.
Finally I implemented it in my code.
Take a look:
/*
Binary Counter with three LED's
The circuit:
* 3 LED's
* 3 220ohm resistors
Created 17/11/2010
By Vito Magnanimo (vitomag89@gmail.com)
*/
#define LED3 3 // pin of LED3
#define LED2 6 // pin of LED2
#define LED1 9 // pin of LED1
int x1=0; // state of LED1
int x2=0; // state of LED2
int x3=0; // state of LED3
//there are auxiliary variables
int new_x1=0; // used to store new LED1 state
int new_x2=0; // used to store new LED2 state
int new_x3=0; // used to stpre new LED3 state
void setup(){
pinMode(LED1,OUTPUT); //set all pin in OUTPUT mode
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
}
void loop(){
delay(1000); // wait 1s before to begin every cicle
// this is useful because if there isnt any
// delay we will not see anything
if (!x3) new_x3=1; // boolean function of the state of third LED
else new_x3=0; //if it's not verified x3 is 0
if( (x2 && !x3) || (!x2 && x3)) new_x2=1; //same thing for second state
else new_x2=0;
if( (!x1 && x2 && x3) || (x1 && !x3) || ( x1 && !x2)) new_x1=1; //same thing for the first state
else new_x1=0;
x1=new_x1; //now new value can be stored in the state variables
x2=new_x2;
x3=new_x3;
//turn ON or turn OFF LED 1 2 3
if(x1==1) digitalWrite(LED1,HIGH);
else digitalWrite(LED1,LOW);
if(x2==1) digitalWrite(LED2,HIGH);
else digitalWrite(LED2,LOW);
if(x3==1) digitalWrite(LED3,HIGH);
else digitalWrite(LED3,LOW);
}