Arduino Nano + Servo + Button + Jitter

Hello,

I want to do a pet feeder based on the cat putting a rubber ball down a tube with an cloned arduino nano, a servo and a switch.
My setup is basic servo on pin 9 and button on pin 2 as in the pictures.

The problem is that the servo is jittering when sitting, if i keep the button pressed its all quiet.

Is something wrong with the code or de setup?

The code is:
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int button = 2; // The button will be on Pin 7

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}

void loop()
{

if (digitalRead(button) == LOW)

for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
// waits 1s for the servo to reach the position
}
if (digitalRead(button) == HIGH)

for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}
}

IMG_1348 (Small).JPG

IMG_1349 (Small).JPG

Why do you output sequences of servo positions all the time, instead of only when the button state changes?

For the difference between button on/off: see the delay() for only one button state.
Avoid delay() in any final version of an sketch.

The jitter will disappear when you replace the for loops by only servo.write(pos), where pos specifies the final position (fully closed or open). Please note that servo.write() expects an angle between 0 and 180, not -90 to +90.

Hy, i”m not a code wizzard but is this what you mean?

#include <Servo.h>

Servo myservo;

int myservo = 0;
int button = 2;

void setup()
{
myservo.attach(9);
digitalWrite (button, LOW);
}

void loop()
{

if (digitalRead(button) == LOW)
myservo.write(0);

}
if (digitalRead(button) == HIGH)
myservo.write(90);

}
}

Thank you

Like that, but with less }

Please use code tags </> when you post code.