Arduino/python25 joystick turrent

Ok i don't know python, but luckly the good people at principle labs particulialy Brian already made open source coding for controlling multiple servos via joystick. only problem im having is the coding that i was able to get has the joystick trigger close python (not ideal for my purposes)! what i want is for the trigger (button 0) to charge a pin on my arduino (ATMEGA168) now im a big fan of KISS (Keep It Simple Stupid) so i would like to tweak the existing code instead of opening new modules or redoing the ide code. Im not a programmer but i would not like to change the codes(python25, arduino) too much or add any modules. so im thinking to program button0 to control a third servo (there is no third servo but i need a current going to feed a pin on the arduino, which will be for umm... a laser pointer!)

suppossing its for a digital pin will the pwm keep the current going to the selected pin charged and opened or will there be too much inconsistancy in the signal being on/off?

if i use an analog pin instead will that keep the circuit alive until the joystick trigger is depressed?

all the code im using came from this link:

If you google this it will be the first result

arduino-python-4-axis-servo-control

is this possible? if you can help or have insight i will try to host/make avilable this upgraded code as long as i can (it could be used for many different applications)

the project is in peices but i will upload pics/movie schematics as soon as its in workable condition.

only problem im having is the coding that i was able to get has the joystick trigger close python (not ideal for my purposes)!

Even though this isn't a python forum, if you post the python code, someone will be able to figure out where the close occurs.

what i want is for the trigger (button 0) to charge a pin on my arduino (ATMEGA168)

What would you like to charge it? $1 per press? :slight_smile:
Should be simple enough.

so im thinking to program button0 to control a third servo (there is no third servo but i need a current going to feed a pin on the arduino, which will be for umm... a laser pointer!)

Digital pins are either on (current is flowing) or off (no current flowing). There is no reason to make the code pretend that there is a servo there, if that is not what is connected to the pin.

Note that there is a limited amount of current that a digital pin can supply, and that the "umm... a laser pointer!" may require more current than the digital pin can supply.

You need to tell us more about the "umm... a laser pointer!".

if i use an analog pin instead will that keep the circuit alive until the joystick trigger is depressed?

Analog pins are input only. They can not supply current.

im trying to trip a relay and the arduino's 40mA signal from a digital pin is enough to do that. after the relay trips it will open up the power supply im using which is 9.6v. im assuming the arduino code will have to be updated as well. here is the complete python code:

#!/usr/bin/env python

################################################################
# Module:   multijoystick.py
# Created:  2 April 2008
# Author:   Brian D. Wendt
#   http://principialabs.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''
Provides four-axis joystick servo control from a PC
using the Arduino "MultipleServos" sketch
and the Python "servo.py" serial abstraction module.

Parts of this code were 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/
  servo    - http://principialabs.com/
 Win32 users may also need:
  pywin32  - http://sourceforge.net/projects/pywin32/
'''
################################################################

import servo
import pygame

# allow multiple joysticks
joy = []

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

        if (e.dict['axis'] == 1):
            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)
                serv = int(90 + -move)
                # and send to Arduino over serial connection
                servo.move(1, serv)

            # Arduino joystick-servo hack
            if (axis == "Y"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                serv = int(90 + move)
                # and send to Arduino over serial connection
                servo.move(2, serv)

            # Arduino joystick-servo hack
            if (axis == "Z"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                serv = int(90 + move)
                # and send to Arduino over serial connection
                servo.move(3, serv)

            # Arduino joystick-servo hack
            if (axis == "Throttle"):
                pos = e.dict['value']
                # convert joystick position to servo increment, 0-180
                move = round(pos * 90, 0)
                serv = int(90 + move)
                # and send to Arduino over serial connection
                servo.move(4, serv)

    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"
            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()

and the code on the arduino is:

/*
 * ------------------------------
 *   MultipleSerialServoControl
 * ------------------------------
 *
 * Uses the Arduino Serial library
 *  (http://arduino.cc/en/Reference/Serial)
 * and the Arduino Servo library
 *  (http://arduino.cc/en/Reference/Servo)
 * to control multiple servos from a PC using a USB cable.
 *
 * Dependencies:
 *   Arduino 0017 or higher
 *     (http://www.arduino.cc/en/Main/Software)
 *   Python servo.py module
 *     (http://principialabs.com/arduino-python-4-axis-servo-control/)
 *
 * Created:  23 December 2009
 * Author:   Brian D. Wendt
 *   (http://principialabs.com/)
 * Version:  1.0
 * License:  GPLv3
 *   (http://www.fsf.org/licensing/)
 *
 */

// Import the Arduino Servo library
#include <Servo.h> 

// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// TO ADD SERVOS:
//   Servo servo5;
//   etc...

// Common servo setup values
int minPulse = 600;   // minimum servo position, us (microseconds)
int maxPulse = 2400;  // maximum servo position, us

// User input for servo and position
int userInput[3];    // raw input from serial buffer, 3 bytes
int startbyte;       // start byte, begin reading input
int servo;           // which servo to pulse?
int pos;             // servo angle 0-180
int i;               // iterator

void setup() 
{ 
  // Attach each Servo object to a digital pin
  servo1.attach(2, minPulse, maxPulse);
  servo2.attach(3, minPulse, maxPulse);
  servo3.attach(4, minPulse, maxPulse);
  servo4.attach(5, minPulse, maxPulse);
  // TO ADD SERVOS:
  //   servo5.attach(YOUR_PIN, minPulse, maxPulse);
  //   etc...

  // Open the serial connection, 9600 baud
  Serial.begin(9600);
} 

void loop() 
{ 
  // Wait for serial input (min 3 bytes in buffer)
  if (Serial.available() > 2) {
    // Read the first byte
    startbyte = Serial.read();
    // If it's really the startbyte (255) ...
    if (startbyte == 255) {
      // ... then get the next two bytes
      for (i=0;i<2;i++) {
        userInput[i] = Serial.read();
      }
      // First byte = servo to move?
      servo = userInput[0];
      // Second byte = which position?
      pos = userInput[1];
      // Packet error checking and recovery
      if (pos == 255) { servo = 255; }

      // Assign new position to appropriate servo
      switch (servo) {
        case 1:
          servo1.write(pos);    // move servo1 to 'pos'
          break;
        case 2:
          servo2.write(pos);
          break;
        case 3:
          servo3.write(pos);
          break;
        case 4:
          servo4.write(pos);
          break;
   // TO ADD SERVOS:
   //     case 5:
   //       servo5.write(pos);
   //       break;
   // etc...
      }
    }
  }


}

can figure out how to get the JOYBUTTONDOWN command to bridge into the arduino and get the current flowing to a digital pin, tried to use one of the servo's PWM pins to get the relay to rip, can hear the pulsing in the relay but no trip

i got tons of usb extension cables so 127ft should be just far enough to make it to my driveway/opened garage can you say....... webcam

so far just need the code then can compile a tutorial:

just uploaded some pics on what it looks like right now, i don't mind doing the arduino part of the mod. but the python code is killing me, i know it shouldn't be difficult i just don't know the syntax/modules at all. is it possible to do with the python-arduino bridge? or am i going to have to run a line from the joystick button/switch to the arduino? which should be possible, but since it will be a irq wouldn't that stop the serial servos from moving when the trigger switch is depressed? please someone let me know if its possible with the python/arduino-serial bridge. only reason im asking is because im sure this has been done before, and i cant find this desired code anywhere