The seven-segment display consists of 8 segments, also counting the dot. Each segment is an individual LED. The type I used has two vcc pins, this middle ones, and 1 pin for each segment.
To show the digits we are going to build the following circuit. Note that you might have a seven-segment display which has a gnd pin instead of a vcc pin. This way you won't need the transistors as I need did.
The shopping list is as follows:
1 x Arduino Duemilanove
1 x seven-segment display (vcc in)
7 x 10 kOhm resisitor (brown, black, orange)
7 x transistor (C547B)
1 x 150 Ohm resistor (brown, green, brown)
1 x USB connector cable
Using the 5 Volt and ground pin on the Arduino board, we can power the circuit. The pins dig7 - dig 13 are connected to the transistors. By enabling a pin a current will flow through the corresponding segment.
Well that's the first part. Up next is programming the Arduino. This is quite annoying, as we'll have to tell the arduino which pins to enable for each digit. While running the Arduino will count till 10, on which it will start over with 0.
/* define the segments */
int ledPinMid = 13;
int ledPinLeftUp = 12;
int ledPinUp = 11;
int ledPinRightUp = 10;
int ledPinLeftDown = 9;
int ledPinDown = 8;
int ledPinRightDown = 7;
int i =0;
void setup() {
// initialize the digital pins as output:
pinMode(ledPinMid, OUTPUT);
pinMode(ledPinLeftUp, OUTPUT);
pinMode(ledPinUp, OUTPUT);
pinMode(ledPinRightUp, OUTPUT);
pinMode(ledPinLeftDown, OUTPUT);
pinMode(ledPinDown, OUTPUT);
pinMode(ledPinRightDown, OUTPUT);
}
void clear()
{
digitalWrite(ledPinMid, LOW);
digitalWrite(ledPinLeftUp, LOW);
digitalWrite(ledPinUp, LOW);
digitalWrite(ledPinRightUp, LOW);
digitalWrite(ledPinLeftDown, LOW);
digitalWrite(ledPinDown, LOW);
digitalWrite(ledPinRightDown, LOW);
}
void setNumber(int number)
{
/* disable all pins */
clear();
/* enable the pins for the current number */
switch (number)
{
case 1:
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinRightDown, HIGH);
break;
case 2:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinLeftDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 3:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 4:
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinRightDown, HIGH);
break;
case 5:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 6:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinLeftDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 7:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinRightDown, HIGH);
break;
case 8:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinLeftDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 9:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinMid, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
case 0:
digitalWrite(ledPinUp, HIGH);
digitalWrite(ledPinLeftUp, HIGH);
digitalWrite(ledPinRightUp, HIGH);
digitalWrite(ledPinRightDown, HIGH);
digitalWrite(ledPinLeftDown, HIGH);
digitalWrite(ledPinDown, HIGH);
break;
}
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
/* set current number */
setNumber(i);
i ++;
if (i == 10) // after 10 second, reset
i = 0;
delay(1000); // wait for a second
}
Well that's it for today. Let's take a look at the result.
Geen opmerkingen:
Een reactie posten