Bis 99 zählen
const int COUNT_DELAY_MS = 1000;
const int DELAY_MS = 5;
const int SEGMENT_PIN[] = { 8, 7, 6, 5, 4, 3, 2 };
const int SEGMENT_PIN_COUNT = 7;
const int DISPLAY_PIN[] = { 9, 10 };
const int DISPLAY_PIN_COUNT = 2;
const byte DIGIT[] = {
// abcdefg
B01111110, // 0
B00110000, // 1
B01101101, // 2
B01111001, // 3
B00110011, // 4
B01011011, // 5
B01011111, // 6
B01110000, // 7
B01111111, // 8
B01111011 // 9
};
const int DIGIT_COUNT = 10;
byte number;
unsigned long nextCount;
void setup() {
int i = 0;
while (i < SEGMENT_PIN_COUNT) {
pinMode(SEGMENT_PIN[i], OUTPUT);
i = i + 1;
}
i = 0;
while (i < DISPLAY_PIN_COUNT) {
pinMode(DISPLAY_PIN[i], OUTPUT);
i = i + 1;
}
number = 0;
nextCount = millis() + COUNT_DELAY_MS;
}
void showByte(byte b) {
byte i = 0;
while (i < SEGMENT_PIN_COUNT) {
if ((b & (1 << i)) != 0) {
digitalWrite(SEGMENT_PIN[i], HIGH);
}
else {
digitalWrite(SEGMENT_PIN[i], LOW);
}
i = i + 1;
}
}
void loop() {
// Display 0 einschalten (LOW), 1 ausschalten (HIGH)
digitalWrite(DISPLAY_PIN[0], LOW);
digitalWrite(DISPLAY_PIN[1], HIGH);
showByte(DIGIT[number % 10]);
delay(DELAY_MS);
// Display 0 ausschalten (HIGH), 1 einschalten (LOW)
digitalWrite(DISPLAY_PIN[0], HIGH);
digitalWrite(DISPLAY_PIN[1], LOW);
showByte(DIGIT[(number / 10) % 10]);
delay(DELAY_MS);
unsigned long now = millis();
if (nextCount <= now) {
number = number + 1;
if (number > 99) {
number = 0;
}
nextCount = now + COUNT_DELAY_MS;
}
}