joystick push button how to

i am new to arduino. like a week young using it. i want to move a servo with a joystick’s push button. Push it and it goes to certain angle. push again and it goes to another angle. I also dont know how to hook up the push button to the arduino uno. I tried before with a code my friend gave me but i couldnt get it to work when i put it into the digital pin. Do i need a resistor in series to make it work ? Im soooo confused. Help please.

This is the breakout board for the joystick im using. Same joystick in pics as well.

Here is how to connect the switch. The capacitor is optional. The cap gives some hardware debounce, but you may still need software debounce to get reliable operation.

digital sw_2.jpg

Perfect !! I’ll try this out tonight !!

I just need a start on the code now. Going to see if i find something here from other posts. Still would love some help on it though.

Open your IDE. Got to File, Examples, Digital. There are examples showing programming for buttons (button, state change, debounce).

unfortunately i dont have my personal laptop with me :confused:
Here’s something i kinda did just now. Not sure if it will do what i want though. My idea now is to use 2 joystick push buttons. When one is pressed, it will go to a defined position and stay there until the other is pressed, which will go to the other defined position. I think this will work but the “go to position and stay there” is what im kinda doubting. I think i wrote it so that when the arduino reads the button being pushed, it will go to the position but when i release the button, it will go back to original position of (im assuming) 90 degrees.

#include <Servo.h>

Servo servo1;
int pos = 90;
int leftbtn = 1;
int rightbtn = 2;
int lbtnstate = 0;
int rbtnstate = 0;

void setup() {
	Serial.begin(9600);
	pinMode(leftbtn, INPUT)
	pinMode(rightbtn, INPUT)
	servo1.attach(9)

}
void loop() {
	
	lbtnstate = digitalRead(leftbtn);
	rbtnstate = digitalRead(rightbtn);

if (lbtnstate == HIGH){
	digitalWrite(pos, 45);
	servo1.write(pos); 
	delay(15);
	}	
		
if (rbtnstate == HIGH){
	digitalWrite(pos, 135)
	servo1.write(pos); 
	delay(15);
	}	
}

The button state will be LOW when pushed. If you command a servo the go to a position, it will stay there until told different. You are on the right track with the code. If both buttons are not pushed the if statements will be skipped and nothing will happen. I would not use pin 1 for a button as it is the hardware serial TX. I forgot to mention in my previous post that the 10K resistor is, also, optional if you enable the internal pull-up resistors with pinMode(myPin,INPUT_PULLUP);