Skip to content

Instantly share code, notes, and snippets.

@vhxs
Created February 19, 2024 00:08
Show Gist options
  • Save vhxs/5577c351a49c268b66e8794bc1feed86 to your computer and use it in GitHub Desktop.
Save vhxs/5577c351a49c268b66e8794bc1feed86 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
struct BCD {
int eights;
int fours;
int twos;
int ones;
};
struct BCD decimal_to_BCD(int value) {
switch (value)
{
case 0:
/* code */
return {0, 0, 0, 0};
case 1:
return {0, 0, 0, 1};
case 2:
return {0, 0, 1, 0};
case 3:
return {0, 0, 1, 1};
case 4:
return {0, 1, 0, 0};
case 5:
return {0, 1, 0, 1};
case 6:
return {0, 1, 1, 0};
case 7:
return {0, 1, 1, 1};
case 8:
return {1, 0, 0, 0};
case 9:
return {1, 0, 0, 1};
default:
break;
}
}
int seconds = 0;
void increment_seconds() {
seconds += 1;
if (seconds >= 60) {
seconds = 0;
}
}
void write_seconds_ones_digit() {
int ones_digit = seconds % 10;
struct BCD bcd = decimal_to_BCD(ones_digit);
digitalWrite(18, bcd.eights);
digitalWrite(19, bcd.fours);
digitalWrite(21, bcd.twos);
digitalWrite(22, bcd.ones);
}
void write_seconds_tens_digit() {
int tens_digit = seconds / 10;
struct BCD bcd = decimal_to_BCD(tens_digit);
digitalWrite(26, bcd.eights);
digitalWrite(27, bcd.fours);
digitalWrite(32, bcd.twos);
digitalWrite(33, bcd.ones);
}
void setup() {
Serial.begin(9600);
// ones place
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
// tens place
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
}
void loop() {
increment_seconds();
write_seconds_ones_digit();
write_seconds_tens_digit();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment