Monday, December 15, 2014

Digital Counter Using ARDUINO UNO

Arduino digital counter with 7-segment multiplexing technique and shift register
A digital counter is one of the simplest Arduino projects, this counter counts from 0 to 9999.
Requirements:
- Arduino uno board
- 74HC164 shift register
- 4NB 7 segment common anode display
- 4 PNP transistors
- 4X10k resistors
- 7X22 ohm resistors
- 4.7k resistor
- Push button switch.....
Arduino 4-digit 7-segment display digital counter circuit:
Arduino digital counter with button 4-digit 7-segment display and shift register

Arduino 4-digit 7-segment display digital counter:
const unsigned char srdata=2, srclk=3, digit4=4, digit3=5, digit2=6, digit1=7;
unsigned int i = 0;
unsigned char d=0,k=0,l=0,m=0,n=0,s=0;

int seg (){
  if (k==0) l=64;
  if (k==1) l=121;
  if (k==2) l=36;
  if (k==3) l=48;
  if (k==4) l=25;
  if (k==5) l=18;
  if (k==6) l=2;
  if (k==7) l=120;
  if (k==8) l=0;
  if (k==9) l=16;
  return k;
}

void setup() {
  pinMode(srdata, OUTPUT);
  pinMode(srclk, OUTPUT);
  pinMode(digit4, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit1, OUTPUT);
  pinMode(8, INPUT_PULLUP);
  }

void loop(){
 if(s==0){
   if(digitalRead(8)==LOW){s=1;i++;if(i>9999) i=0;}}
   if(digitalRead(8)==HIGH) s=0;
 if(d==0) {digitalWrite(digit4,LOW); digitalWrite(digit3,HIGH);
           digitalWrite(digit2,HIGH);digitalWrite(digit1,HIGH);
 k=i%10;
 seg();
 n=0;
 for(m=0;m<7;m++){
   digitalWrite (srdata,bitRead(l,n));
   digitalWrite (srclk,1);
   digitalWrite (srclk,0);n++;
   }delay(4);
   d=1;
 }
 
 if(d==1) {digitalWrite(digit4,HIGH); digitalWrite(digit3,LOW);
           digitalWrite(digit2,HIGH);digitalWrite(digit1,HIGH);
 k=(i/10)%10;
 seg();
 n=0;
 for(int m=0;m<7;m++){
   digitalWrite (srdata,bitRead(l,n));
   digitalWrite (srclk,1);
   digitalWrite (srclk,0);n++;
   }delay(4);d=2;
 }
 
 if(d==2) {digitalWrite(digit4,HIGH); digitalWrite(digit3,HIGH); 
           digitalWrite(digit2,LOW);digitalWrite(digit1,HIGH);
 k=(i/100)%10;
 seg();
 n=0;
 for(int m=0;m<7;m++){
   digitalWrite (srdata,bitRead(l,n));
   digitalWrite (srclk,1);
   digitalWrite (srclk,0);n++;
   }delay(4);d=3;
 }
 
 if(d==3) {digitalWrite(digit4,HIGH); digitalWrite(digit3,HIGH);
           digitalWrite(digit2,HIGH);digitalWrite(digit1,LOW);
 k=(i/1000)%10;
 seg();
 n=0;
 for(int m=0;m<7;m++){
   digitalWrite (srdata,bitRead(l,n));
   digitalWrite (srclk,1);
   digitalWrite (srclk,0);n++;
   }delay(4);d=0;
 }
}