Problem with programming servomotors

For school I have to make a program with 2 servos, a potentiometer and a push button. When I use the potentiometer the servos have to turn a certain amount of degrees. And when I press the push button the 2 servos have to turn 90 °. Everything works, but when I add the push button to the program, the servos start to shake a lot. I included the arduino file and a .png of the program.

Thank you in advance.

programma_servo_met_potentiometer.ino (552 Bytes)

and a .png of the program.

Never ever do that again it is useless.

Your code will move the servo to 0 or 90 degrees depending on if the button is pressed or not. BUT it will also change the position of the servo depending on the pot value.

Can you see why this is silly? The servo dosn't know what to do because it is ALWAYS being told to go to two different places within microseconds of each other.

Yes, I see whats wrong but I do not know how I can fix this problem ? I am kinda new to programming with arduino. But thank you for responding already.

Change the code, so that if the button is pressed the servo is moved to 90 degree, otherwise it is moved to the angle defined by the potentiometer.

Could u help me with the program because I do not know how I should do that ?

OP's code

#include <Servo.h>

Servo myservo;
Servo myservo1;

int potpin = A0;
int val;

const int drukknop = 2;
int buttonState = 0;


void setup() {
  myservo.attach(5);
  myservo1.attach(7);
  pinMode(drukknop, INPUT);
}

void loop() {
  val = analogRead(potpin);            
  val = map(val, 0, 1023, 0, 180); 
  myservo.write(val);      
  myservo1.write(val);


  buttonState = digitalRead(drukknop);
  if(buttonState == HIGH){

  myservo.write(90);
  myservo1.write(90);
  } else{   
  myservo.write(0);
  myservo1.write(0);
  }
}

Sorry, I will not do your homework for you. But you already have an if statement, that defines what the servo does when the button is pressed. Have a look at that and think what you could change so that the servo moves according to the potentiometer if the button is not pressed. Then show us what you changed and describe what is going wrong if it does not work as expected.