Hi, This is not MY sketch. My programming abilities are quite limited, typically I can cut/paste and write the small sections to achieve my goals and learn along the way. This one is really has me stumped. Currently when it powers up, it does a little line blinky thing and waits for D2 to be grounded. Once the button is activated the stopwatch runs, another press pauses, third press resets it to 0 and forth press starts running again.
I have tried lots of experimentation to add two more buttons and nothing is working. My goal is to have one button start the stopwatch. A second button will pause the stopwatch and a third button will reset to 0.
I have tried creating buttonPins in several configurations but nothing works. Can someone please give me a hint or two?!?!?
Thanks Bryan
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
/*--Declare Matrix--*/
Adafruit_7segment matrix = Adafruit_7segment();
#define INTERRUPT_PIN 2
#define DEBOUNCE_RATE 100
volatile int timerState = 0;
volatile unsigned long debounce = 0;
volatile unsigned long startTime;
void setup() {
Serial.begin(9600);
Serial.println("Starting timer sketch..");
/*--setup the interrupt--*/
pinMode(INTERRUPT_PIN, OUTPUT);
digitalWrite(INTERRUPT_PIN, HIGH);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), updateTimerState, LOW);
matrix.begin(0x70);
matrix.clear();
cycleZeros(250, 500);
writeDashesToDisplay(true);
}
void cycleZeros(int wait, int pause) {
for (int i = 4; i > -1; i--) {
if (i == 2) {
--i;
}
matrix.writeDigitNum(i, 0);
matrix.writeDisplay();
delay(wait);
}
matrix.clear();
matrix.writeDisplay();
delay(pause);
}
void writeDashesToDisplay(boolean isDrawingColon){
matrix.clear();
matrix.writeDigitRaw(0, B01000000);
matrix.writeDigitRaw(1, B01000000);
matrix.writeDigitRaw(3, B01000000);
matrix.writeDigitRaw(4, B01000000);
matrix.drawColon(isDrawingColon);
matrix.writeDisplay();
}
void updateTimerState() {
//debounce the interrupt button
if (millis() - debounce > DEBOUNCE_RATE) {
if (timerState == 0) {
timerState = 1;
/*--set the start time--*/
startTime = millis();
} else if (timerState == 1) {
/*--pause the time--*/
timerState = 2;
} else if (timerState == 2) {
/*--reset the time to zeros--*/
timerState = 3;
}else if (timerState == 3){
/*--start the timer again--*/
timerState = 1;
startTime = millis();
}
/*--update the debounce--*/
debounce = millis();
Serial.print("Timer state updated! --> ");
Serial.print(timerState);
Serial.println("");
}
}
void writeCurrentTime() {
unsigned long elapsedTime, seconds, minutes, msValue;
elapsedTime = millis() - startTime;
seconds = elapsedTime / 1000;
matrix.drawColon(true);
if (seconds < 10) {
/*--pad seconds with zero--*/
matrix.writeDigitNum(0, 0);
matrix.writeDigitNum(1, seconds);
} else if (seconds >= 10 && seconds < 60) {
/*--if under 1 min. show seconds & sub-seconds--*/
matrix.writeDigitNum(0, seconds / 10);
matrix.writeDigitNum(1, seconds % 10);
} else if (seconds >= 60) {
/*--if equal or over 1 min., switch to min. & seconds--*/
minutes = seconds / 60;
if (minutes < 10) {
/*--pad with zeros--*/
matrix.writeDigitNum(0, 0);
matrix.writeDigitNum(1, minutes);
} else if (minutes > 99) {
/*--if min. exceed 99, stop timer--*/
timerState = 2;
return;
} else {
matrix.writeDigitNum(0, minutes / 10);
matrix.writeDigitNum(1, minutes % 10);
}
matrix.writeDigitNum(3, (seconds % 60) / 10);
matrix.writeDigitNum(4, (seconds % 60) % 10);
}
//if less than 1 min, show milliseconds
if (seconds < 60) {
msValue = (elapsedTime % 1000) / 10;
matrix.writeDigitNum(3, msValue / 10);
matrix.writeDigitNum(4, msValue % 10);
}
matrix.writeDisplay();
}
void loop() {
if(timerState == 1){
writeCurrentTime();
}
if(timerState == 3){
writeDashesToDisplay(true);
}
}