Controling servos with buttons

Ok guys, I found a problem :slight_smile: Thanks to everyone for helping :slight_smile: The code now looks like this :

 #include <Servo.h>

// constants won't change. They're used here to 
// set pin numbers:
Servo myservo;
const int buttonPin1 = 2;  
const int buttonPin2 = 3;
const int ledPin =  13;

// variables will change:
int buttonState1 = 0;   
int buttonState2 = 0;  
int pos = 0;

void setup() {
  myservo.attach(9);
  pinMode(ledPin, OUTPUT); 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);    
 pinMode(buttonPin2, INPUT);    
 myservo.write(pos);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {     
    // turn LED on:    
    pos=pos+5; 
    myservo.write(pos);
    digitalWrite(ledPin, HIGH);  
    delay(500);  
  } 
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    pos=pos-5; 
    myservo.write(pos);
    digitalWrite(ledPin, LOW);  
    delay(500);  
  } 
  
  }

manic9:
Yes Uno has a built in led in that, but it's just for testing that the buttons works

So why not use the built in LED and why no series resistor on the external LED ?

  if (buttonState2 == HIGH) {     
    // turn LED on:    
    pos=pos-5; 
    myservo.write(pos);
    digitalWrite(ledPin, LOW);  
    delay(500);  
  }

One of my favourite games (sad, I know) on here is to spot where code has been copy/pasted and not revised. This particularly applies to comments. At least this program does not have a variable called buttonState and a second one called buttonState2 added when the program was expanded.

Yeah as a noob I copy things from examples and sometimes forgot to delete stuff after copying that I do not need.... :frowning: