Hi Guys,
Wrote a little sketch to print a timer to the serial monitor and wondering if or how I could improve it?
I added a led flash every second however watching carefully, its slightly out of time. Wondering how I could make it exact?
Let me know what you think. Any help, always appreciated.
The sketch uses 36,384 bytes, wondering how i might be able to reduce / optimize this significantly?
Wrote a little sketch to print a timer to the serial monitor and wondering if or how I could improve it?
I added a led flash every second however watching carefully, its slightly out of time. Wondering how I could make it exact?
Let me know what you think. Any help, always appreciated.
Code:
const int ledPin = 13;
unsigned long currentMillis;
unsigned long previousMillis;
int microSeconds = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis > 10) {
previousMillis = currentMillis;
microSeconds++;
if (microSeconds == 100) {
digitalWrite(ledPin, HIGH);
microSeconds = 0;
seconds++;
} else {
digitalWrite(ledPin, LOW);
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
seconds = 0;
minutes = 0;
hours++;
}
}
}
if (seconds < 10) {
Serial.printf("0%d:0%d:0%d:%d", hours, minutes, seconds, microSeconds);
Serial.println();
} else if (minutes < 10) {
Serial.printf("0%d:0%d:%d:%d", hours, minutes, seconds, microSeconds);
Serial.println();
} else if (hours < 10) {
Serial.printf("0%d:%d:%d:%d", hours, minutes, seconds, microSeconds);
Serial.println();
}
}
}
The sketch uses 36,384 bytes, wondering how i might be able to reduce / optimize this significantly?
Last edited: