How to control Servo Pan & Tilt kit using Joystick module





Parts list

- Arduino UNO R3
- SG90 Micro servo x2
- Pan&Tilt Kit
- Jumper wires
- Breadboard


Arduino Sketch

#include <Servo.h>

Servo servo_pan;
Servo servo_tilt;

int joyX = A0;
int joyY = A1;
int joyVal;

void setup() {
        servo_pan.attach(5);
        servo_tilt.attach(6);
}

void loop(){

  // code for servo pan
   joyVal = analogRead(joyX);
   joyVal = map( joyVal , 0 , 1023 , 0 , 180);
   servo_pan.write( joyVal);

   // code for servo tilt
   joyVal = analogRead(joyY);
   joyVal = map( joyVal , 0 , 1023 , 0 , 180);
   servo_tilt.write( joyVal);

   delay(15);
}

ความคิดเห็น