Teensy 3.1 Phone game-case coding tweaks.

Status
Not open for further replies.

Thetincat

New member
Hello, I made this slide out game pad for my phone using the Teensy 3.1 board. I used the "joystick complete" program below and it works quite well, I just have a few coding questions:

1. How do you increase the sensitivity of the analog inputs? The sticks are being read, but they only have a small range, and I would like to increase that. What code can I use to achieve that?

2. What code would I use to invert the analog sticks? ( so up would be down etc.)

3. How would I re-map which button presses are triggered by each input? for example how would I make the button attached to digital input 4 trigger button 8.

sorry if these are simple/dumb questions. I am a complete code newbie. Thanks!

here is the code I'm using, it is one that came with Teensy:

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 = 13;  // 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(2));
 Joystick.Z(analogRead(1));
 Joystick.Zrotate(analogRead(3));

 
 // 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]);
 }

 
 // 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);
}
 

Attachments

  • WIN_20150125_121024.jpg
    WIN_20150125_121024.jpg
    80.9 KB · Views: 884
  • WIN_20150125_121008.jpg
    WIN_20150125_121008.jpg
    91.1 KB · Views: 646
Status
Not open for further replies.
Back
Top