 const int switchPin2 = 4;
 const int ledPin = 3;
 
+// global to keep track of the servo angle
 int servoPos =0;
 
 void setup(){


void rotateRight(){
 for(; servoPos >= 0; servoPos--){
   servo1.write(servoPos);
   int out = map( servoPos,0,180,0,255);
   analogWrite(ledPin,out);
   delay(20);
   if(digitalRead(switchPin1) == HIGH){
     rotateLeft();
     break;

}

void loop(){
  
  // rotate the servo left if switch one is pressed
  if (digitalRead(switchPin1) == HIGH){
   rotateLeft(); 
  }
  
  // rotate the servo right if switch two is pressed
  else if(digitalRead(switchPin2) == HIGH){
   rotateRight(); 
  }
  
  
  
}

void rotateLeft(){
  for(; servoPos < 180; servoPos++){
    
   // writing to servo
   servo1.write(servoPos);  
    
   // maping servo position to analog value between 0 and 255   
   int out = map( servoPos,0,180,0,255);
   analogWrite(ledPin,out);   
   
   delay(20); 
   
   if(digitalRead(switchPin1) == HIGH){
