Can I turn on an LED, only if the switch has been closed for a desired amount of time?
I haven't tried it. But it compiles without errors. Let me know whether it works or not, please :)

#include &lt;time.h&gt;

const int iBallSwitchPin = 2;     // the number of the Ball Switch pin
const int ledPin =  13;      // the number of the LED pin

int iBallSwitchState = 0;         // variable for reading the Ball Switch status

int iCurrentTime = 0;     // Current time
int iElapsedTime = 0;     // Elapsed time

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the Ball Switch pin as an input:
  pinMode(iBallSwitchPin, INPUT);
}

void loop() {
  // read the state of the Ball Switch value:
  iBallSwitchState = digitalRead(iBallSwitchPin);

  // check if the Ball Switch State is HIGH:
  if (iBallSwitchState == HIGH) {

    iCurrentTime = time(NULL);

    do {
    iElapsedTime = time(NULL);
    } while ( iBallSwitchState == HIGH &amp;&amp; (iElapsedTime - iCurrentTime) &lt; 20);     // Checks the time for 20 seconds

      if((iElapsedTime - iCurrentTime) &gt;= 20)
        digitalWrite(ledPin, HIGH);     // turn Led on
      else
        digitalWrite(ledPin, LOW);   
    } // end if 
    else {
    digitalWrite(ledPin, LOW);      // turn LED off
  }
}