// --------------------------------------------------------------------------
// cnt
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// pinout for Arduino UNO
// D8-A, D9-B, D10-C, D11-D, D12-E, D13-F, D7-G
// D14-reset
// D15-counter 6 to 0
// D16-counter 6 to 0
// D17-counter 9 to 0
// --------------------------------------------------------------------------
#define RESET_PIN 14
#define CNT1_6_0_PIN 15
#define CNT2_9_0_PIN 16
#define CNT3_9_0_PIN 17
#define G_PIN 7
// --------------------------------------------------------------------------
//
// --------------------------------------------------------------------------
void disp(int);
void count(int);
// --------------------------------------------------------------------------
// setup()
// --------------------------------------------------------------------------
void setup() {
DDRB = 0x3F; // set D8-D13 as outputs
pinMode(G_PIN, OUTPUT);
// 7-seg off
PORTB = 0xFF;
digitalWrite(G_PIN, HIGH);
pinMode(RESET_PIN, INPUT_PULLUP);
pinMode(CNT1_6_0_PIN, INPUT_PULLUP);
pinMode(CNT2_9_0_PIN, INPUT_PULLUP);
pinMode(CNT3_9_0_PIN, INPUT_PULLUP);
}
// --------------------------------------------------------------------------
// loop()
// --------------------------------------------------------------------------
void loop() {
static boolean cnt1_6_0_in_old = false;
static boolean cnt2_9_0_in_old = false;
static boolean cnt3_9_0_in_old = false;
delay(10); // reduces port activity rate and also serves as a debouncer
if(!digitalRead(RESET_PIN)) {
PORTB = 0xFF;
digitalWrite(G_PIN, HIGH);
return;
}
boolean cnt1_6_0_in = !digitalRead(CNT1_6_0_PIN);
boolean cnt2_9_0_in = !digitalRead(CNT2_9_0_PIN);
boolean cnt3_9_0_in = !digitalRead(CNT3_9_0_PIN);
if(!cnt1_6_0_in_old && cnt1_6_0_in)
count(6);
else if((!cnt2_9_0_in_old && cnt2_9_0_in) || (!cnt3_9_0_in_old && cnt3_9_0_in))
count(9);
cnt1_6_0_in_old = cnt1_6_0_in;
cnt2_9_0_in_old = cnt2_9_0_in;
cnt3_9_0_in_old = cnt3_9_0_in;
}
// --------------------------------------------------------------------------
// count()
// --------------------------------------------------------------------------
void count(int n) {
for(int i=n; i>-1; i--) {
disp(i);
delay(1000);
}
}
// --------------------------------------------------------------------------
// disp()
// --------------------------------------------------------------------------
void disp(int n) {
switch(n) {
case 0: PORTB = 0x00; digitalWrite(G_PIN, HIGH); break;
case 1: PORTB = 0x39; digitalWrite(G_PIN, HIGH); break;
case 2: PORTB = 0x24; digitalWrite(G_PIN, LOW); break;
case 3: PORTB = 0x30; digitalWrite(G_PIN, LOW); break;
case 4: PORTB = 0x19; digitalWrite(G_PIN, LOW); break;
case 5: PORTB = 0x12; digitalWrite(G_PIN, LOW); break;
case 6: PORTB = 0x02; digitalWrite(G_PIN, LOW); break;
case 7: PORTB = 0x38; digitalWrite(G_PIN, HIGH); break;
case 8: PORTB = 0x00; digitalWrite(G_PIN, LOW); break;
default: PORTB = 0x10; digitalWrite(G_PIN, LOW); break;
}
}