Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Module code for Arduino - XY Joystick to Control XY Servos
// Adapted by gmb, given freely as-is, without any guarantees
#include <Servo.h>
Servo xservo;
Servo yservo;
int xPin = A0;
int yPin = A1;
//int ledPin = 13;
//int buttonPin = 2;
int xval = 0;
int yval = 0;
//int buttonState = 0;
void setup()
{
xservo.attach(11);
yservo.attach(10);
// pinMode(ledPin, OUTPUT);
// pinMode(buttonPin, INPUT);
// digitalWrite(ledPin, LOW);
Serial.begin(9600); // also set up Serial port for debugging
}
void loop() {
// SERVO CONTROL WITH JOYSTICK
xval = analogRead(xPin);
yval = analogRead(yPin);
xval = map(xval, 0, 1023, 0, 179); // map Joystick to Servo values
yval = map(yval, 0, 1023, 0, 179); // map Joystick to Servo values
xservo.write(xval); // Turn servo to specified angle on Joystick X axis
yservo.write(yval); // Turn servo to specified angle on Joystick X axis
/*SEL BUTTON
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
*/
// Serial.println(xval);
// Serial.println(yval);
// Serial.println(buttonPin);
delay(10); // debounce values a wee bit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.