Bluetooth and Joystick with teensy

Status
Not open for further replies.

carcaes1

Well-known member
Hi again

I'd like to ask one question. Is possible to program the teensy 3.2 / LC with a bluetooth module in order to work with it as a joystick usb (serial/joystick/keyboard) for using buttons without usb cable?

And PC can detect the joystick by bluetooth? Or only is possible with emulation keys of keyboard?

I'd like to use this example sketch by bluetooth.

Code:
/* Complete USB Joystick Example
   Teensy becomes a USB joystick with 16 or 32 buttons and 6 axis input

   You must select Joystick from the "Tools > USB Type" menu

   Pushbuttons should be connected between the digital pins and ground.
   Potentiometers should be connected to analog inputs 0 to 5.

   This example code is in the public domain.
*/

// Configure the number of buttons.  Be careful not
// to use a pin for both a digital button and analog
// axis.  The pullup resistor will interfere with
// the analog voltage.
const int numButtons = 16;  // 16 for Teensy, 32 for Teensy++

void setup() {
  // you can print to the serial monitor while the joystick is active!
  Serial.begin(9600);
  // configure the joystick to manual send mode.  This gives precise
  // control over when the computer receives updates, but it does
  // require you to manually call Joystick.send_now().
  Joystick.useManualSend(true);
  for (int i=0; i<numButtons; i++) {
    pinMode(i, INPUT_PULLUP);
  }
  Serial.println("Begin Complete Joystick Test");
}

byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle=0;

void loop() {
  // read 6 analog inputs and use them for the joystick axis
  Joystick.X(analogRead(0));
  Joystick.Y(analogRead(1));
  Joystick.Z(analogRead(2));
  Joystick.Zrotate(analogRead(3));
  Joystick.sliderLeft(analogRead(4));
  Joystick.sliderRight(analogRead(5));
  
  // read digital pins and use them for the buttons
  for (int i=0; i<numButtons; i++) {
    if (digitalRead(i)) {
      // when a pin reads high, the button is not pressed
      // the pullup resistor creates the "on" signal
      allButtons[i] = 0;
    } else {
      // when a pin reads low, the button is connecting to ground.
      allButtons[i] = 1;
    }
    Joystick.button(i + 1, allButtons[i]);
  }

  // make the hat switch automatically move in a circle
  angle = angle + 1;
  if (angle >= 360) angle = 0;
  Joystick.hat(angle);
  
  // Because setup configured the Joystick manual send,
  // the computer does not see any of the changes yet.
  // This send_now() transmits everything all at once.
  Joystick.send_now();
  
  // check to see if any button changed since last time
  boolean anyChange = false;
  for (int i=0; i<numButtons; i++) {
    if (allButtons[i] != prevButtons[i]) anyChange = true;
    prevButtons[i] = allButtons[i];
  }
  
  // if any button changed, print them to the serial monitor
  if (anyChange) {
    Serial.print("Buttons: ");
    for (int i=0; i<numButtons; i++) {
      Serial.print(allButtons[i], DEC);
    }
    Serial.println();
  }
  
  // a brief delay, so this runs "only" 200 times per second
  delay(5);
}

Thanks in advance
 
Last edited:
The keyboard/joystick mode for a Teensy is via USB. So if you have a bluetooth device that you plug into as a USB host that'd work. What I suspect you have is a bluetooth serial module, which lacks the commands or anything else to send joystick data. If you can modify or get a device that supports bluetooth joystick commands then a Teensy or any other suitable micro can act as the analog interface element, though you would also need the documentation on how to talk to it.

If you really want to you can send the joystick state as serial data and attempt to make a receiver, either internal to the PC as some sort of custom driver or by sending to a receiver unit on a second teensy that is plugged into USB but both of those are going to be major projects (basic blue tooth modules normally can't connect to other basic modules so you'd probably need different wireless hardware)

Option
 
The keyboard/joystick mode for a Teensy is via USB. So if you have a bluetooth device that you plug into as a USB host that'd work. What I suspect you have is a bluetooth serial module, which lacks the commands or anything else to send joystick data. If you can modify or get a device that supports bluetooth joystick commands then a Teensy or any other suitable micro can act as the analog interface element, though you would also need the documentation on how to talk to it.

If you really want to you can send the joystick state as serial data and attempt to make a receiver, either internal to the PC as some sort of custom driver or by sending to a receiver unit on a second teensy that is plugged into USB but both of those are going to be major projects (basic blue tooth modules normally can't connect to other basic modules so you'd probably need different wireless hardware)

Option

Thanks for your quick reply.

Yes, you are right, one option is with 2 teensys (1 connected by usb as a joystick, another one sending data), but i think with keyboard emulation bluetooth could be possible (but without joystick game device).

Something like that

https://www.adafruit.com/product/1535
 
Status
Not open for further replies.
Back
Top