Introduction: Control Arduino Using Joystick

About: vigilant ad low volantes scaphas

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