Joystick with 2 Servos

Image.png
Image.png
Image.png
COMPONENTS:
-Arduino Uno
-Joystick
-2x Servos

CODE:
#include “Servo.h”;
Servo servo1;
Servo servo2;
int x_key = A0;
int y_key = A1;
int x_pos;
int y_pos;
int servo1_pin = 11;
int servo2_pin = 12;
int initial_position = 90;
int initial_position1 = 90;
void setup ( ) {
  Serial.begin (9600) ;
  servo1.attach (servo1_pin ) ;
  servo2.attach (servo2_pin ) ;
  servo1.write (initial_position);
  servo2.write (initial_position1);
  pinMode (x_key, INPUT) ;
  pinMode (y_key, INPUT) ;
}
void loop ( ) {
  x_pos = analogRead (x_key) ;
  y_pos = analogRead (y_key) ;
  if (x_pos < 300) {
    if (initial_position < 10) { } else {
      initial_position = initial_position – 20;
      servo1.write ( initial_position ) ;
      delay (100) ;
    }
  } if (x_pos > 700) {
    if (initial_position > 180)
    {
    }
    else {
      initial_position = initial_position + 20;
      servo1.write ( initial_position ) ;
      delay (100) ;
    }
  }
  if (y_pos < 300) {
    if (initial_position1 < 10) { } else {
      initial_position1 = initial_position1 – 20;
      servo2.write ( initial_position1 ) ;
      delay (100) ;
    }
  } if (y_pos > 700) {
    if (initial_position1 > 180)
    {
    }
    else {
      initial_position1 = initial_position1 + 20;
      servo2.write ( initial_position1 ) ;
      delay (100) ;
    }
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s