int resetBtn = 4, countBtn = 16;
int countAnode = 0, countCathode = 0;
int resetValue, countValue;
int lastBtnState = 1;
int resetBtnState = 1;
int commonAnode[] = {23, 22, 21, 2, 19, 18, 5, 17};
int commonCathode[] = {14, 12, 32, 1, 33, 15, 26, 27};
const int anode[10][8] = {
//Common Anode 7 Segment
{0, 0, 0, 1, 0, 0, 0, 1}, //0
{1, 0, 0, 1, 1, 1, 1, 1}, //1
{0, 0, 1, 1, 0, 0, 1, 0}, //2
{0, 0, 0, 1, 0, 1, 1, 0}, //3
{1, 0, 0, 1, 1, 1, 0, 0}, //4
{0, 1, 0, 1, 0, 1, 0, 0}, //5
{0, 1, 0, 1, 0, 0, 0, 0}, //6
{0, 0, 0, 1, 1, 1, 1, 1}, //7
{0, 0, 0, 1, 0, 0, 0, 0}, //8
{0, 0, 0, 1, 0, 1, 0, 0} //9
};
const int cathode[10][8] = {
//Common Cathode 7 Segment
{1, 1, 1, 0, 1, 1, 1, 0}, //0
{0, 1, 1, 0, 0, 0, 0, 0}, //1
{1, 1, 0, 0, 1, 1, 0, 1}, //2
{1, 1, 1, 0, 1, 0, 0, 1}, //3
{0, 1, 1, 0, 0, 0, 1, 1}, //4
{1, 0, 1, 0, 1, 0, 1, 1}, //5
{1, 0, 1, 0, 1, 1, 1, 1}, //6
{1, 1, 1, 0, 0, 0, 0, 0}, //7
{1, 1, 1, 0, 1, 1, 1, 1}, //8
{1, 1, 1, 0, 1, 0, 1, 1} //9
};
void setup() {
for(int i=0; i<8; i++){
pinMode(commonAnode[i], OUTPUT);
pinMode(commonCathode[i], OUTPUT);
}
pinMode(resetBtn, INPUT);
pinMode(countBtn, INPUT);
Serial.begin(115200);
}
void loop() {
Serial.println("");
resetValue = digitalRead(resetBtn);
countValue = digitalRead(countBtn);
Serial.print("Count Button Value: ");
Serial.print(countValue);
Serial.print(" Reset Button Value: ");
Serial.print(resetValue);
if(countValue == LOW && lastBtnState == HIGH){
if(countAnode == 10){
countAnode = 0;
countCathode++;
}
if(countCathode == 10) countCathode = 0;
counting(countAnode, countCathode);
Serial.print(" Count Anode Value: ");
Serial.print(countAnode);
countAnode++;
}
if(resetValue == LOW && resetBtnState == HIGH){
reset();
}
lastBtnState = countValue;
resetBtnState = resetValue;
delay(50);
}
void counting(int anodeNumber, int cathodeNumber){
for(int b=0; b<8; b++){
digitalWrite(commonAnode[b], anode[anodeNumber][b]);
digitalWrite(commonCathode[b], cathode[cathodeNumber][b]);
Serial.print(" Count Cathode: ");
Serial.print(countCathode);
}
}
void reset(){
countAnode = 0;
countCathode = 0;
counting(0, 0);
}