something funny going on with my limit switch...

Hi gang!
here is the schematic of my limit curcuit:

or the link
Imgur

The switch is a Cherry magnetic reed switch with contact ratings of 0.5 amps. I am working with one meter sheilded leads to the switch and am in a noisy environment hence the strong pull up resistor R12 of 330 ohms and the RC low pass filter, R11 and C9.

Here is the code supporting it:

#define Lim_pin  9            // for the limit switch input
#define PWM_pin 10            // for the speed control


void Limit_check()
{
      if(digitalRead(Lim_pin) == LOW)                        // found a limit switch
      {                                          // debounce
            delay(100);                              // pause 100ms (1/10) second
            if(digitalRead(Lim_pin) == HIGH) {return;}      // false alarm, get out
      }

      .
      .
      .
      handle my limit encounter....
}


void setup() 
{
      pinMode (Lim_pin, INPUT);                  // limit switch
      pinMode (PWM_pin, OUTPUT);                  // output for motor PWM

      digitalWrite (Lim_pin, HIGH);                       // turn on pullup resistors
      
      TCCR2A = B10000001;                        // set Atmega PWM params
      TCCR2B = B00000001;                        // set Atmega PWM params - bout 31250 hz
      OCR2A = 0;                              // init pulse width to min (max = 255) 0 = off
}

void loop() 
{
      delay(10);            // without it, loop is less that one ms
      
      Limit_check();

      .
      .
      .
      .
      other stuff
}

THE PROBLEM!
Sometimes! <- (emphasis!) The Arduino will think the limit switch is close when it is not. It may go through several hundred limit cycles or happen on the first one. I have place my multi meter across the limit contacts and normal readings are 5v or zero volts depending on if the magnet is in range or not. When I am getting a false reading, the meter says 0.5 volts or zero volts. SOMETHING is clamping it down! Since the only non-discrete component in the system is the Arduino, I am wondering what the hey is going on. I am wondering if the PWM signal I am manipulating on pin ten is affecting pin nine ( they both share the same oscillator, Timer 2). I manipulate it with the code TCCR2A = B10000001 and TCCR2B = B00000001 in setup then inc or dec OCR2A in a subroutine from 0 to 255. I use this output to control my motor speed.

Hmm....very suspicious. Are you using the tone() function anywhere? That makes use of Timer 2.

Can you stop using pin 10 temporarily to test out your crosstalk theory?

--
The Quick Shield: breakout all 28 pins to quick-connect terminals

The code you show doesn't seem to be an issue. Can you post the entire code to see if there is another resource conflict?

CMiYC: could but it's complicated! ...over 2500 lines

RC: no tone function anywere. can't move pin ten but moved limit to pin 28 and testing.

I am getting the good out of the Mega, I use the following:
I2C for RTC
timer two for motor speed control
timer three interrupt at 1/2 second period for movement/stall check
state change interrupt on pin 2 for an optical encoder
serial for diagnostic code to Arduino environment software
serial1 to communicate with touch screen
EEPROM to store and retrieve user instructions.

have tried to be very carefull with my code to make sure there are no recursive loops, bad variable writes and such that may corrupt memory or cause a stack overflow. These normally take a bit of time to rear their ugly head and SOMETIMES this limit problem happens right after boot.

CMiYC: could but it's complicated! ...over 2500 lines

that might be your problem, its to complex for you to debug, try sectioning the malfunctioning function to stand alone and come back with that information

till then its nothing but "something" in 25k lines of "whatever"

I tend to nit pick and perfect every function before moving on, then when something has an effect I know its not the old code, but what I am doing to it in the current manipulation

Have you tried some decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html
The other thing I would try is a 0.1uF capacitor across the arduino input to try and swallow any interference.