I have successfully completed my main goal, which was to build a 2 lane Slotcar Timer. I tried a few and learned quite a bit, but eventually realized the need for ISR and INPUT_PULLUP. I found one in German which was good because I translated it line for line and really got an idea of how things worked. I had to swap items around to get the declarations in right places for 2020. I was able to get a unique LED to flash for each lane when the car crossed the line, but I cheated, and left the variable world the OP had created to do this in newb fashion.
Where i am at is this. Each lane has 3 LEDs. I have initialized them in two arrays. I need to assign each array to a specific lane, so when lap calculations are being compiled it happens automatically in the variables. (I think). I made many efforts to flash an LED whenever a new fast lap was acheived, but any sort of digitalWrite in the middle of the calculation made results go whacky. I could get the light to come on, but the fast lap data would not process correctly. (I got a hint when the 1st car would pass, it would flash the fastlap light for both lanes the 1st time!)
if (lapTime[i] < lapRecord[i])
// led code here
lapRecord[i] = lapTime[i];
seems easy enough but I have tried many things, and it just doesn't want me to put a function in there. I even tried splitting the code and removing * and replacing with the value, but that gets wonky if both lanes trigger. Can I make the LED array assignable? and can I do anything with that lap calculation?*
Thank you for any ideas in advance.
*Here is the entire sketch. *
```
*#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
int LEDPIN0[] = {9, 7, 8};
int LEDPIN1[] = {12, 10, 11};
//int ledGroup[] = {0,1} == {LEDPIN0, LEDPIN1} ?? how would I do this? THIS!!
int pinCount = 3;
#define SLOTS 2
// Minimum lap time in milliseconds, interpreting everything below as contact bouncing
#define MINROUNDTIME 3000
// Pins for connecting the laser sensors (D2, D3)
byte laserPins[] = {2, 3};
// Interrupt-Numbers for these pins
byte pinInterrupts[] = {0, 1};
// Start of the last round in milliseconds, volatile because access is also from ISR!
volatile long roundStart[] = {0, 0};
//Lap time of the last lap in milliseconds
long lapTime[] = {0, 0}; //lap time
// Lap time of the fastest lap in milliseconds
long lapRecord[] = {15000, 15000}; //lap time record
// Lap Counter
int lapNumber[] = {0, 0};
// Interrupt (treatment routines)
void timing1()
{
if (millis() - roundStart[0] > MINROUNDTIME)
roundStart[0] = millis();
}
void timing2()
{
if (millis() - roundStart[1] > MINROUNDTIME)
roundStart[1] = millis();
}
void (*isrFunctions[])() = { timing1, timing2 };
void setup() {
lcd.init();
lcd.backlight();
// Initialize laser sensor and ISR routines
for (int i = 0; i < SLOTS; i++)
{
pinMode(laserPins[i], INPUT_PULLUP);
attachInterrupt(pinInterrupts[i], isrFunctions[i], HIGH);
}
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
pinMode(LEDPIN0[thisPin], OUTPUT);
pinMode(LEDPIN1[thisPin], OUTPUT);
}
lcd.setCursor(4, 0);
lcd.print("Suitcase");
lcd.setCursor(4, 1);
lcd.print("Speedway");
delay(3000);
lcd.clear();
}
//void ProcessLapTimes() //Process lap times
void NumberofLaps() //Show number of laps
{ static long lastUpdate;
char lcdline[17];
// Update display only every 500 ms
if (millis() / 500 == lastUpdate) return;
lastUpdate = millis() / 500;
for (int i = 0; i < SLOTS; i++)
{
snprintf(lcdline, sizeof(lcdline), "Lane%d Lap %5d ", i + 1, lapNumber[i]);
lcd.setCursor(0, i);
lcd.print(lcdline);
}
}
void ShowLapTimes() //Show lap times
{ static long lastUpdate;
long thisLap, thisRecord;
char lcdline[17];
int ltime[4];
// Update display only every 500 ms
if (millis() / 500 == lastUpdate) return;
lastUpdate = millis() / 500;
for (int i = 0; i < SLOTS; i++)
{
// Lap time and fastest lap time in hundredths of a lap
thisLap = (lapTime[i] + 5) / 10; // lap time
thisRecord = (lapRecord[i] + 5) / 10; // lap time record
ltime[0] = thisLap / 100; // whole seconds
ltime[1] = thisLap % 100; // hundredths of second
ltime[2] = thisRecord / 100; // whole seconds
ltime[3] = thisRecord % 100; // hundredths of second
snprintf(lcdline, sizeof(lcdline), "%d%3d.%02d %3d.%02d", i + 1, ltime[0], ltime[1], ltime[2], ltime[3]);
lcd.setCursor(0, i);
lcd.print(lcdline);
}
}
void loop()
{
//ProcessLapTimes(); // process lap times
// Start of the penultimate round in milliseconds
static long RoundProcessed[] = {0, 0};
long curMillis;
// Note the current status of the millis () function
curMillis = millis();
// Check all slots for changes in the round start time
for (int i = 0; i < SLOTS; i++)
{
if (RoundProcessed[i] != roundStart[i])
{ // New lap since the last loop run
lapNumber[i]++; // Round counters high counts
// Determine lap time
lapTime[i] = roundStart[i] - RoundProcessed[i];
// Determine if it was the fastest lap
if (lapTime[i] < lapRecord[i])
// can I do anything here? i need this calculation before next line! THIS!
lapRecord[i] = lapTime[i];
// In the end, mark this lap time as processed
RoundProcessed[i] = roundStart[i];
}
}
if ((millis() / 2000) % 2 == 1)
ShowLapTimes(); // show lap times
else
NumberofLaps(); // show number of laps
// flash the green led when you cross the line (see how I cheated?)
if (millis() - roundStart[0] < 200)
digitalWrite(LEDPIN0[0], HIGH);
else
digitalWrite(LEDPIN0[0], LOW);
if (millis() - roundStart[1] < 200)
digitalWrite(LEDPIN1[0], HIGH);
else
digitalWrite(LEDPIN1[0], LOW);
//delay(50);
// digitalWrite(LEDPIN0[1], LOW);
// digitalWrite(LEDPIN1[1], LOW);
}*
```