#define A 13
#define B 12
#define C 11
#define D 10
#define E 9
#define F 8
#define G 7
#define CLK 2
#define DT 3
#define SW 4
boolean ledCathode = false; //if you using anode 7 segment display, Make it true
int currentStateCLK, lastStateCLK, start;
unsigned long lastButtonPress = 0;
unsigned int counter = 0;
int countDelay = 1000; // Countdown delay
byte numeral[11] = {
B0000001,//0
B1001111,//1
B0010010,//2
B0000110,//3
B1001100,//4
B0100100,//5
B0100000,//6
B0001111,//7
B0000000,//8
B0001100,//9
B0111000 //F
};
const int segmentPins[7] = {G, F, E, D, C, B, A};
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 7; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
counter = 0;
}
void loop()
{
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
counter --;
} else {
counter ++;
}
}
lastStateCLK = currentStateCLK;
Serial.println(counter);
if (counter > 0 && counter <= 10) {
showDigit(counter);
} else if (counter == 11) {
counter = 0;
showDigit(counter);
} else {
counter = 0;
showDigit(counter);
}
int btnState = digitalRead(SW);
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
for (start = counter; start >= 0; start -- ) {
showDigit(start);
delay(countDelay);
}
counter = 0;
showDigit(counter);
}
lastButtonPress = millis();
}
delay(1);
}
void showDigit( int number)
{
boolean isBitSet;
for (int segment = 0; segment < 7; segment++)
{
isBitSet = bitRead(numeral[number], segment);
if (ledCathode == true) {
isBitSet = ! isBitSet;
}
digitalWrite( segmentPins[segment], isBitSet);
}
}