servo problems sg90 3 way button

Hello

(sg90 , arduino nano, switch)
the idea is a switch with 3 positions to control a servo to 3 set position on wich they stand still and move fast to.
the servo has it's own power
I did try to make a good working code that does work but nothing worked : read a 3-way switch with (on /of /on1 ) to set ,this one don't work
:
#include<Servo.h>

Servo myservo;
int button_1 = 2;
int button_2 = 3;

int val=0;

void setup() {
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);

myservo.attach(9);
}

void loop()
{
val = digitalRead(button_1);
if (val == HIGH)
{
myservo.write(60);

}
val = digitalRead(button_2);
if (val == HIGH)
{
myservo.write(100);

}

}
and this one don't work :

Servo myservo;
int button_1 = 2;
int button_2 = 3;
int buttonstate1 = 0;
int buttonstate2 = 0;

void setup() {
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);

myservo.attach(9);
}
void loop(){
digitalRead(button_1);
if (buttonstate1 == HIGH);
{
myservo.write(60);

}
if (buttonstate2 == HIGH);
{
myservo.write(80);

}
}

I have build this with a normal switch for on/off read write servo and it works good for 2 positions. the servo act strange like its burning the motor etc. and does not respond to the button1 and little to button 2 but continuously makes noise and moves.
can some one explain why (and i do not always use resistors but i have them all sort, i don't experience the benefit (some experiment? and in digitalread the arduino don't need a resistor ''digital'')

the code that works good, of the on/off switch 2 way servo

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(155);
}
else {
servo1.write(60);
}
}

question 2 how to remove the jitter/ noise from servos standing still ?(the servo's& arduino's power +- are from 1 source split up to 9 servo's .1 arduino to 1 servo and switch)
It seems i have many cable close crossing etc lenghts off 2 meters. from buton to arduino to servo is 3or 4 meters totall. the project is not @my home so the time is limited. I'm planing to do the cables nicely and shorten them and solder. could be off influence??..
i noticed that `1 arduino nano and 4 servo don't work and it need be on for hours and not jitter or make sound, therefore every servo apart works better in reality.

it is a short code please help me with the whole code because off time but i want to understand what i'm missing ,thanks !:smiley:

First, please read Mr. Gammon's post at the top of the page and post your code using the </> icon.
How is your switch wired? Do you have pullup or pulldown resistors on the input pins?
Also your Arduino supply should be separate from the servo supply and the servo supply needs to be able to supply 1 amp times the maximum number of servos energized at the same time and you need a ground wire from the servo supply ground to Arduino ground.

pull down resistor i need : when the switch is closed, the micro-controller input is at a logical high value, but when the switch is open, the pull-down resistor pulls the input voltage down to ground (logical zero value)

but i have 17 servos become more to 25 servos some (4 with pull down resistors) but the still have the jitter , may be because the power to arduino and servo come from the same source ? outsider say : separate

need help with code for 2 on states, and 1 off, for the button to servo 3 states thanks

u can test : 1 servo 2 buttons ( 3 states on on off) 3 positions servo