Arduino and Joysticks

I am playing around with some linear actuators (Glideforce) and Pololu USB Motor Controllers with feedback and an Arduino Mega 2560. Currently I am using a little Sunfounder Joystick PS2 module. The joystick PS2 module has two lines of analog output (X, Y) and one of digital output (Z). The cross rocker is a bidirectional 10k resistor. It's ok for playing around, but does anyone have any experience or recommendations for a bigger joystick that I can just drop in and not have to rewrite code (for now, its a very simple application)? Preferably one that is panel mounted and isn't hundreds of dollars? I would like one that is a bit more durable for a class that I will be teaching....TIA!

From what I can find with the link you posted to the Sunfounder Joystick PS2 module it appears to have two pots internal and a switch. Most any analog joystick should work. Not all have the switch and it may not be configured the same. Look for some bigger ones and see what they tell you.

Any Nintendo will easily work. The following code was used for a servo powered lock and required the user to successfully input the Konami code of NES fame. I also adapted the code to work the internals on a mock up of the TARDIS interior I did for Halloween one year by assigning different buttons and directions to different outputs: lights, sounds etc.

  /*
   No need to add LED lighting code explicitly, it's all already here!
   Adding the LEDs to pins 5, 6, and 8 (A, B and start) allows other free effects
   too using the NES Advantage TURBO and SLOW buttons and dials
   Adapted from Adafruit "Classy Solution"  by Hallowed31 to use Nintendo controller
  instances instead of led instances

     _____

                     0     --0v (ground)
     +5V  --  0      0     --CLOCK          Clock to Pin 3
nothing  ---  0      0     --LATCH          Latch goes to Pin 2 
nothing  ---  0      0     --SERIAL OUT     Serial Out to Pin 4

NOTE: also works on 3.3V

NES Controller wire colouring guide:
    +5V: white
    Ground: brown
    Pulse: red(3)
    Latch: orange(2)
    Serial Data Out: yellow(4)   
                   _______

    Servo object in this sketch is pin 13 by default
*/


#include <Servo.h>

const int ledpin = 11; 
const int latch = 2; // set the latch pin   mid
const int clock = 3; // set the clock pin   top
const int datin = 4;// set the data in pin  bottom

class Contra
{
    //Class Member Variables
    //These are initialized at startup
    //servo object
    Servo servo;


    //servo member variables
    int increment;             // increment to move for each interval
    int pos = 0;               // current servo position

    //keep track of time
    int  updateInterval;       // interval between updates
    unsigned long lastUpdate;  // last update of position

    ///NES controller
    int var;

    byte controller_data = 0;

  public:
    Contra(int interval)
    {
      updateInterval = interval;
      increment = 1;
    }

    void Attach(int pin)
    {
      servo.attach(pin);
      servo.write(10);
    }

    void Detach()
    {
      servo.detach();
    }

    /* THIS READS THE CONTROLLER DATA */
    void controllerRead() {
      controller_data = 0;
      digitalWrite(latch, LOW);
      digitalWrite(clock, LOW);

      digitalWrite(latch, HIGH);
      delayMicroseconds(2);
      digitalWrite(latch, LOW);

      controller_data = digitalRead(datin);

      for (int i = 1; i <= 7; i ++) {
        digitalWrite(clock, HIGH);
        delayMicroseconds(2);
        controller_data = controller_data << 1;
        controller_data = controller_data + digitalRead(datin) ;
        delayMicroseconds(4);
        digitalWrite(clock, LOW);
      }
    }

    void Update()
    {
      if ((millis() - lastUpdate) > updateInterval) // time to update
      {
        lastUpdate = millis();

        controllerRead();
        Serial.println(controller_data, BIN);

        Serial.println(var);

        if (controller_data == B11110111) //UP, var has become 1
          if (var == 0) {
            var = 1;
          }

        if (var == 1) {
          if (controller_data == B11110111) //UP
            var++;
        }

        if (var == 2) {
          if (controller_data == B11111011) //DOWN
            var++;
        }


        if (var == 3) {
          if (controller_data == B11111011) //DOWN
            var++;
        }

        if (var == 4) {
          if (controller_data == B11111101) //LEFT
            var++;
        }

        if (var == 5) {
          if (controller_data == B11111110) //RIGHT
            var++;
        }

        if (var == 6) {
          if (controller_data == B11111101) //LEFT
            var++;
        }

        if (var == 7) {
          if (controller_data == B11111110) //RIGHT
            var++;
        }


        if (controller_data == B10111111) {      //B
          if (var == 8) {
            var = 9;
          }
        }

        if (var == 9) {                          //Tripwire 1
          if (controller_data != B11111111) { //no buttons pressed
            if (controller_data != B01111111) { //A
              if (controller_data != B10111111) { //B
                var = 0;
              }
            }
          }
        }

        if (var == 9) {
          if (controller_data == B01111111) {    //A
            var = 10;
          }
        }

        if (var == 10) {                          //Tripwire 2
          if (controller_data != B11111111) { //no buttons pressed
            if (controller_data != B11101111) { //start
              if (controller_data != B01111111) { //A
                var = 0;   //If you enter something wrong, like UP,
              }            // var will go back to 0
            }
          }
        }

        if (var == 10) {
          if (controller_data == B11101111) {     //START
            var = 11;
          }
        }

        if (controller_data == B11011111) { //SELECT, OFF
          var = 0;
          digitalWrite(ledpin, LOW);
        }

        if (var == 11) { //If the code has been entered correctly var=11
          digitalWrite(ledpin, HIGH); //LED is on, and motors work
          if (controller_data == B11110111) { //UP
            servo.write(178);
          } else {
            if (controller_data == B11111011) { //DOWN
              servo.write(0);
            } else {
              if (controller_data == B11111110) { //RIGHT
                servo.write(90);
              }
            }
          }
        }
        delay(100);
      }
    }
};

Contra contra1(10);

void setup() {
  Serial.begin(57600);         //Make sure to set your
  pinMode(latch, OUTPUT);      //serial port to 57600
  pinMode(clock, OUTPUT);      //if you want to use it
  pinMode(datin, INPUT);
  pinMode(ledpin, OUTPUT);

  digitalWrite(latch, HIGH);
  digitalWrite(clock, HIGH);

  contra1.Attach(13);
}



void loop() {
  contra1.Update();

}

Sounds good, thanks!

Outstanding, thanks, this helps!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.