#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
unsigned long timer;
int deciSeconds;
void setup() {
pinMode(1, INPUT);
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop() {
if (digitalRead(1) == HIGH) { // when click, start the stopwatch.
delay(200); // add delay to ensure the programming goes to next line, bacause clicking the button takes much longer time than the programming.
stopwatch();
}
sevseg.setNumber(deciSeconds, 1); // this will save the time we stopped and display.
sevseg.refreshDisplay();
}
void stopwatch() {
timer = millis();
deciSeconds=0; // reset the stopwatch to 0.
while (1) {
if (digitalRead(1) == HIGH) { // when click, stop the stopwatch.
delay(200);
break;
}
if (millis() - timer >= 100) {
timer += 100;
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond
if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
deciSeconds=0;
}
sevseg.setNumber(deciSeconds, 1);
}
sevseg.refreshDisplay(); // Must run repeatedly
}
}
/// END ///