I have the following code that is supposed to track time, keep a record of the past 10 times, and have an average time. When displayed, the current time doesn't change (stays at the previous time recorded instead of having a time that updates by the second), occasionally the bottom value for the previous times will become a blob that is different values merged, and the average time only uses the past 10 values in its average (it should use all the values recorded if possible). I figure its a few minor tweaks that I need but I cannot figure out what those tweaks are.
#include <Arduino_GigaDisplay_GFX.h> // Include Arduino Giga Display GFX library
GigaDisplay_GFX tft; // Initialize the Giga Display object
#define BLACK 0x0000
#define WHITE 0xFFFF
#define MAX_RECORDS 10
#define THRESHOLD 500 // Set threshold to a desired value
unsigned long lastReadingTime = 0; // Last reading timestamp
unsigned long timerStart = 0; // Timer start timestamp
unsigned long recordedTimes[MAX_RECORDS] = {0}; // Array to store recorded times in seconds
int recordIndex = 0; // Current index for storing times
float averageTime = 0; // Average time calculation in seconds
int readingCount = 0; // Number of readings taken
void setup() {
Serial.begin(9600); // Start serial communication
tft.begin(); // Initialize the display
tft.setRotation(1); // Set the rotation if needed
tft.fillScreen(BLACK); // Clear the screen at the beginning
tft.setTextColor(WHITE);
tft.setTextSize(3); // Set the text size
// Draw section headers
tft.setCursor(10, 10);
tft.print("Current Time:");
tft.setCursor(450, 10);
tft.print("Previous Times:");
tft.setCursor(10, 240);
tft.print("Average Time:");
}
void displayTime(unsigned long duration) {
unsigned long minutes = duration / 60;
unsigned long seconds = duration % 60;
tft.print(minutes);
tft.print("m ");
tft.print(seconds);
tft.print("s");
}
void displayCurrentReading(unsigned long duration) {
tft.fillRect(10, 40, 300, 40, BLACK); // Clear larger display area for current time
tft.setCursor(10, 40);
tft.print("Duration: ");
displayTime(duration);
}
void displayPreviousTimes() {
tft.fillRect(450, 40, 300, 250, BLACK); // Clear larger display area for previous times
tft.setCursor(450, 40);
for (int i = 0; i < MAX_RECORDS; i++) {
int idx = (recordIndex - i - 1 + MAX_RECORDS) % MAX_RECORDS; // Retrieve in descending order
if (recordedTimes[idx] > 0) {
tft.setCursor(450, 60 + i * 25); // Adjust y position for each time
displayTime(recordedTimes[idx]);
}
}
}
void displayAverageTime() {
tft.fillRect(10, 270, 300, 40, BLACK); // Clear larger display area for average time
tft.setCursor(10, 270);
tft.print("Average: ");
unsigned long averageInSeconds = (unsigned long)averageTime;
displayTime(averageInSeconds);
}
void loop() {
int reading = analogRead(A5); // Read the value from pin A5
if (reading > THRESHOLD) { // Check if reading exceeds the threshold
unsigned long currentTime = millis(); // Get current time in milliseconds
// If this is the first reading or if a new reading is detected
if (timerStart == 0) {
timerStart = currentTime; // Start the timer
} else {
// Calculate the duration in seconds since the last reading
unsigned long duration = (currentTime - timerStart) / 1000;
// Store the recorded time in the array
recordedTimes[recordIndex] = duration;
recordIndex = (recordIndex + 1) % MAX_RECORDS; // Increment index cyclically
readingCount++;
// Calculate the average time in seconds
unsigned long totalTime = 0;
for (int i = 0; i < MAX_RECORDS; i++) {
totalTime += recordedTimes[i];
}
averageTime = (readingCount > 0) ? (float)totalTime / readingCount : 0;
// Display the new reading duration, previous times, and average time
displayCurrentReading(duration);
displayPreviousTimes();
displayAverageTime();
// Reset the timer for the next reading
timerStart = currentTime;
}
delay(1000); // Update every second
}
}