Door activated light fading and occurance counting question

New to programming and trying to learn, I have compiled a sketch that is functional, but has a strange delay.

So far, before I complicate the circuit any more, I have it configured so that when a door is opened, a counter will count up one occurance, and an LED will fade on. When the door is closed, the LED will fade off and will wait.

It’s working as intended in that the door is opened, and the LED fades on, but if the door is closed, there is a significant delay before it registers and begins to fade out the LED.

I have been playing with the delays within the sketch, but am not clear on what is really happening here (I put “Fading In” and “Fading Out” to know where the sketch currently is, but those lines will be removed once this issue is sorted.

/*
  
   Magnetic Switch attached to pin 2 from +5V
   10K resistor attached to pin 2 from ground (not in current configuration, internal pullup is being used)
   LED attached from pin 9 to 220ohm resistor to DIGITAL ground 

  created  10MAR16

*/

// this constant won't change:
const int  SwitchPin = 2;    // the pin that the reed switch is attached to (2 to +5v)
const int ledPin = 9;       // the pin that the LED is attached to   (9 to gnd)

// Variables will change:
int SwitchCounter = 0;   // counter for the number of button presses
int SwitchState = 0;         // current state of the button
int lastSwitchState = 0;     // previous state of the button

void setup() {
  
  pinMode(SwitchPin, INPUT); // initialize the switch pin as a input
  pinMode(SwitchPin, LOW); //initialize switch pin as LOW till pressed
  pinMode(ledPin, OUTPUT); // initialize the LED as an output:
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the switch input pin:
  SwitchState = digitalRead(SwitchPin);  // compare the SwitchState to its previous state
   
    if (SwitchState != lastSwitchState) // if the state has changed, increment the counter
  {
    lastSwitchState=SwitchState;
    
    if (SwitchState == HIGH) // if the current state is HIGH then the switch went from off to on:
    {  
      SwitchCounter++;
      Serial.println("Door Open");
      Serial.print("Number of times door has been opened:  ");
      Serial.println(SwitchCounter);
      Serial.println(digitalRead(SwitchPin));  //Display current value
      delay(20);
      // fade in from min to max in increments of 5 points:
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
        // sets the value (range from 0 to 255):
        analogWrite(ledPin, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(50);
        Serial.print("Fading In    ");
      }
    } else {
      // if the current state is LOW then the switch
      // went from on to off:
      Serial.println("Door Closed");
      for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
        // sets the value (range from 0 to 255):
        analogWrite(ledPin, fadeValue);
        // wait for 30 milliseconds to see the dimming effect
        delay(60);
        // Delay a little bit to avoid bouncing
        delay(50);
        Serial.print("Fading Out    ");
      }


    }    }    }
        // wait for 30 milliseconds to see the dimming effect
        delay(50);

When the useless comment and the code do not agree, the useless comment looks stupid.

The for loop to fade up iterates 52 times, pausing for 50 milliseconds per iteration, for a total of 2.6 seconds.

there is a significant delay

Define “significant”. Is it, by any chance, 2.6 seconds?

If it is longer than that, we need to know how the switch is wired. You are not using the internal pullup resistor, so an external resistor (in pulldown mode, since you assume that HIGH means pressed) is required. You do have one, right?