Using two servos and two buttons.

Hope all are well.
Ran into my first real problem today re. programing and cant find anything in English to help.

I am trying to run four servos. Two will work from the usual thumb stick and I can manage that with all the videos on the subject. The other two will work from simple momentary switches. The switched ones have to move from rest (say 0 degrees) to a defined position when the button is pressed then return to 0 when the button is released.

I cant seem to find anything on the combined switching. can anyone help find me a program or video.

If your button switch were wired from an input to ground with the internal pull up enabled it will read LOW when pressed. Then the code:

if(digitalRead(buttonPin) == LOW)
{
  servo.write(presetPosition);
}
else
{
  servo.write(0);
}

Will keep the servo at presetPosition till the button is released, then the servo returns to 0.
The same code with different variables for the second switch.

Thank you for your help.

I was trying to include it in this sketch but as you can see I used the same parameters for all four inputs.

I'll have to have another think now and use the axes from the joystick and something else for the buttons. Although one of the buttons is the joystick press down.

// joystick and joystick button plus extra button (B) control for arduino. 4 servos'.

//include library for servo control
#include <Servo.h>

// Define servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

// Assign analog pin numbers
int joyX = A0;
int joyY = A1;
int joyZ = A2;
int joyB = A3;

int joyVal;

void setup()

{
// attach servos to digital pins
servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
servo4.attach(5);
}

void loop()
{
//read joystick value
joyVal = analogRead (joyX);
joyVal = map (joyVal, 0, 1023, 0, 180);
servo1.write(joyVal);

joyVal = analogRead (joyY);
joyVal = map (joyVal, 0, 1023, 0, 180);
servo2.write(joyVal);
delay(20);

joyVal = analogRead (joyZ);
joyVal = map (joyVal, 0, 30, 30,0);
delay(20);
servo3.write(30);
delay(20);

joyVal = analogRead (joyB);
joyVal = map (joyVal, 0, 90, 90,0);
delay(20);
servo4.write(90);
delay(20);

}

as you can see I used the same parameters for all four inputs.

Why? And, why is that one variable, that is used ONLY in loop(), global in scope?