Tech Journal Back to Tech Journal

I used the Arduino to drive a 7-segment LED digit display

I used the Arduino to drive a LED digit (7-segrment). Below is the code I used in the Arduino development software:

#define LEDNUM 8
// The pins connected to A..G and DP, in that order
int ledPins[LEDNUM] = {7, 6, 11, 12, 5, 9, 8, 10};

// on 30/jul/2009:

// 1 == 5 = E
// 2 == 12 = D
// 3 == 11 = C
// 4 == 10 = DP

// 18 == 9 = F
// 17 == 8 = G
// 16 == 7 = A
// 15 == 6 = B

#define DIGITNUM 10
byte digits[DIGITNUM] = {
0x3F, // "0" =0011 1111
0x06, // "1" = 0000 0110
0x5B, // "2" = 0101 1011
0x4F, // "3" = 0100 1111
0x66, // "4" = 0110 0110
0x6D, // "5" = 0110 1101
0x7D, // "6" = 0111 1101
0x07, // "7" = 0000 0111
0x7F, // "8" = 0111 1111
0x6F // "9" = 0110 1111
};

void setup()
{
int i;
for (i=0; i<LEDNUM; ++i) {
pinMode(ledPins[i], OUTPUT); // sets the digital pin as output
}
}

void snakeLight() {
int currentPin = 0;
for (currentPin=0; currentPin<LEDNUM; ++currentPin) {
digitalWrite(ledPins[currentPin],HIGH);
delay(20);
digitalWrite(ledPins[currentPin],LOW);
delay(20);
}
}

void allOff() {
for (int i=0; i<LEDNUM; ++i) {
digitalWrite(ledPins[i],LOW);
}
}

void showDigit(int digit) {
byte d = digits[digit];

int i;
for (i=0; i<LEDNUM; ++i) {
digitalWrite(ledPins[i], d & 1);
d >>= 1;
}
}

#define INTERVAL 1000
long lastMillis = 0;

int currentDigit = 0;
void loop()
{
if (millis() - lastMillis > INTERVAL) {
lastMillis = millis();

snakeLight();
delay(20);
showDigit(currentDigit);
currentDigit = (currentDigit + 1) % DIGITNUM;
delay(100);
}
}

a

Last updated on 2009-07-30 09:50:57 -0700, by Shalom Craimer

Back to Tech Journal