Hello everyone. A few weeks back I requested some help in Project guidance regarding my Arduino Uno project for counting a four lane slot car race. The advice was quite good. The best advice I have seen is trying to describe the problem fully, so here it goes:
PROJECT GOAL:
-Display and accurately count laps for 1/32 slot cars passing over the start finish line in a real time race.
-Check for race end and display final placings for each lane.
-Provide Visual or Audible feedback for race start and end.
PROTOTYPE COMPLETED AND EVOLUTION:
-I completed a working prototype with the Arduino UNO and utilized a traditional "light bar" for counting the passing cars. A light bar in slot car lingo is a series of overhead lights that the car passes under. The car's shadow is sensed underneath the track and is processed from there.
-The prototype worked fine, but at actual race speeds (very fast), the car would not be counted. Via my own research I realized that my photoresistors might be reacting too slow or my code was too un-optimized. Turning to these forums I was told about "interrupts", and it was pointed out that indeed my code was too busy doing other adminstrative duties rather than looking for cars passing. I have since educated myself on the use of interrupts and I am working on a newer second prototype. My research led me to an alternative Arduino, the MEGA 2650 due to the 6 interrupts available to me, which would give one for each of the 4 lanes. I have substituted photoresistors on a benchtop breadboard with momentary switches that trigger one of four ISRs thus simulating a car triggering a sensor. (I am waiting on a slot photointerruptors to arrive any day for sensing).
THE PROBLEM:
-One of the four - - IDENTICAL - - ISRs is working as intended and counting laps as one would expect for a momentary switch. That is, there is some bounce, but I will take care of that later. 3 of the 4 ISRs are causing immediate interrupts and counting down the laps for 3 of the 4 lanes almost immediately after starting the race to zero. I have isolated it to one working pin (D2). Now, I have noted that the LCD takes up 2 of the 6 pins for SDA and SCL (Pins20 and 21), but I don't know if that has anything to do with my problem.
I have attached the code in full, but below is a list of what i feel are germane parts of the code:
// Global Variable Declarations prior to setup
LiquidCrystal_I2C lcd(0x27, 20, 4); //Sets LCD address plus 20 Char long. 4 Char tall.
volatile int Lane1_Lap = 0; //Lap Counts Changed in Interrupt Routines
volatile int Lane2_Lap = 0;
volatile int Lane3_Lap = 0;
volatile int Lane4_Lap = 0;
const int Lane1_Sens = 2; //Interrupt Pins Declaration
const int Lane2_Sens = 3;
const int Lane3_Sens = 18;
const int Lane4_Sens = 19;
//These are the final lines of the void setup
attachInterrupt(digitalPinToInterrupt(Lane1_Sens), Lane1_Trigger, FALLING);
attachInterrupt(digitalPinToInterrupt(Lane2_Sens), Lane2_Trigger, FALLING);
attachInterrupt(digitalPinToInterrupt(Lane3_Sens), Lane3_Trigger, FALLING);
attachInterrupt(digitalPinToInterrupt(Lane4_Sens), Lane4_Trigger, FALLING);
}
// These are the four ISRs
void Lane1_Trigger(){
Lane1_Lap = Lane1_Lap -1;
if (Lane1_Lap <= 0){ //Argument prevents negative laps
Lane1_Lap = 0;
Finisher[0]=0; //4 unit Array for placings (bubble sorted in void loop)
}
}
void Lane2_Trigger(){
Lane2_Lap = Lane2_Lap - 1;
if (Lane2_Lap <= 0){
Lane2_Lap = 0;
Finisher[1]=0;
}
}
void Lane3_Trigger(){
Lane3_Lap = Lane3_Lap - 1;
if (Lane3_Lap <= 0){
Lane3_Lap = 0;
Finisher[2]=0;
}
}
void Lane4_Trigger(){
Lane4_Lap = Lane4_Lap - 1;
if (Lane4_Lap <= 0){
Lane4_Lap = 0;
Finisher[3]=0;
}
}
4LaneCounter_V006.ino (11.7 KB)