// by Hui Lian
// 2 Digit Seven-Segment LED Display ScoreBoard with Buttons
//Team A's score on the Left digit, Team B's score on the Right digit, up to 19,dots on top left hand side are 10's
//Button1:Team A +/-1, Button2:Team B +/-1,Button3:Clear both when pressed long enough greater than set time,BTN3DownTime
//Button4: combined with button1 & 2, pressed -1, not pressed +1
// Common Anode digit 1 pin 3,8
// Common Anode digit 2 pin 3,8
// G F C1 A B G F C2 A B
// | | | | | | | | | | -> pins and segments they control
// ----------- -----------
// | A | | A |
// F| |B F| |B
// |----G----| |----G----|
// E| |C E| |C
// | D | | D |
// -----------#DP -----------#DP
// | | | | | | | | | | -> pins and segments they control
// E D C1 C DP E D C2 C DP
// Segments that make each number when lit:
// 0 => -FEDCBA
// 1 => ----BC-
// 2 => G-ED-BA
// 3 => G--DCBA
// 4 => GF--CB-
// 5 => GF-DC-A
// 6 => GFEDC-A
// 7 => ----CBA
// 8 => GFEDCBA
// 9 => GF-DCBA
// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8
#define DP 9
// Pushbuttons connected to pins A0,A1,A2 & A3
//BTN1:Left digit +1,BTN2:Right digit +1,BTN3:Clear both digits,BTN4:when being held down to decremet by 1 with
//BTN1 for left digit and BTN2 for right digit BTN1 for left digit
#define BTN1 A0
#define BTN2 A1
#define BTN3 A2
#define BTN4 A3
// Pins driving common anodes
#define CA1 10
#define CA2 11
// Pins for A B C D E F G,DP in sequence
const int segs[8] = { A, B, C, D, E, F, G,DP};
// Segments that make each number & letter:0,1,2,3,4,5,6,7,8,9,A,b,t in the format of [0b DP G F E D C B A] for each number
const byte numbers[13] = { 0b11000000, 0b11111001, 0b10100100, 0b10110000, 0b10011001, 0b10010010,
0b10000010, 0b11111000, 0b10000000, 0b10010000,0b10001000,0b10000011,0b10000111 };
int digit1 = 0;
int digit1low=0;
int digit2 = 0;
int digit2low=0;
int LastBTN1State=0;
int LastBTN2State=0;
int LastBTN3State=0;
int BTN1State=0;
int BTN2State=0;
int BTN3State=0;
int BTN4State=0;
unsigned long BTN3StartTime =0;
//unsigned long BTN3EndTime =0;
const int BTN3DownTime=900;
void setup() {
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(DP, OUTPUT);
pinMode(BTN1, INPUT);
pinMode(BTN2, INPUT);
pinMode(BTN3, INPUT);
pinMode(BTN4, INPUT);
pinMode(CA1, OUTPUT);
pinMode(CA2, OUTPUT);
//Display "A" for team A on the Left digit (digit1)
//Display "B" for team B on the right digit (digit2)
//twice to inform teams,players...etc
lightDigit1(numbers[10]);
delay(2000);
lightDigit2(numbers[11]);
delay(2000);
lightDigit1(numbers[10]);
delay(2000);
lightDigit2(numbers[11]);
delay(2000);
}
void loop() {
// check button4, as button4 will decide to +1 or -1 the score
BTN4State = digitalRead(BTN4);
// check button1 Team A, if there is a rising edge,change digit1:+1 if BTN4 not pressed,-1 if BTN4 pressed
BTN1State = digitalRead(BTN1);
if (BTN1State != LastBTN1State && BTN1State==HIGH) {
if (BTN4State==LOW) {
digit1++;
digit1 %= 20;
delay(1);
}
else {
if (digit1>0){
digit1=digit1-1;
delay(1);
}
}
}
LastBTN1State=BTN1State;
// check button2 Team B, if there is a rising edge,change digit2:+1 if BTN4 not pressed,-1 if BTN4 pressed
BTN2State = digitalRead(BTN2);
if (BTN2State != LastBTN2State && BTN2State==HIGH) {
if (BTN4State==LOW) {
digit2++;
digit2 %= 20;
delay(1);
}
else {
if (digit2>0){
digit2=digit2-1;
delay(1);
}
}
}
LastBTN2State=BTN2State;
// check button3
//if there is a rising edge: set BTN3 start time, falling edge BTN3 end time
//if the duration is greater than the set time, BTN3DownTime, Clear both digits when button3 released
BTN3State = digitalRead(BTN3);
if (BTN3State != LastBTN3State) {
if (BTN3State==HIGH) {
BTN3StartTime = millis();
// BTN3EndTime = BTN3StartTime;
}
else {
// BTN3EndTime = millis();
}
}
if (((millis()-BTN3StartTime)>BTN3DownTime) && BTN3State==HIGH) {
digit1=0;
digit2=0;
BTN3StartTime=0;
// BTN3EndTime=0;
delay(1);
}
LastBTN3State=BTN3State;
//Anti-Bouncing delay
delay(20);
//Old program:clear both digits when button3 pressed
// check button3
// BTN3State = digitalRead(BTN3);
// if (BTN3State == HIGH) {
// digit1=0;
// digit2=0;
// delay(1);
// }
// display number
// unsigned long startTime = millis();
// for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {}
digit1low=digit1;
if (digit1>9) {
digit1low=digit1-10;
}
lightDigit1(numbers[digit1low]);
delay(5);
digit2low=digit2;
if (digit2>9) {
digit2low=digit2-10;
}
lightDigit2(numbers[digit2low]);
delay(5);
// }
}
void lightDigit1(byte number) {
digitalWrite(CA1, LOW);
digitalWrite(CA2, LOW);
lightSegments(number);
if (digit1>9) {
digitalWrite(DP, LOW);
}
else {
digitalWrite(DP, HIGH);
}
digitalWrite(CA1, HIGH);
}
void lightDigit2(byte number) {
digitalWrite(CA1, LOW);
digitalWrite(CA2, LOW);
lightSegments(number);
if (digit2>9) {
digitalWrite(DP, LOW);
}
else {
digitalWrite(DP, HIGH);
}
digitalWrite(CA2, HIGH);
}
void lightSegments(byte number) {
for (int i = 0; i < 8; i++) {
int bit = bitRead(number, i);
digitalWrite(segs[i], bit);
}
}