Okay so I acknowledge that this one will be a bit unconventional. I'm working on a game to record how long someone can hang on a bar. To record the length of the hang time, I just set up a counter that goes off every 100ms while the switch for the hang bar is pressed. So far this works great for reporting the current time.
I'm displaying the scores on two Adafruit 64x32 matrix displays so I'm using a Metro M0 Express to report the scores to each display.
They're sharing the switch that starts the timer and when both are set up to display the current score, they're totally synced up. However, when I try to record the high score (longest hanging time), I get a longer time than the current time. The code for both is identical, except that the the high score is reset to zero in the current time display. For the high score, I just comment it out. Code is below:
#include <RGBmatrixPanel.h>
#define CLK 8
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64);
float highScore;
float milTime = 0;
int hitPin = 12;
int hangBar = 11;
int mode = 2;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
matrix.begin();
pinMode(hangBar, INPUT_PULLUP);
pinMode(hitPin, OUTPUT);
digitalWrite(hitPin, HIGH);
matrix.setTextColor(matrix.Color333(0,0,7));
matrix.setTextSize(2);
matrix.setCursor(3, 9);
matrix.print(0);
matrix.print(0);
matrix.print(0);
matrix.print(".");
matrix.println(0);
}
void loop() {
switch (mode) {
case 1:
while (digitalRead(hangBar) == LOW) {
if (milTime < 99) {
milTime = (milTime + 1);
digitalWrite(hitPin, LOW);
delay(50);
digitalWrite(hitPin, HIGH);
Serial.println(milTime);
delay(50);
if (milTime > highScore) {
highScore = (highScore + 1);
matrix.setTextSize(2);
matrix.fillScreen(0);
matrix.setCursor(3, 9);
matrix.print(0);
matrix.print(0);
matrix.println(highScore / 10, 1);
}
} else if (milTime >= 99) {
milTime = (milTime + 1);
digitalWrite(hitPin, LOW);
delay(50);
digitalWrite(hitPin, HIGH);
Serial.println(milTime);
delay(50);
if (milTime > highScore) {
highScore = (highScore + 1);
matrix.setTextSize(2);
matrix.fillScreen(0);
matrix.setCursor(3, 9);
matrix.print(0);
matrix.println(highScore / 10, 1);
}
} else if (milTime >= 999) {
milTime = (milTime + 1);
digitalWrite(hitPin, LOW);
delay(50);
digitalWrite(hitPin, HIGH);
Serial.println(milTime);
delay(50);
if (milTime > highScore) {
highScore = (highScore + 1);
matrix.setTextSize(2);
matrix.fillScreen(0);
matrix.setCursor(3, 9);
matrix.println(highScore / 10, 1);
}
}
if (digitalRead(hangBar) == HIGH) {
mode = 2;
break;
}
}
case 2:
if (digitalRead(hangBar) == LOW) {
mode = 1;
milTime = 0;
highScore = 0;
break;
}
}
}