Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "TimerOne.h"
const int PIN_1_K = 10;
const int PIN_2_K = 12;
const int PIN_3_K = 11;
int digits[10][8] = {
// 0 1 2 3 4 5 6 7
{ 1, 1, 0, 1, 0, 1, 1, 1 }, // 0
{ 0, 0, 0, 1, 0, 1, 0, 0 }, // 1
{ 1, 1, 0, 0, 1, 1, 1, 0 }, // 2
{ 0, 1, 0, 1, 1, 1, 1, 0 }, // 3
{ 0, 0, 0, 1, 1, 1, 0, 1 }, // 4
{ 0, 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 1, 1, 0, 1, 1, 0, 1, 1 }, // 6
{ 0, 0, 0, 1, 0, 1, 1, 0 }, // 7
{ 1, 1, 0, 1, 1, 1, 1, 1 }, // 8
{ 0, 0, 0, 1, 1, 1, 1, 1 } // 9
};
volatile bool disableDigits = false;
volatile int digit1 = 1;
volatile int digit2 = 2;
volatile int digit3 = 3;
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
Timer1.initialize(100);
Timer1.attachInterrupt(refresh);
}
void displayDigit(uint8_t n) {
int *segments = digits[n];
for (int i = 0; i < 8; i++) {
digitalWrite(2 + i, segments[i]);
}
}
uint8_t count = 0;
void refresh() {
if (disableDigits) {
pinMode(PIN_1_K, INPUT);
pinMode(PIN_2_K, INPUT);
pinMode(PIN_3_K, INPUT);
return;
}
uint8_t v = (count >> 4) % 3;
if (v == 0) {
pinMode(PIN_3_K, INPUT);
pinMode(PIN_1_K, OUTPUT);
displayDigit(digit1);
} else if (v == 1) {
pinMode(PIN_1_K, INPUT);
pinMode(PIN_2_K, OUTPUT);
displayDigit(digit2);
} else if (v == 2) {
pinMode(PIN_2_K, INPUT);
pinMode(PIN_3_K, OUTPUT);
displayDigit(digit3);
}
count++;
}
void setDigits(int v) {
digit1 = v / 100 % 10;
digit2 = v / 10 % 10;
digit3 = v % 10;
}
void loop() {
static int multiplier = 1;
static int multiplierShowCounter = 0;
static int count = 0;
static bool wasHigh = false;
if (!digitalRead(A0)) {
count = 0;
}
if (!digitalRead(A1)) {
multiplier++;
if (multiplier > 3) {
multiplier = 1;
}
multiplierShowCounter = 100;
disableDigits = true;
delay(100);
disableDigits = false;
setDigits(multiplier);
delay(100);
}
if (digitalRead(A2)) {
if (!wasHigh) {
count++;
wasHigh = true;
}
} else {
if (wasHigh) {
wasHigh = false;
}
}
if (multiplierShowCounter == 0) {
setDigits(count * multiplier);
} else {
multiplierShowCounter--;
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment