Hi, there people,
This is my first project and also my first time coding. So I'm trying to create a slotcar timer for my dad (I did not know this was a fairly prevalent project), since his timer died. Now I would like to detect if a car makes a jumpstart. My problem is that by using a short minimumtime I will get false positives for new laps. But by creating a long minimum time the car could already cross the line and I would not get the starting time of the car. So my idea was to switch the hardware interrupt funtions with a longer minimum elapsed time once the first crossing was done. But right now I'm a little stuck in the code I created, I simplified it a little and left irrelevant function calls out of it:
// Setup sensors
byte sensorPin1 = 2;
byte sensorPin2 = 3;
// flag for function pointer array
int setFunction1 = 0;
int setFunction2 = 0;
// for handeling the when to increase the function pointer or end script
int raceState1 = 0;
int raceState2 = 0;
// variables needed related to timing
unsigned long contactTime1;
unsigned long contactTime2;
unsigned long contactTime2previous;
unsigned long contactTime1previous;
unsigned long raceStart1;
}
// function pointer with array of isr functions
void (*functionISR1[])() = {newContactStart1, newContactLap1};
void (*functionISR2[])() = {newContactStart2, newContactLap2};
// first lap contact timer to look for a jumpstart by car 1 and/or 2
void newContactStart1(){
if (millis() - contactTime1 > jumpstart) {
contactTime1 = millis() ;
*****
}
else {
raceState = 5;
*****
}
}
void newContactStart2(){
if (millis() - contactTime2 > jumpstart) {
contactTime2 = millis() ;
*****
}
else {
raceState = 5;
*****
}
}
// longer mintime to allow for sensor reset since laps shouldnt be that short.
// ISR funtions 1 and 2 respectively
void newContactLap1() {
if (millis() - contactTime1 > minTime) {
contactTime1 = millis();
}
}
void newContactLap2() {
if (millis() - contactTime2 > minTime) {
contactTime2 = millis();
}
}
void setup() {
// interrupts attached.
attachInterrupt(digitalPinToInterrupt(sensorPin1), functionISR1[setFunction1], FALLING);
attachInterrupt(digitalPinToInterrupt(sensorPin2), functionISR2[setFunction2], FALLING);
}
void loop() {
if (contactTime1 != contactTime1previous) //which is based on jumpstart time and if before jumpstart time state =5;
if (raceState1 == 0){
raceState ++ ;
#####
// so i know the total time of the race for the winner and reset the timer for laps.
contactTime1previous = raceStart1 = contactTime1 ;
// Display laps total race etc.
}
if (raceState1 == 1){
// calcultate and display laptime base on previous contactime. take of a lap --
contactTime1previous = contactTime1 ;
}
if (raceState == 5) {
// end script because of jumpstart, or something else like add a panalty lap or panalty time to total time.
}
}
// the same for player 2 with all the values 2
Now I think my problem is that updating the function in the functionpointer doesn't update the ISR function am I right? To solve this can I just call a new attachinterrupt on the places specified with "*****"? Am I allowed to do this inside the ISR function to make sure it doesn't keep interrupting before setting the timer to the other interrupt function in the main loop? Or should i do it at the place i specified with "#####" outside of the ISR function?
I would like to use IR sensors in the final setup but I am using buttons right now to test since I wasn't sure i was going to get this to work (like I said first project). I have since ordered the sensors but they haven't arrived yet. is there any other problem I should be wary of when using IR sensors instead of the buttons I now use besides dialing in the minimumtime and jumpstart variable?
Lastly, I don't know if this should be in a different post but would switching to an Arduino nano, because it has 2 cores (if i'm not mistaken), allow me to play music when the race starts and ends?
I hope I stated my problem clearly and anyone can help me out. Thank you in advance for reading.