Comments: Wireless Joystick Hookup Guide

Pages

Comments 12 comments

  • I purchased a board for a remote control robot I’m making. I see the transmission example code, but is there example code to receive joystick data and drive the robot?

  • I am having trouble using the OLED Display. I have connected the Micro OLED Breakout board how it is mentioned in this guide and the display is not powering on.

    I have verified the voltage at the display, as well as ensured I had the latest OLED library (v1.2) installed, yet whenever I upload the code in this guide, or the Micro OLED demo code the display does not appear to turn on.

    Out of ideas, can anyone help?

  • Soldered up for dual axis and wanting to try out the sample sketch. It appears that the files for the SparkFun SAMD21 Dev Breakout are here: https://github.com/sparkfun/SAMD21_Dev_Breakout

    But when I download it as a zip file, I can’t add it into my library: “Specified file does not contain a valid library.” I presume I’m trying to add this in the wrong way?

  • What kind of male connector does the USB interface wire take? It looks like its another size than the one that usually attaches an arduino.

  • I purchased a wireless joystick board, and am having problems downloading code to the board via USB. I compile code using the Arduino IDE (version 1.8.4), it compiles, but then gets an error ‘No device found on COM9’ when trying to upload the code to the board. I noticed in my device manager (windows 8.1 64 bit) that there was an ‘other device’ present labeled SparkFun SAMD21 showing an error, but also it shows up in the list of ports as COM9 with no errors associated with it. Is there a driver that is needed for this board? I have tried pushing the reset button to no avail. Thanks.

  • This is my version with just the buttons

    /* Wireless Joystick Buttons
     * 
     * license: Creative Commons Attribution-ShareAlike 4.0 (CC BY-SA 4.0)
     * Do whatever you'd like with this code, use it for any purpose.
     * Please attribute and keep this license. 
     * 
     * This is example code for the Sparkfun Wireless Joystick using XBee. 
     * Modified from the tank code found here https://learn.sparkfun.com/tutorials/wireless-joystick-hookup-guide
     * 
                                           X
                                         Y   A
                                           B
     */
    
    #define UP_BUTTON 8 // X
    #define DOWN_BUTTON 2 // B
    #define LEFT_BUTTON 4 // Y
    #define RIGHT_BUTTON 9 // A
    
    #define L_TRIG 6        // Pin used for left trigger
    #define R_TRIG 3        // Pin used for right trigger
    
    #define JOYSTICK_BUTTON 5
    
    #define V_JOYSTICK A3   // Pin used for left joystick
    #define H_JOYSTICK A2 
    
    
    void setup() {  
      Serial1.begin(9600); // Start serial communication with XBee at 9600 baud
      delay(10);
      Serial1.print("W7001\r\n"); // Set the bit in enable register 0x70 to 0x01
    
      pinMode(L_TRIG,INPUT_PULLUP); // Enable pullup resistor for left trigger
      pinMode(R_TRIG,INPUT_PULLUP); // Enable pullup resistor for right trigger
      pinMode(UP_BUTTON,INPUT_PULLUP);
      pinMode(DOWN_BUTTON,INPUT_PULLUP);
      pinMode(LEFT_BUTTON,INPUT_PULLUP);
      pinMode(RIGHT_BUTTON,INPUT_PULLUP);
      pinMode(JOYSTICK_BUTTON,INPUT_PULLUP);
    }
    
    void loop() {
      int16_t horizontalJS, verticalJS;    // We'll store the the analog joystick values here
      char hor[10],vert[10]; // character buffers used to set motor speeds
    
      char joySTBuf[9];
      joySTBuf[0] = 'J';
     (digitalRead(L_TRIG)==0) ? joySTBuf[1] = '1' : joySTBuf[1] = '0';
     (digitalRead(R_TRIG)==0) ? joySTBuf[2] = '1' : joySTBuf[2] = '0';
     (digitalRead(UP_BUTTON)==0) ? joySTBuf[3] = '1' : joySTBuf[3] = '0';
     (digitalRead(DOWN_BUTTON)==0) ? joySTBuf[4] = '1' : joySTBuf[4] = '0';
     (digitalRead(LEFT_BUTTON)==0) ? joySTBuf[5] = '1' : joySTBuf[5] = '0';
     (digitalRead(RIGHT_BUTTON)==0) ? joySTBuf[6] = '1' : joySTBuf[6] = '0';
     (digitalRead(JOYSTICK_BUTTON)==0) ? joySTBuf[7] = '1' : joySTBuf[7] = '0';
      joySTBuf[8]='\0'; // this is the null character. this tells other stuff the char array is at the end. (in case you didn't know.)
    
    
    horizontalJS = analogRead(H_JOYSTICK);
    verticalJS = analogRead(V_JOYSTICK);
    
    sprintf(hor,"H%d",horizontalJS);
    sprintf(vert,"V%d",verticalJS);
    
     Serial1.println(joySTBuf);
     delay(2);
     Serial1.println(hor);
     delay(2);
     Serial1.println(vert);
     delay(2);
    
    }
    
    • and Processing to test (you must change COM5 to your port. use an xbee explorer connected to your computer and send the joystick to the test code

      import processing.serial.*;
       Serial myPort;
      
      String serialInputString ="";
      boolean UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON,L_TRIG,R_TRIG,JOYSTICK_BUTTON;
      
      float controllerWidth = 516;
      float controllerHeight = 280;
      float rawH=512,rawV = 512;
      float HOffSet,VOffSet = 0;
      
      void setup() {
        size(640, 640);
         myPort = new Serial(this, "COM5", 9600); //Set up serial
         myPort.bufferUntil('\n');  
        //dummySerialEvent();
      }
      
      
      void draw() {
        background(50,50,50);
        rectMode(CENTER);
        fill(200,100,100);
        rect(width/2,height/2, controllerWidth, controllerHeight, 7);
      
        drawButtons();
        drawJoystick(); 
        delay(5);
      }
      
      void drawButtons()
      {
       if (UP_BUTTON) {  fill(10,10,250); } else { fill(250,250,250); }
        rect(width/2+controllerWidth/4,height/2-50, 35, 35, 3);
       if (DOWN_BUTTON) {  fill(250,250,10); } else { fill(250,250,250); }
        rect(width/2+controllerWidth/4,height/2+50, 35, 35, 3);
      
       if (LEFT_BUTTON) {  fill(10,250,10); } else { fill(250,250,250); }
        rect(width/2+controllerWidth/4-50,height/2, 35, 35, 3);
       if (RIGHT_BUTTON) {  fill(250,10,10); } else { fill(250,250,250); }
        rect(width/2+controllerWidth/4+50,height/2, 35, 35, 3);
      
       if (L_TRIG) {  fill(10,250,250); } else { fill(250,250,250); }
        rect(width/2-105,(height-controllerHeight)/2, 55, 15, 3);
       if (R_TRIG) {  fill(10,250,250); } else { fill(250,250,250); }
        rect(width/2+105,(height-controllerHeight)/2, 55, 15, 3);
      }
      
      void drawJoystick()
      {
        if (JOYSTICK_BUTTON) {  fill(10,250,10); } else { fill(250,250,250); }
        ellipse(width/2-controllerWidth/4,height/2, 135, 135);
        HOffSet = map(rawH, 0, 1023,40, -40);
        VOffSet = map(rawV, 0, 1023, 40, -40);
        fill(10,10,10);
        ellipse(width/2-controllerWidth/4+HOffSet,height/2+VOffSet, 35, 35);
      }
      
      void serialEvent (Serial myPort)
      {
        serialInputString = myPort.readStringUntil('\n'); //Gets val
        processStrings(serialInputString);
      }
      
      /*
      void testSerialEvent()
      {
        processStrings("J0000000");
         processStrings("H1021");
         processStrings("V216");
      }
      */
      void processStrings(String theInput)
      {
        if (theInput.charAt(0) == 'J') { parseButtonValues(theInput); }
        if (theInput.charAt(0) == 'H') { parseHorizontalValue(theInput); }
        if (theInput.charAt(0) == 'V') { parseVerticalValue(theInput); }
      }
      
      void parseButtonValues(String buttonValues)
      {
        if (buttonValues.charAt(1) == '1') {L_TRIG = true;} else {L_TRIG = false;}
        if (buttonValues.charAt(2) == '1') {R_TRIG = true;} else {R_TRIG = false;}
        if (buttonValues.charAt(3) == '1') {UP_BUTTON = true;} else {UP_BUTTON = false;}
        if (buttonValues.charAt(4) == '1') {DOWN_BUTTON = true;} else {DOWN_BUTTON = false;}
        if (buttonValues.charAt(5) == '1') {LEFT_BUTTON = true;} else {LEFT_BUTTON = false;}
        if (buttonValues.charAt(6) == '1') {RIGHT_BUTTON = true;} else {RIGHT_BUTTON = false;}
        if (buttonValues.charAt(7) == '1') {JOYSTICK_BUTTON = true;} else {JOYSTICK_BUTTON = false;}
      }
      
      void parseHorizontalValue(String HValue)
      {
        float JSValues[] = float(split(HValue,"H"));
        rawH=JSValues[1];
      }
      
      void parseVerticalValue(String VValue)
      {
        float JSValues[] = float(split(VValue,"V"));
        rawV=JSValues[1];
      }
      
  • I am having an issue with this hookup guide. Seems nice but issue 1 is a missing “sam.h” file. Something in the background has changed since you added this hookup guide. I even found the missing file and created a reference to it but additional errors stating additional missing files have occurred. Can you confirm what arduino IDE, version of boards that do work with this wireless joystick? I think if you were to try this guide again, you will see that it doesn’t work as described. I am fairly confident that I have covered my bases but hey, I’m human too. So, can we get some help? Thank you,

    • You should be using the “SparkFun SAMD21 Dev Breakout” board definitions, but you’ll also need to have the “Arduino SAMD Boards” installed as well. I’m running version 1.6.11 of the IDE, and haven’t run into issues other than occasionally having to put the board into bootloader mode by quickly pressing the reset button twice.

  • Nice!

    Have you considered having code for just sending the values of the buttons and joysticks out? Then you could have other stuff talk to the common format of the joystick without needing to recode your joystick each time.

    I’m making the code myself starting from Alex’s Tank, so if you want you can have mine once I test it if you like.

    • Hi, have you finished making your code? I am working on controlling a robot and an example on how to interpret and receive the data would be helpful. Thanks


If you've found a bug or have other constructive feedback for our tutorial authors, please send us your feedback!