/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define WINDOW_SIZE 11
#define TFT_DC 9
#define TFT_CS 10
#define ROTATION 1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int INDEX = 1;
float SUM = 0;
float READINGS[WINDOW_SIZE];
float AVERAGED = 0.0;
float VALUE = 0.0;
float TOTALTIME = 0.00;
unsigned long startMicros = 0; //some global variables available anywhere in the program
unsigned long currentMicros = 0;
int triggerPin = 2;
int stopPin = 3;
int counterReset = 5;
int hitPin = 4;
int counter = 0;
//Counter location reset rectangle
int counterLocationX = 250;
int counterLocationY = 5;
int counterSizeX = 45;
int couterSizeY = 20;
//Shot time location reset rectangle
int timeLocationX = 5;
int timeLocationY = 65;
int timeSizeX = 280;
int timeSizeY = 250;
//Hit location reset rectangle
int hitLocationX = 60;
int hitLocationY = 80;
int hitSizeX = 250;
int hitSizeY = 150;
//Average location reset rectangle
int averageLocationX = 145;
int averageLocationY = 200;
int averageSizeX = 75;
int averageSizeY = 20;
int ST1X = 5, ST1Y = 65;
int ST2X = 75, ST2Y = 70;
void stopTimer();
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(ROTATION);
tft.fillScreen(ILI9341_BLACK);
//Hit Counter
tft.setCursor(5, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hit Count:");
tft.setCursor(250, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(counter);
//Waiting for Trigger
tft.setCursor(5, 25);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Waiting for trigger...");
pinMode(triggerPin, INPUT_PULLUP);
pinMode(counterReset, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
pinMode(hitPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(triggerPin) == LOW) {
startMicros = micros();
delay(250);
}
if (digitalRead(stopPin) == LOW) {
currentMicros = micros();
delay(250);
Serial.println("Timer Stop");
stopTimer();
}
if (digitalRead(hitPin) == LOW) {
currentMicros = micros();
delay(250);
Serial.println("HIT!");
targetHit();
}
if (digitalRead(counterReset) == LOW) {
startMicros = micros();
delay(250);
Serial.println("Reset");
resetCounter();
}
if (INDEX == 2) {
tft.setCursor(5, 65);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(30, 65);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[1], 4);
tft.setCursor(100, 65);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 3) {
tft.setCursor(5, 85);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(30, 85);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[2], 4);
tft.setCursor(100, 85);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 4) {
tft.setCursor(5, 105);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(30, 105);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[3], 4);
tft.setCursor(100, 105);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 5) {
tft.setCursor(5, 125);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(30, 125);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[4], 4);
tft.setCursor(100, 125);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 6) {
tft.setCursor(5, 145);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(30, 145);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[5], 4);
tft.setCursor(100, 145);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 7) {
tft.setCursor(160, 65);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(185, 65);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[6], 4);
tft.setCursor(255, 65);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 8) {
tft.setCursor(160, 85);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(185, 85);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[7], 4);
tft.setCursor(255, 85);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 9) {
tft.setCursor(160, 105);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(185, 105);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[8], 4);
tft.setCursor(255, 105);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 10) {
tft.setCursor(160, 125);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX - 1));
tft.setCursor(185, 125);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[9], 4);
tft.setCursor(255, 125);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
if (INDEX == 0) {
tft.setCursor(160, 145);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println((INDEX + 10));
tft.setCursor(185, 145);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(READINGS[10], 4);
tft.setCursor(255, 145);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
}
}
void stopTimer() {
counter++;
TOTALTIME = (float(currentMicros - startMicros) / 1000000);
//SUM = SUM - READINGS[INDEX]; // Remove the oldest entry from the sum *********************************************************************
VALUE = TOTALTIME;// Read the next sensor value
READINGS[INDEX] = VALUE;
SUM = SUM + VALUE;
// Increment the index, and wrap to 0 if it exceeds the window size
AVERAGED = SUM / INDEX; // Divide the sum of the window by the window size for the result
Serial.print (INDEX); Serial.print(". ");
Serial.print(VALUE, 4);
Serial.print(",");
Serial.println(AVERAGED, 4);
tft.fillRect(averageLocationX, averageLocationY, averageSizeX, averageSizeY, ILI9341_BLACK);
tft.setCursor(45, 200);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Average:");
tft.setCursor(145, 200);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(AVERAGED, 4);
tft.setCursor(215, 200);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(" s");
INDEX = (INDEX + 1) % WINDOW_SIZE;
Serial.print("New index #: ");
Serial.println(INDEX);
/* received a packet
Serial.println("Received packet '");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(ST1X, ST1Y);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Shot time:");
tft.setCursor(ST2X, ST2Y);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.println(TOTALTIME, 4);
tft.setCursor(175, 105);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.println(" s");
*/
delay(10);
Serial.println("Timer Reset.");
Serial.println("--------------------------------------------------------------------------------------------------------------------");
tft.fillRect(counterLocationX, counterLocationY, counterSizeX, couterSizeY, ILI9341_BLACK);
//Hit Counter
tft.setCursor(5, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hit Count:");
tft.setCursor(250, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(counter);
startMicros = 0;
}
void targetHit() {
counter++;
tft.setCursor(60, 80);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(9);
tft.println("HIT!");
delay(1500);
tft.fillRect(hitLocationX, hitLocationY, hitSizeX, hitSizeY, ILI9341_BLACK);
tft.fillRect(counterLocationX, counterLocationY, counterSizeX, couterSizeY, ILI9341_BLACK);
//Hit Counter
tft.setCursor(5, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hit Count:");
tft.setCursor(250, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(counter);
}
void resetCounter() {
counter = 0;
INDEX = 1;
AVERAGED = 0;
VALUE = 0;
startMicros = 0; //some global variables available anywhere in the program
currentMicros = 0;
TOTALTIME = 0.00;
SUM = 0;
float READINGS[INDEX];
tft.fillRect(counterLocationX, counterLocationY, counterSizeX, couterSizeY, ILI9341_BLACK);
tft.fillRect(timeLocationX, timeLocationY, timeSizeX, timeSizeY, ILI9341_BLACK);
//Hit Counter
tft.setCursor(5, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hit Count:");
tft.setCursor(250, 5);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(counter);
//counterLocationX = 20;
//counterLocationY = 20;
//counterSizeX = 20;
//couterSizeY = 20;
}