Skip to content

Instantly share code, notes, and snippets.

@cnrkuo
Last active June 12, 2026 03:32
Show Gist options
  • Select an option

  • Save cnrkuo/a60892bd9d96bbe577692520470c9ac9 to your computer and use it in GitHub Desktop.

Select an option

Save cnrkuo/a60892bd9d96bbe577692520470c9ac9 to your computer and use it in GitHub Desktop.
Arduino Binary Stopwatch
const int bluePins[4] = {3, 2, 1, 0};
const int greenPins[6] = {9, 8, 7, 6, 5, 4};
const int redPins[6] = {13, 12, 11, 10, A1, A2};
const int btnStartPause = A0;
const int btnReset = A5;
const int btnLapView = A3;
const int btnClear = A4;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool isRunning = false;
bool lastSP = HIGH;
bool lastRst = HIGH;
bool lastLapView = HIGH;
bool lastClear = HIGH;
unsigned long lapRecords[15];
int recordCount = 0;
int currentViewIndex = -1;
unsigned long viewTimer = 0;
bool isDisplayingLapNum = false;
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(bluePins[i], OUTPUT);
}
for (int i = 0; i < 6; i++) {
pinMode(greenPins[i], OUTPUT);
pinMode(redPins[i], OUTPUT);
}
pinMode(btnStartPause, INPUT_PULLUP);
pinMode(btnReset, INPUT_PULLUP);
pinMode(btnLapView, INPUT_PULLUP);
pinMode(btnClear, INPUT_PULLUP);
}
void displayTime(unsigned long timeMs) {
unsigned long totalTenths = timeMs / 100;
unsigned int tenths = totalTenths % 10;
unsigned int seconds = (totalTenths / 10) % 60;
unsigned int minutes = (totalTenths / 600) % 64;
for (int i = 0; i < 4; i++) {
digitalWrite(bluePins[i], (tenths >> (3 - i)) & 1);
}
for (int i = 0; i < 6; i++) {
digitalWrite(greenPins[i], (minutes >> (5 - i)) & 1);
digitalWrite(redPins[i], (seconds >> (5 - i)) & 1);
}
}
void displayLapNumber(int num) {
for (int i = 0; i < 4; i++) {
digitalWrite(bluePins[i], (num >> (3 - i)) & 1);
}
for (int i = 0; i < 6; i++) {
digitalWrite(greenPins[i], LOW);
digitalWrite(redPins[i], LOW);
}
}
void loop() {
bool currentSP = digitalRead(btnStartPause);
if (lastSP == HIGH && currentSP == LOW) {
delay(50);
if (digitalRead(btnStartPause) == LOW) {
if (isRunning) {
elapsedTime += millis() - startTime;
isRunning = false;
} else {
startTime = millis();
isRunning = true;
currentViewIndex = -1;
}
}
}
lastSP = currentSP;
bool currentRst = digitalRead(btnReset);
if (lastRst == HIGH && currentRst == LOW) {
delay(50);
if (digitalRead(btnReset) == LOW) {
isRunning = false;
elapsedTime = 0;
currentViewIndex = -1;
recordCount = 0;
}
}
lastRst = currentRst;
bool currentLapView = digitalRead(btnLapView);
if (lastLapView == HIGH && currentLapView == LOW) {
delay(50);
if (digitalRead(btnLapView) == LOW) {
if (isRunning) {
if (recordCount < 15) {
lapRecords[recordCount] = elapsedTime + (millis() - startTime);
recordCount++;
}
} else {
if (elapsedTime > 0 && recordCount > 0) {
currentViewIndex = (currentViewIndex + 1) % recordCount;
viewTimer = millis();
isDisplayingLapNum = true;
}
}
}
}
lastLapView = currentLapView;
bool currentClear = digitalRead(btnClear);
if (lastClear == HIGH && currentClear == LOW) {
delay(50);
if (digitalRead(btnClear) == LOW) {
recordCount = 0;
currentViewIndex = -1;
}
}
lastClear = currentClear;
if (isRunning) {
displayTime(elapsedTime + (millis() - startTime));
} else {
if (elapsedTime > 0 && currentViewIndex != -1) {
if (isDisplayingLapNum) {
displayLapNumber(currentViewIndex + 1);
if (millis() - viewTimer >= 1000) {
isDisplayingLapNum = false;
}
} else {
displayTime(lapRecords[currentViewIndex]);
}
} else {
displayTime(elapsedTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment