// COMMON ANODE 4-digit 7-seg + increment + reset button
int segPins[7] = {21, 19, 18, 5, 17, 16, 4}; // a b c d e f g
int digitPins[4] = {13, 12, 14, 27}; // D1 D2 D3 D4
#define BTN_INC 15 // artırma butonu
#define BTN_RST 26 // sıfırlama butonu
byte ccDigits[10][7] = {
{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1}
};
volatile int number = 0;
// debounce
unsigned long lastDebounceInc = 0;
unsigned long lastDebounceRst = 0;
const unsigned long debounceMs = 40;
int lastIncState = HIGH;
int lastRstState = HIGH;
void setup() {
for (int i=0;i<7;i++) pinMode(segPins[i], OUTPUT);
for (int i=0;i<4;i++) pinMode(digitPins[i], OUTPUT);
pinMode(BTN_INC, INPUT_PULLUP);
pinMode(BTN_RST, INPUT_PULLUP);
// kapalı durumlar
for (int i=0;i<4;i++) digitalWrite(digitPins[i], LOW);
for (int i=0;i<7;i++) digitalWrite(segPins[i], HIGH);
}
void setSegmentsCA(int val) {
for (int i=0;i<7;i++) {
digitalWrite(segPins[i], ccDigits[val][i] ? LOW : HIGH);
}
}
void showDigit(int d, int val) {
for (int i=0;i<4;i++) digitalWrite(digitPins[i], LOW);
setSegmentsCA(val);
digitalWrite(digitPins[d], HIGH);
}
void handleButtons() {
// -------- Artırma --------
int incReading = digitalRead(BTN_INC);
if (incReading != lastIncState) {
lastDebounceInc = millis();
lastIncState = incReading;
}
if ((millis() - lastDebounceInc) > debounceMs && incReading == LOW) {
number++;
if (number > 9999) number = 0;
delay(150); // tek basış
}
// -------- Reset --------
int rstReading = digitalRead(BTN_RST);
if (rstReading != lastRstState) {
lastDebounceRst = millis();
lastRstState = rstReading;
}
if ((millis() - lastDebounceRst) > debounceMs && rstReading == LOW) {
number = 0;
delay(150);
}
}
void loop() {
handleButtons();
int n = number;
int d[4] = { n/1000, (n/100)%10, (n/10)%10, n%10 };
for (int i=0;i<4;i++) {
showDigit(i, d[i]);
delay(2);
}
}