// Stable 6/12 - untested on slot car
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define TFT_BLACK 0x0000
#define TFT_NAVY 0x000F
#define TFT_DARKGREEN 0x03E0
#define TFT_DARKCYAN 0x03EF
#define TFT_MAROON 0x7800
#define TFT_PURPLE 0x780F
#define TFT_OLIVE 0x7BE0
#define TFT_LIGHTGREY 0xC618
#define TFT_DARKGREY 0x7BEF
#define TFT_BLUE 0x001F
#define TFT_GREEN 0x07E0
#define TFT_CYAN 0x07FF
#define TFT_RED 0xF800
#define TFT_MAGENTA 0xF81F
#define TFT_YELLOW 0xFFE0
#define TFT_WHITE 0xFFFF
#define TFT_ORANGE 0xFD20
#define TFT_GREENYELLOW 0xAFE5
#define TFT_PINK 0xF81F
//#include <TFT_HX8357.h> // Hardware-specific library
//TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library
// laps info
unsigned long currentRunStartMillis;
unsigned long lastRunInMillis;
unsigned long bestRunInMillis;
int currentLap;
int lastLap;
unsigned long savedMillis;
// global for display
int min_val, sec_val, milli_val;
// laser gate
const int gateSensorPin = 2; // the number of the gate sensor pin
int gateSensorState; // the current reading from the sensor
int lastgateSensorState = LOW; // the previous reading from sensor
void setup(void) {
//tft.init();
tft.begin();
tft.setRotation(3);
// pin mode
pinMode(gateSensorPin, INPUT);
// reset params
currentRunStartMillis = 0;
lastRunInMillis = 0;
bestRunInMillis = 0;
currentLap = 0;
tft.fillScreen(TFT_BLACK);
// Print "Current Lap"
tft.setCursor(20, 0);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(2);
tft.println("CURRENT LAP");
// Print "Last Lap"
tft.setCursor(20, 80);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(2);
tft.println("LAST LAP");
// Print "Best Lap"
tft.setCursor(20, 160);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setTextSize(2);
tft.println("BEST LAP");
updateScreen();
}
void loop() {
// read the state of the laser sensor:
int reading = digitalRead(gateSensorPin);
// If the switch changed, due to noise or pressing:
if (reading != lastgateSensorState) {} //end if
if (reading != gateSensorState) {
gateSensorState = reading;
// If we went low, this mean the beam was broken
if (gateSensorState == LOW) {
// save the millis so all the math on it will be done with the same value.
savedMillis = millis();
// if its not the first lap
if (currentLap > 0) {
// save the last run
lastRunInMillis = savedMillis - currentRunStartMillis;
// if last run is faster then best run
if (lastRunInMillis < bestRunInMillis || bestRunInMillis == 0) {
//save as best
bestRunInMillis = lastRunInMillis;
} //end if
} //end if
//reset the current
currentRunStartMillis = savedMillis;
// move lap counter
currentLap++;
} //end if
} //end if
lastgateSensorState = reading;
// save current milis
savedMillis = millis();
// if we start the first lap
if (currentLap > 0) {
calcResultFromMillis(savedMillis - currentRunStartMillis, &min_val, &sec_val, &milli_val);
} else {
calcResultFromMillis(0, &min_val, &sec_val, &milli_val);
} //end if
// Print "Current Lap" details
tft.setCursor(20, 20);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (min_val < 10) {
tft.print('0');
}
tft.println(min_val); // Print Current Lap "MM"
tft.setCursor(150, 20);
if (sec_val < 10) {
tft.print('0');
}
tft.println(sec_val); // Print Current Lap "SS"
tft.setCursor(250, 20);
if (milli_val < 1) {
tft.print('0');
}
tft.println(milli_val); // Print Current Lap "MS"
if (currentLap != lastLap) {
updateScreen();
lastLap = currentLap;
}
}
void updateScreen() {
// print Lap #
tft.setCursor(290, 50);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.setTextSize(2);
if (currentLap < 10) {
tft.print('0');
}
tft.println(currentLap);
// Print "Current Lap" details
tft.setCursor(20, 20);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (min_val < 10) {
tft.print('0');
}
tft.println(min_val); // Print Current Lap "MM"
tft.setCursor(132, 20);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Current Lap ":"
tft.setCursor(150, 20);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (sec_val < 10) {
tft.print('0');
}
tft.println(sec_val); // Print Current Lap "SS"
tft.setCursor(232, 20);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Current Lap ":"
tft.setCursor(250, 20);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (milli_val < 1) {
tft.print('0');
}
tft.println(milli_val); // Print Current Lap "MS"
// calcResultFromMillis for Last Lap
calcResultFromMillis(lastRunInMillis, &min_val, &sec_val, &milli_val);
//Print "Last Lap" details
tft.setCursor(50, 100);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (min_val < 10) {
tft.print('0');
}
tft.println(min_val); // Print Last Lap "MM"
tft.setCursor(132, 100);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Last Lap ":"
tft.setCursor(150, 100);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (sec_val < 10) {
tft.print('0');
}
tft.println(sec_val); // Print Last Lap "ss"
tft.setCursor(232, 100);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Last Lap ":"
tft.setCursor(250, 100);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (milli_val < 1) {
tft.print('0');
}
tft.println(milli_val); // Print Last Lap "MS"
// calcResultFromMillis for Best Lap
calcResultFromMillis(bestRunInMillis, &min_val, &sec_val, &milli_val);
// Print "Best Lap" details
tft.setCursor(50, 180);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (min_val < 10) {
tft.print('0');
}
tft.println(min_val); // Print Best Lap "mm"
tft.setCursor(132, 180);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Best Lap ":"
tft.setCursor(150, 180);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (sec_val < 10) {
tft.print('0');
}
tft.println(sec_val); // Print Best Lap "ss"
tft.setCursor(232, 180);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextSize(3);
tft.println(':'); // Print Best Lap ":"
tft.setCursor(250, 180);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(3);
if (milli_val < 1) {
tft.print('0');
}
tft.println(milli_val); // Print Best Lap "MS"
}
// calculate millis into 3 values; minutes, seconeds and millis for display
void calcResultFromMillis(unsigned long value, int *min_val, int *sec_val, int *milli_val) {
*min_val = int(value / 60000);
*sec_val = int((value - (*min_val * 60000)) / 1000);
*milli_val = value - ((*sec_val * 1000) + (*min_val * 60000));
// ??? -q
//*sec_val = *sec_val % 60;
//*min_val = *min_val % 60;
}