Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
ejshea
Published © GPL3+

4-Way Joystick Control

Control the pan and tilt of a camera with a 4-way joystick.

IntermediateFull instructions provided10,612
4-Way Joystick Control

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Limit Switch Wiring

Servo Motor Wiring

Code

Control Pan-Tilt Module with a 4-Way Joystick

Arduino
#include <Servo.h>

Servo servo1;  // create servo object to control servo 1
Servo servo2;  // create servo object to control servo 2

int pos1 = 0;    // variable to store servo1 position
int pos2 = 0;    // variable to store servo2 position
int joyUp = 0;
int joyDown = 0;
int joyLeft = 0;
int joyRight = 0;

void setup() {
  for(int i = 2; i < 6; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
  
  for(int j = 6; j < 9; j++){
    pinMode(j, INPUT);
  }
  pinMode(11, INPUT);
  
  servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  servo2.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
  joyUp = digitalRead(6);
  joyDown = digitalRead(7);
  joyLeft = digitalRead(8);
  joyRight = digitalRead(11);
  
  if (joyUp == 1){
    //move camera up
    if (pos2 <= 180){
      servo2.write(pos2);
      pos2++;
      delay(10);
    } 
  }

  if (joyDown == 1){
    //move camera down
    if (pos2 >= 0){
      servo2.write(pos2);
      pos2--;
      delay(10);
    }
  }

  if (joyRight == 1){
    //move camera right
    if (pos1 <= 180){
      servo1.write(pos1);
      pos1++;
      delay(10);
    }
  }

  if (joyLeft == 1){
    //move camera left
    if (pos1 >= 0){
      servo1.write(pos1);
      pos1--;
      delay(10);
    }
  }
}

Credits

ejshea

ejshea

16 projects • 30 followers

Comments