Skip to content

Instantly share code, notes, and snippets.

@zwazel
Last active November 5, 2022 19:19
Show Gist options
  • Save zwazel/8a2672be66ad6de99274601e1075cff0 to your computer and use it in GitHub Desktop.
Save zwazel/8a2672be66ad6de99274601e1075cff0 to your computer and use it in GitHub Desktop.
A stopwatch on a M5Atom stack, displaying the time in a binary format on the 5x5 led display. Worked together with @Ash-broccoli
#include "M5Atom.h"
int delayAmt = 1000;
int second_10th = 0;
int second_1st = 0;
int minute_10th = 0;
int minute_1st = 0;
unsigned long millis_start = millis();
uint32_t lastButtonPress = millis();
bool switchRunning = false;
bool isRunning = true;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
M5.begin(true, false, true);
millis_start = millis();
// int min1 = 9;
//int binaryMin1[5];
//convert_to_binary(binaryMin1, min1);
//display_binary(9, binaryMin1);
}
void loop() {
M5.update();
uint32_t lastChange = M5.Btn.lastChange();
// Serial.print(String(lastChange)+"\n");
if (lastChange > lastButtonPress) {
if (switchRunning) {
Serial.print("pressed\n");
isRunning = !isRunning;
if (isRunning) {
millis_start = millis();
}
}
switchRunning = !switchRunning;
lastButtonPress = millis();
}
if (isRunning) {
if ((millis() - millis_start) >= 1000) {
int binary_second_10th[5];
int binary_second_1st[5];
convert_to_binary(second_10th, binary_second_10th);
convert_to_binary(second_1st, binary_second_1st);
int binary_minute_10th[5];
int binary_minute_1st[5];
convert_to_binary(minute_10th, binary_minute_10th);
convert_to_binary(minute_1st, binary_minute_1st);
// print the binary array
display_binary(1, binary_minute_10th);
display_binary(2, binary_minute_1st);
display_binary(4, binary_second_10th);
display_binary(5, binary_second_1st);
// increment the seconds
second_1st++;
if (second_1st == 10) {
second_1st = 0;
second_10th++;
}
if (second_10th == 6) {
second_10th = 0;
minute_1st++;
}
if (minute_1st == 10) {
minute_1st = 0;
minute_10th++;
}
millis_start = millis();
}
}
}
void convert_to_binary(int num, int array[5]) {
int i = 0;
while (num > 0) {
array[i] = num % 2;
num /= 2;
i++;
}
while (i < 5) {
array[i] = 0;
i++;
}
}
void display_binary(int line, int binArr[]) {
int count = 0;
if (line > 5) {
count = 20;
} else if (line > 1) {
count = (line - 1) * 5;
}
for (int j = 4; j >= 0; j--) {
if (binArr[j] == 1) {
M5.dis.drawpix(count, 0xffffff);
} else {
M5.dis.drawpix(count, 0x000000);
}
count++;
}
M5.dis.drawpix(11, 0x03fc28);
M5.dis.drawpix(13, 0x03fc28);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment