/*
Project: No library 7 segment counter
Description: Counts up on a 4 digit seven segment display
In a real circuit add 330 ohm resistors on each segment
and transistors on each digit! (Wokwi doesn't care...)
The gray wires are digits, colored are the segments.
Creation date: 4/10/23
Author: AnonEngineering
License: Beerware
*/
const int MAX_DIGITS = 4;
const int DP_POS = 2; // decimal point position 0-3
const unsigned long CNT_INTERVAL = 100; // 0.1 second count
// pin constants
const int DP_PIN = 5;
const int SEG_PINS[7] = {9, 13, 4, 6, 7, 10, 3}; // A-G
const int DIGIT_PINS[MAX_DIGITS] = {8, 11, 12, 2}; // big Endian
// segment lookup table
const int NUM_SEGS[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, /* 0 */
{0, 1, 1, 0, 0, 0, 0}, /* 1 */
{1, 1, 0, 1, 1, 0, 1}, /* 2 */
{1, 1, 1, 1, 0, 0, 1}, /* 3 */
{0, 1, 1, 0, 0, 1, 1}, /* 4 */
{1, 0, 1, 1, 0, 1, 1}, /* 5 */
{1, 0, 1, 1, 1, 1, 1}, /* 6 */
{1, 1, 1, 0, 0, 0, 0}, /* 7 */
{1, 1, 1, 1, 1, 1, 1}, /* 8 */
{1, 1, 1, 1, 0, 1, 1} /* 9 */
};
unsigned long prev_cnt_time = 0;
int countVal = 0;
void writeDigit(int digit, int number, bool dp) {
// set up the segments & dp
for (int segment = 0; segment < 7; segment++) {
digitalWrite(SEG_PINS[segment], NUM_SEGS[number][segment]);
}
digitalWrite(DP_PIN, dp);
// turn digit on
digitalWrite(DIGIT_PINS[digit], LOW);
// turn digit off
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
Serial.begin(9600);
// set pins to output
for (int digit = 0; digit < MAX_DIGITS; digit++) {
pinMode(DIGIT_PINS[digit], OUTPUT);
}
for (int seg = 0; seg < 7; seg++) {
pinMode(SEG_PINS[seg], OUTPUT);
}
}
void loop() {
bool showDP = false;
int value[MAX_DIGITS];
if (millis() - prev_cnt_time >= CNT_INTERVAL) {
prev_cnt_time = millis();
countVal++;
if (countVal > 9999) countVal = 0;
// separate the digits
value[0] = countVal / 1000; // thousands
value[1] = (countVal % 1000) / 100; // hundreds
value[2] = (countVal % 100) / 10; // tens
value[3] = countVal % 10; // ones
//}
// loop thru the digits
for (int digit = 0; digit < MAX_DIGITS; digit++) {
// place decimal point
digit == DP_POS ? showDP = true : showDP = false;
writeDigit(digit, value[digit], showDP);
}
}
}