int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int buttonUp = 11;
int buttonDown = 12;
int buttonStateUp = 0;
int buttonStateDown = 0;
int lastButtonStateUp = 0;
int lastButtonStateDown = 0;
int count = 0;
unsigned long lastDebounceTimeUp = 0;
unsigned long lastDebounceTimeDown = 0;
unsigned long debounceDelay = 10;
void setup() {
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
}
void displayNumber(int num) {
switch (num) {
case 0:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, LOW);
break;
case 1:
digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW);
break;
case 2:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH);
break;
case 3:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, HIGH);
break;
case 4:
digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH);
break;
case 5:
digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH);
break;
case 6:
digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH);
break;
case 7:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW);
break;
case 8:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH);
break;
case 9:
digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH);
break;
}
}
void loop() {
int readingUp = digitalRead(buttonUp);
int readingDown = digitalRead(buttonDown);
if (readingUp != lastButtonStateUp) {
lastDebounceTimeUp = millis();
}
if (readingDown != lastButtonStateDown) {
lastDebounceTimeDown = millis();
}
if ((millis() - lastDebounceTimeUp) > debounceDelay) {
if (readingUp != buttonStateUp) {
buttonStateUp = readingUp;
if (buttonStateUp == HIGH) {
count++;
if (count > 9) {
count = 0;
}
displayNumber(count);
}
}
}
if ((millis() - lastDebounceTimeDown) > debounceDelay) {
if (readingDown != buttonStateDown) {
buttonStateDown = readingDown;
if (buttonStateDown == HIGH) {
count--;
if (count < 0) {
count = 9;
}
displayNumber(count);
}
}
}
lastButtonStateUp = readingUp;
lastButtonStateDown = readingDown;
}