Skip to content

Instantly share code, notes, and snippets.

@elktros
Created July 5, 2018 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/9a0332254b10f53bb0aab3c1c42060cf to your computer and use it in GitHub Desktop.
Save elktros/9a0332254b10f53bb0aab3c1c42060cf to your computer and use it in GitHub Desktop.
Code for controlling two servos with Joystick and Arduino.
#include <Servo.h>
const int ledPin = 13;
const int xPin = A0;
const int yPin = A1;
const int switchPin = 2;
const int servoxPin = 10;
const int servoyPin = 11;
int xValue;
int yValue;
int X_pos = 90;
int Y_pos = 90;
volatile int switchState = 0;
Servo servox;
Servo servoy;
void setup()
{
pinMode (xPin, INPUT) ;
pinMode (yPin, INPUT) ;
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
servox.attach(servoxPin );
servoy.attach(servoyPin);
servox.write(X_pos);
servoy.write(Y_pos);
attachInterrupt(digitalPinToInterrupt(switchPin), switchISR, CHANGE);
}
void loop()
{
xValue = analogRead(xPin);
yValue = analogRead(yPin);
// For X-Axis Servo
if (xValue < 300)
{
if (X_pos < 10)
{
// Do Nothing
}
else
{
X_pos = X_pos - 5;
servox.write(X_pos);
delay(100);
}
}
if (xValue > 700)
{
if (X_pos > 160)
{
// Do Nothing
}
else
{
X_pos = X_pos + 5;
servox.write(X_pos);
delay(100);
}
}
// For Y-Axis Servo
if (yValue < 300)
{
if (Y_pos < 10)
{
// Do Nothing
}
else
{
Y_pos = Y_pos - 5;
servoy.write(Y_pos);
delay(100);
}
}
if (yValue > 700)
{
if (Y_pos > 160)
{
// Do Nothing
}
else
{
Y_pos = Y_pos + 5;
servoy.write(Y_pos);
delay(100);
}
}
}
void switchISR()
{
switchState = digitalRead(switchPin);
digitalWrite(ledPin, switchState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment