Created
March 30, 2014 01:12
-
-
Save GreyBurkart/9865718 to your computer and use it in GitHub Desktop.
Module code for Arduino - XY Joystick to Control XY Servos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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