Introduction: Control Arduino Using Joystick

after about a month of have my arduino I got bored so I was surfing the web and found this cool way of controlling arduino


if you do any thing illegal i am not held responsable

Step 1: Things Needed

1.arduino (duh)
2. computer (if u are reading this you have one unless it is not yours _)
3.usb ports(most computers have these)
4.time(took me 5 hours to figure it out & to cofigure to my servo)
5.servo
6.JOYSTIK

Step 2: Software You Need

arduino programer
python
pyserial(plug in for python)
pygame(plug in for python)
joystick drivers

Step 3: Programs

i have added the programs you need

arduino:

/*
  • JoystickSerialServo
  • --------------
  • Servo control with a PC and Joystick
*
*
*/

/** Adjust these values for your servo and setup, if necessary **/
int servoPin = 3; // control pin for servo motor
int minPulse = 600; // minimum servo position (to find use google)
int maxPulse = 2400; // maximum servo position (to find use google)
int refreshTime = 20; // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
int servoPosition; // commanded servo position, 0-180 degrees
int pulseRange; // max pulse - min pulse
long lastPulse = 0; // recorded time (ms) of the last pulse

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulseRange = maxPulse - minPulse;
centerServo = maxPulse - ((pulseRange)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
Serial.begin(9600);
}

void loop() {
// wait for serial input
if (Serial.available() > 0) {
// read the incoming byte:
servoPosition = Serial.read();

// compute pulseWidth from servoPosition
pulseWidth = minPulse + (servoPosition * (pulseRange/180));

// stop servo pulse at min and max
if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
if (pulseWidth < minPulse) { pulseWidth = minPulse; }

// debug
//Serial.println(servoPosition);
}

// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo's position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}

python:

#!/usr/bin/env python
#
# joystick-servo.py
#
# created 19 December 2007
# copyleft 2007 Brian D. Wendt
# http://principialabs.com/
#
# code adapted from:
# http://svn.lee.org/swarm/trunk/mothernode/python/multijoy.py
#
# NOTE: This script requires the following Python modules:
# pyserial - http://pyserial.sourceforge.net/
# pygame - http://www.pygame.org/
# Win32 users may also need:
# pywin32 - http://sourceforge.net/projects/pywin32/
#

import serial
import pygame

# allow multiple joysticks
joy = []

# Arduino USB port address (try "COM5" on Win32)
usbport = "COM8"

# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600)

# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
axis = "unknown"
if (e.dict['axis'] == 1):
axis = "X"

if (e.dict['axis'] == 0):
axis = "Y"

if (e.dict['axis'] == 2):
axis = "Throttle"

if (e.dict['axis'] == 3):
axis = "Z"

if (axis != "unknown"):
str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
# uncomment to debug
#output(str, e.dict['joy'])

# Arduino joystick-servo hack
if (axis == "X"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
servo = int(90 - abs(move))
else:
servo = int(move + 90)
# convert position to ASCII character
servoPosition = chr(servo)
# and send to Arduino over serial connection
ser.write(servoPosition)
# uncomment to debug
#print servo, servoPosition

elif e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
# uncomment to debug
#output(str, e.dict['joy'])
# Button 0 (trigger) to quit
if (e.dict['button'] == 0):
print "Bye!\n"
ser.close()
quit()
else:
pass

# print the joystick position
def output(line, stick):
print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
while True:
e = pygame.event.wait()
if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):
handleJoyEvent(e)

# main method
def main():
# initialize pygame
pygame.joystick.init()
pygame.display.init()
if not pygame.joystick.get_count():
print "\nPlease connect a joystick and run again.\n"
quit()
print "\n%d joystick(s) detected." % pygame.joystick.get_count()
for i in range(pygame.joystick.get_count()):
myjoy = pygame.joystick.Joystick(i)
myjoy.init()
joy.append(myjoy)
print "Joystick %d: " % (i) + joy[i].get_name()
print "Depress trigger (button 0) to quit.\n"

# run joystick listener loop
joystickControl()

# allow use as a module or standalone script
if name == "main":
main()

Step 4: Test It _

i did not write the python script
i found it on

http://principialabs.com/joystick-control-of-a-servo/

HAVE fun

4 servo version

http://principialabs.com/joystick-control-of-a-servo/

Step 5: Video

this is not my video its the video from the website

Comments

author
Great+Wight+Ninja made it!(author)2012-07-16

This would be a simple edit, but you might want to add a joystick as another "duh" thing you need.

author
bigbodysmallbrain made it!(author)2012-08-19

your right. totally skipped my mind

author
axeman911 made it!(author)2012-01-04

how do u upload the programs to the arduino if your using python?

author
bigbodysmallbrain made it!(author)2012-08-19

the arduino ide. python just sends commands over serial com

author
lane29 made it!(author)2010-12-13

nice background

author
bigbodysmallbrain made it!(author)2010-12-25

LOL. thanks

author
geeklord made it!(author)2009-05-17

That's funny, I just finished up a project almost exactly like this. I attached a joystick to an Arduino to a Servo motor! The only difference is that I dirrectly connected to the potentiometers in the joystick, I didn't use any programs or serial communication. Yours is great! I should post an 'ible for mine.

author
eliastorre made it!(author)2010-01-11

Hi there!

Hey please post an instructable for that!!! That would be interesting. And to the author, very cool instructable.

author
geeklord made it!(author)2010-01-24

Yea, that was a while ago... It's reeeaaallly simple, though. The potentiometers were very easy to access  once you got the plastic case off. Then I just used a couple analogRead's and the Servo library.

author
bigbodysmallbrain made it!(author)2010-01-12

thanks

author
bigbodysmallbrain made it!(author)2009-05-27

cool. but i use multiple joysticks for reason i dont quite konw

author
geeklord made it!(author)2009-05-27

o.....k.......

author
ReCreate made it!(author)2009-07-02

How could this get illegal?

author
bigbodysmallbrain made it!(author)2009-07-31

if you use it to throw ninja star to kill someone

author
ReCreate made it!(author)2009-07-31

Its like if you show someone to use a computer, and then they download warez, And then they sue you when the RIAA hits them, And then you say i am not held Liable blah blah blah...Nevermind >_