Skip to content

Instantly share code, notes, and snippets.

@kaminski-tomasz
Created April 26, 2022 20:30
Show Gist options
  • Save kaminski-tomasz/b2a9733db9835d741f4282df549c29e1 to your computer and use it in GitHub Desktop.
Save kaminski-tomasz/b2a9733db9835d741f4282df549c29e1 to your computer and use it in GitHub Desktop.
Arduino code for digital counter device with 4-digit 7-segment display
#define SEG_C 4
#define SEG_E 21
#define SEG_D 20
#define SEG_B 0
#define SEG_G 3
#define SEG_A 2
#define SEG_F 1
#define DISP_4 19
#define DISP_3 18
#define DISP_2 17
#define DISP_1 16
#define NONE 10
#define RESET 9
#define PAUSE 10
#define INCREASE 8
#define DECREASE 14
byte digitSegments[11] = {
//1gfedcba
0b01000000, //0
0b01111001, //1
0b00100100, //2
0b00110000, //3
0b00011001, //4
0b00010010, //5
0b00000010, //6
0b01111000, //7
0b00000000, //8
0b00010000, //9
0b01111111 //NONE
};
byte segmentPin[7] = {
SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G
};
byte displayPin[4] = {
DISP_4, DISP_3, DISP_2, DISP_1
};
volatile int counter = 0;
volatile bool counterVisible = true;
volatile bool pause = false;
int resetLastState = HIGH;
int pauseLastState = HIGH;
int increaseLastState = HIGH;
int decreaseLastState = HIGH;
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPin[i], OUTPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(displayPin[i], OUTPUT);
}
pinMode(RESET, INPUT_PULLUP);
pinMode(PAUSE, INPUT_PULLUP);
pinMode(INCREASE, INPUT_PULLUP);
pinMode(DECREASE, INPUT_PULLUP);
delay(500);
}
#define DIGIT_INTERVAL 2000
#define PAUSE_INTERVAL 200000
void loop() {
showCounter();
int resetState = digitalRead(RESET);
if (resetState == HIGH && resetLastState == LOW) {
reset();
}
resetLastState = resetState;
int pauseState = digitalRead(PAUSE);
if (pauseState == HIGH && pauseLastState == LOW) {
togglePause();
}
pauseLastState = pauseState;
int increaseState = digitalRead(INCREASE);
if (increaseState == HIGH && increaseLastState == LOW) {
increase();
}
increaseLastState = increaseState;
int decreaseState = digitalRead(DECREASE);
if (decreaseState == HIGH && decreaseLastState == LOW) {
decrease();
}
decreaseLastState = decreaseState;
}
void reset() {
counter=0;
pause=false;
counterVisible=true;
}
void togglePause() {
pause = !pause;
if (!pause) counterVisible = true;
}
void increase() {
if (pause) {
return;
}
counter++;
if (counter > 9999) {
counter=0;
}
}
void decrease() {
if (pause) {
return;
}
if (counter != 0) {
counter--;
}
}
void showCounter() {
static int digitPosition = 1;
static int tens = 1;
static unsigned long previousTime;
static unsigned long previousPauseTime;
unsigned long currentTime = micros();
if (pause && (currentTime - previousPauseTime >= PAUSE_INTERVAL)) {
previousPauseTime = currentTime;
counterVisible = !counterVisible;
if (!counterVisible) {
showDigit(NONE);
}
}
if (counterVisible && (currentTime - previousTime >= DIGIT_INTERVAL)) {
previousTime = currentTime;
showDigit(NONE);
digitPosition ++;
tens = tens * 10;
if (digitPosition > 4) {
digitPosition = 1;
tens = 1;
}
setDigitPosition(digitPosition);
if (counter >= tens || (tens == 1 && counter == 0)) {
showDigit((counter / tens) % 10);
}
}
}
void setDigitPosition(int digitPosition) {
digitalWrite(DISP_1, digitPosition != 1);
digitalWrite(DISP_2, digitPosition != 2);
digitalWrite(DISP_3, digitPosition != 3);
digitalWrite(DISP_4, digitPosition != 4);
}
void showDigit( int digit) {
byte segments = digitSegments[digit < 10? digit: NONE];
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPin[i], (segments >> i) & 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment