const int button1 = A0;
const int button2 = A1;
const int button3 = A2;
const int button4 = A3;
int countTens = 0;
int countOnes = 0;
int blinks = 0;
bool state1 = false;
bool state2 = false;
bool state3 = false;
bool state4 = false;
long interval = 100;
unsigned long prevTime = 0;
//The line below is the array containing all the binary numbers for the digits on a SSD from 0 to 9
const int number[11] = {0b0111111,
0b0000110, 0b1011011, 0b1001111,
0b1100110, 0b1101101, 0b1111101,
0b0000111, 0b1111111, 0b1101111,
0b0000000};
void setup(){
for (int i = 0; i <= 13; i++)
pinMode(i, OUTPUT); //Set all pins from 0 to 13 as OUTPUT
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
for(int i = 0,z = 0;i < 7;i++, z++){ // defaults displays to 00
digitalWrite(i, bitRead(number[0],z));
}
for(int j = 7,c = 0;j <= 13;j++, c++){
digitalWrite(j, bitRead(number[0],c));
}
}
void loop(){
unsigned long currentTime = millis();
if(digitalRead(button1) == LOW){
state1 = true;
state2 = false;
state3 = false;
state4 = false;
countOnes = 9;
countTens = 9;
}
else if(digitalRead(button2) == LOW){
state1 = false;
state2 = true;
state3 = false;
state4 = false;
countOnes = 0;
countTens = 0;
}
else if(digitalRead(button3) == LOW){
state1 = false;
state2 = false;
state3 = true;
state4 = false;
countOnes = 0;
countTens = 9;
}
else if(digitalRead(button4) == LOW){
state1 = false;
state2 = false;
state3 = false;
state4 = true;
blinks = 0;
}
if(state1){
if((currentTime - prevTime)>interval){
resetDisplay();
display_ones(countOnes);
incDecTens(countTens);
countOnes--;
if(countOnes < 0){
countOnes = 9;
countTens--;
if(countTens < 0)
countTens = 9;
}
prevTime = currentTime;
}
}
else if(state2){
if((currentTime - prevTime)>interval){
resetDisplay();
display_ones(countOnes);
incDecTens(countTens);
countOnes++;
if(countOnes > 9){
countOnes = 0;
countTens++;
if(countTens > 9)
countTens = 0;
}
prevTime = currentTime;
}
}
else if(state3){
if((currentTime - prevTime)>interval){
resetDisplay();
display_ones(countOnes);
incDecTens(countTens);
countOnes++;
if(countOnes > 9)
countOnes = 0;
countTens--;
if(countTens < 0)
countTens = 9;
prevTime = currentTime;
}
}
else if(state4){
if(blinks < 10){
if((currentTime - prevTime)>interval)
turnOff();
if((currentTime - prevTime)>interval*2){
resetDisplay();
prevTime = currentTime;
blinks++;
}
}
}
}
void display_ones(const int x){
int pin2, b;
for (pin2 = 7, b = 0; pin2 <= 13; pin2++, b++) {
digitalWrite(pin2, bitRead(number[x], b));
}
}
void incDecTens(const int t){
int pin1, a;
for (pin1 = 0, a = 0; pin1 < 7; pin1++, a++){
digitalWrite(pin1, bitRead(number[t],a));
}
}
void resetDisplay(){
int pins, l;
for(pins = 0, l = 0;pins < 7;pins++, l++){
digitalWrite(pins, bitRead(number[0],l));
}
for(pins = 7, l = 0;pins <= 13;pins++, l++){
digitalWrite(pins, bitRead(number[0],l));
}
}
void turnOff(){
int pins, l;
for(pins = 0, l = 0;pins < 7;pins++, l++){
digitalWrite(pins, bitRead(number[11],l));
}
for(pins = 7, l = 0;pins <= 13;pins++, l++){
digitalWrite(pins, bitRead(number[11],l));
}
}