John Reilly

Archive for April, 2010

Arduino 4-Digit 7-Segment Display

by jreilly on Apr.13, 2010, under Arduino, Electronics

Source code:

long n = 0;
int x = 100;
int del = 1;
 
void setup(){
//  clearLEDs();
//  testLEDs();
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(1, OUTPUT);
 
}
 
void loop(){
 
  clearLEDs();
  pickDigit(1);
  pickNumber((n/x/1000)%10);
  delayMicroseconds(del);
  clearLEDs();
  pickDigit(2);
  pickNumber((n/x/100)%10);
  delayMicroseconds(del);
  clearLEDs();
  pickDigit(3);
  pickNumber((n/x/10)%10);
  delayMicroseconds(del);
  clearLEDs();  
  pickDigit(4);
  pickNumber(n/x % 10);  
  delayMicroseconds(del);
  if(n/x > 9999){ 
    n = 0;
    delay(5000);
  }
  else n++;
}
 
void pickDigit(int x){
  digitalWrite(13, LOW); // dig 1
  digitalWrite(12, LOW); // dig 2
  digitalWrite( 9, LOW); // dig 3
  digitalWrite( 8, LOW); // dig 4
 
  switch(x){
    case 1: digitalWrite(13, HIGH); break;
    case 2: digitalWrite(12, HIGH); break;
    case 3: digitalWrite(9, HIGH); break;
    case 4: digitalWrite(8, HIGH); break;    
  }
}
 
void pickNumber(int x){
   switch(x){
     case 1: one(); break;
     case 2: two(); break;
     case 3: three(); break;
     case 4: four(); break;
     case 5: five(); break;
     case 6: six(); break;
     case 7: seven(); break;
     case 8: eight(); break;
     case 9: nine(); break;
     default: zero(); break;
   } 
}
 
void clearLEDs(){  
  //digitalWrite(13, HIGH);
 // digitalWrite(12, HIGH);
  //digitalWrite(9, HIGH);
//  digitalWrite(8, HIGH);  
  digitalWrite( 5, HIGH); // A
  digitalWrite( 7, HIGH); // B
  digitalWrite( 4, HIGH); // C
  digitalWrite(11, HIGH); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, HIGH); // F
  digitalWrite( 6, HIGH); // G 
}
 
void one(){
  digitalWrite( 5, HIGH); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, HIGH); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, HIGH); // F
  digitalWrite( 6, HIGH); // G 
}
 
void two(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, HIGH); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, LOW); // E
  digitalWrite( 3, HIGH); // F
  digitalWrite( 6, LOW); // G 
}
 
void three(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, HIGH); // F
  digitalWrite( 6, LOW); // G 
}
 
void four(){
  digitalWrite( 5, HIGH); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, HIGH); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, LOW); // G 
}
 
void five(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, HIGH); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, LOW); // G 
}
 
void six(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, HIGH); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, LOW); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, LOW); // G 
}
 
void seven(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, HIGH); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, HIGH); // F
  digitalWrite( 6, HIGH); // G 
}
 
void eight(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, LOW); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, LOW); // G 
}
 
void nine(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, HIGH); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, LOW); // G 
}
 
void zero(){
  digitalWrite( 5, LOW); // A
  digitalWrite( 7, LOW); // B
  digitalWrite( 4, LOW); // C
  digitalWrite(11, LOW); // D
  digitalWrite(10, LOW); // E
  digitalWrite( 3, LOW); // F
  digitalWrite( 6, HIGH); // G 
}
Comments Off more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...