//  Servo w/ switch
// 
//  Setup: - Wire a switch between digital pin 5 and ground
//         - Connect a servo to +, gnd and digital pin 6 
// ----------------------------------------------------------- 
 
int pos = 0;      // variable to store the servo position 
int sw;           // variable for state of switch
int servoPin = 6; // variable for servo pin number
 
void setup() 
{ 
  pinMode(5, INPUT_PULLUP);  // Sets digital pin 5 for input (getting button state)
} 
 
void loop() { 
   sw = digitalRead(5);
 if(sw == LOW){                    // If switch is closed (pin connected to ground)
   for(int i = 0; i < 40; i++){    // number of PWM pulses to send to motor
      digitalWrite(servoPin, HIGH); // servo control Pin high
      delayMicroseconds(1000);      // 1 millisecond positioning pulse
      digitalWrite(servoPin, LOW);  // servo control Pin low 
      delay(15);                    // (20 milliseconds in between pulses)
      } 
  }else {             
   for(int i = 0; i < 40; i++){    // number of PWM pulses to send to motor
      digitalWrite(servoPin, HIGH); // servo control Pin high
      delayMicroseconds(2000);      // 1 millisecond positioning pulse
      digitalWrite(servoPin, LOW);  // servo control Pin low 
      delay(15);                    // (20 milliseconds in between pulses)
      } 
     } 
}