7-Segment-Displays können in zwei verschiedenen Modifikationen vorkommen, mit einer gemeinsamen Kathode oder mit einer gemeinsamen Anode. Je nachdem, welche Anwendung man umsetzen möchte, ist es wichtig, die passende Komponente zu wählen (siehe auch: Zähler mit 7-Segmentanzeigen).
Hinweis: Sicherheitshalber sollten an jedem Pin der 7-Segment-Anzeige ein Vorwiderstand verwendet werden!
#define LED_SEG_A_PIN 2
#define LED_SEG_B_PIN 3
#define LED_SEG_C_PIN 4
#define LED_SEG_D_PIN 5
#define LED_SEG_E_PIN 6
#define LED_SEG_F_PIN 7
#define LED_SEG_G_PIN 8
#define LED_SEG_DOT_PIN 9
bool isDotActive;
void setup()
{
isDotActive = false;
pinMode(LED_SEG_A_PIN, OUTPUT);
pinMode(LED_SEG_B_PIN, OUTPUT);
pinMode(LED_SEG_C_PIN, OUTPUT);
pinMode(LED_SEG_D_PIN, OUTPUT);
pinMode(LED_SEG_E_PIN, OUTPUT);
pinMode(LED_SEG_F_PIN, OUTPUT);
pinMode(LED_SEG_G_PIN, OUTPUT);
pinMode(LED_SEG_DOT_PIN, OUTPUT);
}
void loop()
{
for(byte i=0; i<10; i++) {
displayDigit(i);
for(byte d=0; d<4; d++) {
toggleDot();
delay(250);
}
}
}
void toggleDot()
{
if (isDotActive == true) {
digitalWrite(LED_SEG_DOT_PIN, HIGH);
isDotActive = false;
} else {
digitalWrite(LED_SEG_DOT_PIN, LOW);
isDotActive = true;
}
}
void displayDigit(byte digit)
{
turnOffDisplay();
if (digit > 9) {
return;
}
//Conditions for displaying segment a
if(digit!=1 && digit != 4) {
digitalWrite(LED_SEG_A_PIN, HIGH);
}
//Conditions for displaying segment b
if(digit != 5 && digit != 6) {
digitalWrite(LED_SEG_B_PIN, HIGH);
}
//Conditions for displaying segment c
if(digit !=2) {
digitalWrite(LED_SEG_C_PIN, HIGH);
}
//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7) {
digitalWrite(LED_SEG_D_PIN, HIGH);
}
//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0) {
digitalWrite(LED_SEG_E_PIN, HIGH);
}
//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7) {
digitalWrite(LED_SEG_F_PIN, HIGH);
}
if (digit!=0 && digit!=1 && digit !=7) {
digitalWrite(LED_SEG_G_PIN, HIGH);
}
}
void turnOffDisplay()
{
digitalWrite(LED_SEG_A_PIN, LOW);
digitalWrite(LED_SEG_B_PIN, LOW);
digitalWrite(LED_SEG_C_PIN, LOW);
digitalWrite(LED_SEG_D_PIN, LOW);
digitalWrite(LED_SEG_E_PIN, LOW);
digitalWrite(LED_SEG_F_PIN, LOW);
digitalWrite(LED_SEG_G_PIN, LOW);
}
zurück