Skip to content

Instantly share code, notes, and snippets.

@Xowap
Last active August 29, 2015 14:12
Show Gist options
  • Save Xowap/673f1b355c08f906553b to your computer and use it in GitHub Desktop.
Save Xowap/673f1b355c08f906553b to your computer and use it in GitHub Desktop.
int A = 8;
int B = 9;
int C = 10;
int D = 11;
int E = 12;
int F = 13;
int G = 6;
int H = 7;
int D1 = 2;
int D2 = 3;
int D3 = 4;
int D4 = 5;
int BTN = 0;
int BTN2 = 1;
int DS[4] = {D1, D2, D3, D4};
int POW10[4] = {1000, 100, 10, 1};
int DELAY = 55;
class DigitalDisplay {
public:
int n;
DigitalDisplay() :
n(0)
{
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(H, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
for (int i = 0; i < 4; i += 1) {
digitalWrite(DS[i], HIGH);
}
}
void writeDigit(int i) {
bool a, b, c, d, e, f, g;
a = i == 2 || i == 3 || (i >= 5 && i <= 9) || i == 0;
b = (0 <= i && i <= 4) || (7 <= i && i <= 9);
c = i >= 0 && i != 2;
d = i == 0 || i == 2 || i == 3 || i == 5 || i == 6 || i == 8 || i == 9;
e = i == 0 || i == 2 || i == 6 || i == 8;
f = i == 0 || (4 <= i && i <= 6) || i == 8 || i == 9;
g = (2 <= i && i <= 6) || i == 8 || i == 9;
digitalWrite(A, a);
digitalWrite(B, b);
digitalWrite(C, c);
digitalWrite(D, d);
digitalWrite(E, e);
digitalWrite(F, f);
digitalWrite(G, g);
}
void draw() {
for (int i = 3; i >= 0; i -= 1) {
digitalWrite(DS[i], LOW);
if (n >= POW10[i] || (n == 0 && i == 3)) {
int digit = (n / POW10[i]) % 10;
writeDigit(digit);
} else {
writeDigit(-1);
}
delay(2);
writeDigit(-1);
digitalWrite(DS[i], HIGH);
}
}
void setNumber(int n) {
this->n = n;
}
};
DigitalDisplay disp;
void setup() {
pinMode(BTN, INPUT);
pinMode(BTN2, INPUT);
disp.setNumber(0);
}
void loop() {
int cnt = 0;
bool last1 = false, last2 = false;
while (true) {
bool btn1 = digitalRead(BTN) == LOW,
btn2 = digitalRead(BTN2) == LOW;
if (btn1 != last1 && !btn1) {
cnt += 1;
}
if (btn2 != last2 && !btn2) {
cnt -= 1;
}
disp.setNumber(cnt);
last1 = btn1;
last2 = btn2;
disp.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment