i think what i have here is a hardware anomaly ("i'll take 'things programmers say' for $1,000").
about 2+ years ago i started a project that was supposed to be a simple slot car lap counter. as you can see from the attachment, it's grown substantially.
(btw, i included the picture of the test track just to give you a visual of the beast. i realize that you won't be able to glean any details concerning actual wiring and such).
here's the components that make up the counter architecture.
1. 4 lane track. each track has a ldr/resister circuit installed under it. 4 holes in the track allow the ldrs to be visible from above the track. these circuits are connected to IH pins on a mega.
2. 4 lasers mounted in a housing that point at the ldr's from above the track. these are connected to nothing. they simply supply the light source.
3. 4 interrupt handlers that detect the absence of laser on the ldr when the cars pass under the laser.
4. 4 "idiot" lights in the form of leds that tell me when an IH is dispatched. connected to pins on mega.
5. 4 more idiot lights that tell me when i'm processing the interrupt. connected to pins on mega.
here's what happens during test.
1. i boot this whole mess.
2. i pass a pencil through the laser, blocking the ldr, to simulate a lap. everything's cool - no problem what-so-ever. lap's are counted, speed is displayed, etc, etc, etc.
(btw, the pencil test is how i developed and tested 75% of the code. that is, i didn't connect the components to an actual track until about a year ago. that's when i ran into this anomaly. i then downsized the track length to make it the size of the attachment pic. easier to handle/test, and wife wanted the 4x8 sheet of plywood off her dining room table.)
3. then, with no cars on the track, i squeeze the trigger on the remote control applying voltage to the track(s). nothing happens - this is good. nothing should happen.
(btw, track runs off an adapter rated at 17v dc 6va.)
4. then, with car(s) on the track, i squeeze the trigger. this is when all hell breaks loose. idiot lights flash constantly and with no predictability. has nothing to do with whether the car passes through the laser. happens all the way around the track. and, has nothing to do with how many cars are on the track. even 1 car will blink all lights. this is most curious. how can 1 car on 1 track affect all tracks?
bottom line:
something is causing the IH pins on the mega to change state regardless of the ldr/laser interaction and thus dispatching the IH's randomly.
here's what i've tried.
1. changed the value of the resistor on the ldr/resistor circuit. don't remember exactly what values i used, but if i know me, i'm sure i went very high and then very low in an effort to divide and conquer. neither high or low made a difference, so i abandoned that approach.
(fyi - during the writing of this post i tried one more time first installing a 1M resistor. then adding 1 more at a time for a total of 3M's in series. here's the results:
- a) with the ldr's close to the track - no difference.*
- b) with the ldr's raised above the track, about 6" - same as "a)" except led indicators don't blink as bright.*
- c) pencil test fails - seems that with this amount of resistance i can't even "force" a lap)*
2. isolation. tried wrapping the ldr/resistor circuit in a variety of insulators. aluminum foil, copper, cardboard, a combination of each.
3. ferrite cores. added them to every inch of wire from the ldr/resistor circuit to the pins on the mega, including 5v and gnd wires.
4. reversed the location of the ldr/resistor circuit and the laser in an effort to move the ldr/resistor as far away from the track as possible. that is, shot the laser beam through the track up to the ldr. the ldr was about 6" above the track.
i hesitate to include the next 2 points because i don't want to steer the conversation in a particular direction. but ...
1. i've, of course, googled extensively. the most recent hit i ran across are these things called 'noise filtering circuits'. have no idea if it's what i need - the words just seem to fit. my problem, as an amateur in electronics, is that i've only ran across schematics which i can't fully read. for me to implement i'd need a wiring diagram.
2. is it possible that the cars themselves are causing the problem? that is, are they really capable of generating an electromagnetic field that would have this effect on the ldr's/IH pins?
willing to try whatever. been working on this for a long time. would really like to see it happen. <<<
thanks <<<
again, i don't think this is a software problem since it works flawlessly with the pencil test. i included the following snippets for the sake of completeness.
// ldr sensor pins.
// the interrupt handlers are attached to these pins
//
const int car_1_pin = 2; // pins per mega
const int car_2_pin = 3;
const int car_3_pin = 18;
const int car_4_pin = 19;
here's how the ldr's are wired. duplicated for each of 4 tracks. 2, 3, 18 and 19.
ldr is either GL5516 or KE-10720. sorry, i can't remember which. it's a basic ldr. nothing special.
+5v (mega) .--- ldr ---.--- 15k ---. GND
|
|
IH pin
this is how i attach the interrupts. notice 2 versions. i use them interchangeably when testing and i think
one method might work better than the other. both versions seem to act exactly the same.
/*
pinMode( car_1_pin, INPUT_PULLUP );
pinMode( car_2_pin, INPUT_PULLUP );
pinMode( car_3_pin, INPUT_PULLUP );
pinMode( car_4_pin, INPUT_PULLUP );
//
attachInterrupt( digitalPinToInterrupt(car_1_pin), car_1_ih, FALLING );
attachInterrupt( digitalPinToInterrupt(car_2_pin), car_2_ih, FALLING );
attachInterrupt( digitalPinToInterrupt(car_3_pin), car_3_ih, FALLING );
attachInterrupt( digitalPinToInterrupt(car_4_pin), car_4_ih, FALLING );
*/
digitalWrite( car_1_pin, HIGH );
digitalWrite( car_2_pin, HIGH );
digitalWrite( car_3_pin, HIGH );
digitalWrite( car_4_pin, HIGH );
//
attachInterrupt( digitalPinToInterrupt(car_1_pin), car_1_ih, RISING );
attachInterrupt( digitalPinToInterrupt(car_2_pin), car_2_ih, RISING );
attachInterrupt( digitalPinToInterrupt(car_3_pin), car_3_ih, RISING );
attachInterrupt( digitalPinToInterrupt(car_4_pin), car_4_ih, RISING );
here's the IH. duplicated for each of 4 tracks using appropriate array index for each.
basically flashes the "got the interrupt" led, and sets a trigger which is examined in loop()
// car 1 interrupt handler
//
void car_1_ih( void ) {
digitalWrite( ih_leds[0], HIGH );
if( flag_e == e_green ) {
for( int i=0; i<num_cars; i++ ) { // find first "empty" spot in array
if( ih_trigger[i] == 0 ) {
ih_trigger[i] = 1; // track 1
car_rf_time[0] = millis();
break;
}
}
}
digitalWrite( ih_leds[0], LOW );
}