Created
June 20, 2022 23:01
-
-
Save rubpy/7df449e5832f704c1eb5798eef0464b1 to your computer and use it in GitHub Desktop.
A (better-coded) example of a 0-to-9 counter running on an Arduino & displaying the digits on a 7-segment LED display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
/** | |
* A (better-coded) example of a 0-to-9 counter running on | |
* an Arduino & displaying the digits on a 7-segment LED display. | |
* | |
* Based on: https://create.arduino.cc/projecthub/akhtar/simple-0-9-counter-dcba41 | |
* | |
* Try it in a simulator: https://wokwi.com/projects/335025004843369044 | |
*/ | |
#define SEG_PINA 8 | |
#define SEG_PINB 9 | |
#define SEG_PINC 4 | |
#define SEG_PIND 3 | |
#define SEG_PINE 2 | |
#define SEG_PINF 7 | |
#define SEG_PING 6 | |
#define SEG_PINP 5 | |
#define SEG_A 1 /* 0b00000001 */ | |
#define SEG_B 2 /* 0b00000010 */ | |
#define SEG_C 4 /* 0b00000100 */ | |
#define SEG_D 8 /* 0b00001000 */ | |
#define SEG_E 16 /* 0b00010000 */ | |
#define SEG_F 32 /* 0b00100000 */ | |
#define SEG_G 64 /* 0b01000000 */ | |
#define SEG_P 128 /* 0b10000000 */ | |
/* clang-format off */ | |
static const uint8_t digits[] = { | |
/* A | B | C | D | E | F | G | P */ | |
(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | 0 | 0), /* 0 */ | |
(0 | SEG_B | SEG_C | 0 | 0 | 0 | 0 | 0), /* 1 */ | |
(SEG_A | SEG_B | 0 | SEG_D | SEG_E | 0 | SEG_G | 0), /* 2 */ | |
(SEG_A | SEG_B | SEG_C | SEG_D | 0 | 0 | SEG_G | 0), /* 3 */ | |
(0 | SEG_B | SEG_C | 0 | 0 | SEG_F | SEG_G | 0), /* 4 */ | |
(SEG_A | 0 | SEG_C | SEG_D | 0 | SEG_F | SEG_G | 0), /* 5 */ | |
(SEG_A | 0 | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G | 0), /* 6 */ | |
(SEG_A | SEG_B | SEG_C | 0 | 0 | 0 | 0 | 0), /* 7 */ | |
(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G | 0), /* 8 */ | |
(SEG_A | SEG_B | SEG_C | SEG_D | 0 | SEG_F | SEG_G | 0), /* 9 */ | |
}; | |
/* clang-format on */ | |
void setup() { | |
pinMode(SEG_PINA, OUTPUT); | |
pinMode(SEG_PINB, OUTPUT); | |
pinMode(SEG_PINC, OUTPUT); | |
pinMode(SEG_PIND, OUTPUT); | |
pinMode(SEG_PINE, OUTPUT); | |
pinMode(SEG_PINF, OUTPUT); | |
pinMode(SEG_PING, OUTPUT); | |
pinMode(SEG_PINP, OUTPUT); | |
} | |
void loop() { | |
uint8_t d; | |
for (int i = 0; i < sizeof(digits); ++i) { | |
d = digits[i]; | |
digitalWrite(SEG_PINA, (d & SEG_A)); | |
digitalWrite(SEG_PINB, (d & SEG_B)); | |
digitalWrite(SEG_PINC, (d & SEG_C)); | |
digitalWrite(SEG_PIND, (d & SEG_D)); | |
digitalWrite(SEG_PINE, (d & SEG_E)); | |
digitalWrite(SEG_PINF, (d & SEG_F)); | |
digitalWrite(SEG_PING, (d & SEG_G)); | |
digitalWrite(SEG_PINP, (d & SEG_P)); | |
delay(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment