This week we attach a joystick control to an Arduino to control a pair of servos.  It is much simpler than you think!

Downloads

HD Apple HD Apple SD Audio MP3

Example Code

Reading Joystick Input and outputting to Serial Monitor

/*############### READ JOYSTICK INPUT AND OUTPUT POSITION ##################
       This code is reading the input from an XY joystick and outputting the
       position to the serial port every half second.  The joystick has 2
       pots that are varied by moving the joystick.
       
       To get the position we read analog pins 0 and 1.  There is also a button
       on the joystick that is activated by pressing the joystick.  That can
       be read by reading any digital input.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 20 (http://tech-zen.tv/index.php/shows/let-s-make-it/episodes/59-sensor-fun-with-arduino-1-massive-failure-but-4-successes-let-s-make-it-episode-6)
       
       http://tech-zen.tv
       
       For the sample code, show notes, contact information and many more 
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of 
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around 
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking 
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can 
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for 
       a new Let's Make it episode, you can contact us via email or phone at 
       the show information page.
################################################################################*/

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorPin2 = A1;
int buttonPin = 2;
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensorValue2 = 0;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); 
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); 
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  sensorValue2 = analogRead(sensorPin2);   
  
  Serial.print("X Value: ");
  Serial.println(sensorValue); 
  Serial.print("Y Value: ");
  Serial.println(sensorValue2);
  
  Serial.println("");
  
  delay(500);                  
}

Controlling a Servo With The Joystick

/*############### READ JOYSTICK INPUT AND POSITION SERVOS ##################

      This code is reading the input from an XY joystick and positioning 2 servos based on the input 
from the joystick. The joystick has 2 pots that are varied by moving the joystick. To get the position we read analog pins 0 and 1. There is also a button on the joystick that is activated by pressing the joystick. That can be read by reading any digital input. From: Mike Myers (http://mikemyers.me) @netnutmike Let's Make It Episode 20 (http://tech-zen.tv/index.php/shows/let-s-make-it/episodes/59-sensor-fun-with-arduino-1-massive-failure-but-4-successes-let-s-make-it-episode-6)
http://tech-zen.tv
For the sample code, show notes, contact information and many more videos, visit the show page at http://tech-zen.tv/letsmakeit Please subscribe to our YouTube channel or our netcasts at any of your favorite netcast / podcast outlets. We normally record Let's Make It live on Tuesday evenings around 7pm eastern. You can watch it live by going to tech-zen.tv and clicking the live link at the top of the page. We also have a community setup for our viewers and listeners. You can join the community by going to community.tech-zen.tv. We love input on what you would like to know or if you have an idea for a new Let's Make it episode, you can contact us via email or phone at the show information page. ################################################################################*/ #include <Servo.h> Servo myservo; Servo myservo2; int sensorPin = A0; // select the input pin for the potentiometer int sensorPin2 = A1; int buttonPin = 2; int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int sensorValue2 = 0; int servoPin = 9; int servoPin2 = 10; int maxServo = 175; int minServo = 2; int maxPotValue = 1023; void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); myservo.attach(servoPin); myservo2.attach(servoPin2); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); sensorValue2 = analogRead(sensorPin2); myservo.write(map(sensorValue, 0, maxPotValue, minServo, maxServo)); myservo2.write(map(sensorValue2, 0, maxPotValue, minServo, maxServo)); if (digitalRead(buttonPin)) { digitalWrite(ledPin, true); } else { digitalWrite(ledPin, false); } delay(25); }