How to Control 2 Axis Gun with joystick

       


            CODE 

#include <Servo.h>
//Youtube Channel(Tech Technology Pk)
Servo servoX;
Servo servoY;
int joyX, joyY;

void setup() {
  servoX.attach(9); // Change these pins to the ones your servos are connected to
  servoY.attach(10);
 
}

void loop() {
  joyX = analogRead(A0); // Assuming joystick X-axis is connected to A0
  joyY = analogRead(A1); // Assuming joystick Y-axis is connected to A1
 
  int angleX = map(joyX, 0, 1023, 0, 180); // Mapping joystick X-axis value to servo angle
  int angleY = map(joyY, 0, 1023, 0, 180); // Mapping joystick Y-axis value to servo angle
 
  servoX.write(angleX); // Move servo X to the calculated angle
  servoY.write(angleY); // Move servo Y to the calculated angle

}

Post a Comment

Previous Post Next Post

Contact Form