byte pin[] = {2, 3, 4, 5, 6, 7, 8}; // Add pin 9 for the DP
byte GBTN = 10;
byte YBTN = 11;
byte RBTN = 12;
byte num[10][8] = {
{0, 0, 0, 0, 0, 0, 1, 0}, //0
{1, 0, 0, 1, 1, 1, 1, 0}, //1
{0, 0, 1, 0, 0, 1, 0, 0}, //2
{0, 0, 0, 0, 1, 1, 0, 0}, //3
{1, 0, 0, 1, 1, 0, 0, 0}, //4
{0, 1, 0, 0, 1, 0, 0, 0}, //5
{0, 1, 0, 0, 0, 0, 0, 0}, //6
{0, 0, 0, 1, 1, 1, 1, 0}, //7
{0, 0, 0, 0, 0, 0, 0, 0}, //8
{0, 0, 0, 1, 1, 0, 0, 0}, //9
};
byte status = 0;
unsigned long startTime = 0;
int interval = 1000;
void setup() {
//---Set the pins for lighting up the segments
for (byte x = 0; x < 9; x++) {
pinMode(pin[x], OUTPUT);
}
// --- Make sure segments are high (off)
for(byte i = 0; i <= 7; i++){
digitalWrite(pin[i], 1);
}
// --- Set button pins
pinMode (GBTN, INPUT);
pinMode (YBTN, INPUT);
pinMode (RBTN, INPUT);
Serial.begin(9600);
}
// --- Start watching for pinstates and act accordingly
void loop() {
if (digitalRead(GBTN) == LOW) {//green button counts 0-9
status = 3;
} else if (digitalRead(YBTN) == LOW) {//yellow button countdown 9-0
status = 2;
} else if (digitalRead(RBTN) == LOW) {//red button stop
status = 1;
}else{
status=0;
}
if(status != 0){goBtn(status);}
}
// --- Function to handle counter routine..without using DELAYS!!
void goBtn(byte stat){
if (stat == 3) {
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 9; b++) {
startTime = millis();
digitalWrite(pin[b], num[a][b]);
}
while (millis() - startTime <= interval){
// ---While twiddling thumbs, check the buttons and break if
// ---one has been pushed
if(digitalRead(RBTN) == LOW||digitalRead(YBTN) == LOW){return;}
}
a = (a == 9 ? -1 : a);
}
} else if (stat == 2) {
for (int a = 9; a >= 0; a--) {
for (int b = 8; b > -1; b--) {
startTime = millis();
digitalWrite(pin[b], num[a][b]);
}
while (millis() - startTime <= interval){
if(digitalRead(GBTN) == LOW||digitalRead(RBTN) == LOW){return;}
}
a = (a == 0 ? 10 : a);
}
} else if (stat == 1) {
// --- If red button was pushed, turn out the lights please
for(byte i = 0; i <= 7; i++){
digitalWrite(pin[i], 1);
}
}
}