Help for joystick/keyboard setup

Status
Not open for further replies.

deelaleo

Well-known member
Hi, I am making a simple controller that uses buttons and switches, so I can use it as game controller for my kids.

I am having a lots of issues with the configuration. I did find an example on instructables to start, since it was using a Teensy (I have a 3.1 version).

So far,I was able to set a switch, although I have few problems:

1) I can't figure out how do you set number of buttons and how to enable/disable the analog input; my controller need more buttons and I need only one analog input, but when I try to pass something like `includeRZAxis = false`; I get an error that class usb_joystick_class has no member named includeRZAxis. Same if I try to set the number of buttons, calling `buttonCount`.

2) When I push a button; the analog values go crazy; even if I am not touching any analog control (they are not even connected to any pin so far); which seems wrong; also when I connect the device; if I look at the joystick config program in windows; I can see that a button is marked as pressed; although its state is not pressed (this goes away if I toggle the button once). Do I need to initialize the buttons to 0 in the setup part of the sketch?

3) is possible to have the Teensy to behave as game pad, but also send keyboard keystroke? Some games does not allow you to customize the buttons, and they work only with specific buttons from the keyboard; so I was wondering if I can achieve this or not.

This is the sketch that I have so far. I have a LED that turn on when a toggle switch is turned on, and goes off when I turn it off. I do simulate one single press, so the toggle switch act more like a momentary button. I am also using a 5x8 matrix to control all the input I want (40 buttons), using only 13 inputs.


Code:
#define NUM_INPUTS 13
//Which pins are used for the button matrix
int buttonList[NUM_INPUTS] = {2,3,4,5,6,7,8,9,10,11,14,15,16};
#define INTENSITY 200


struct GAMESWITCH
{
  // Simple struct to define a game switch. We can set it as toggle, control a LED 
  // and assign a specific keyboard character, when the button is pressed.
  String switch_name;
  int pin1;
  int pin2;
  bool is_pressed;
  bool previous_state;
  bool is_led_on;
  char keyboard_character;
  int button_number;
};

// Create all the switch and buttons needed
struct GAMESWITCH s_power, s_emergencybrake;

void set_gameswitches()
{
  // initialize the buttons for the game controller. This controller has 12 toggle with LED, 9 generic toggle
  // 5 protected toggle, a key toggle, 9 momentary buttons and a 11 positions rotary switch.

  // Main power key toggle
  s_power.switch_name = "Main Power";
  s_power.pin1 = 2;
  s_power.pin2 = 7;
  s_power.is_pressed = false;
  s_power.previous_state = false;
  s_power.is_led_on = false;
  s_power.keyboard_character = 's';
  s_power.button_number = 1;

  // Emergency brake toggle
  s_emergencybrake.switch_name = "Emergency Switch";
  s_emergencybrake.pin1 = 2;
  s_emergencybrake.pin2 = 8;
  s_emergencybrake.is_pressed = false;
  s_emergencybrake.previous_state = false;
  s_emergencybrake.is_led_on = false;
  s_emergencybrake.keyboard_character = 'e';
  s_emergencybrake.button_number = 2;
  
}

void setup() {
  //This makes it so the states are send by us manually
  Joystick.useManualSend(true);
  Joystick.buttonCount = 40;
  // switch off all the analog axis; enable what you need
  Joystick.hatSwitchCount = 0;
  Joystick.includeXAxis = false;
  Joystick.includeYAxis = false;
  Joystick.includeZAxis = false;
  Joystick.includeRXAxis = false;
  Joystick.includeRYAxis = false;
  Joystick.includeRZAxis = false;
  
  
  Serial.begin(9600);
  Serial.println("starting app");
  
  //Declare button pins as input with the internal pullup resistor on
  for (int i = 0; i < NUM_INPUTS; i++)  
  {
    pinMode(buttonList[i], INPUT_PULLUP);
  }
  
  //Declare our LED pins as outputs
  pinMode(17, OUTPUT);  // Red
  pinMode(18, OUTPUT);  // Green
  pinMode(19, OUTPUT);  // Yellow
  pinMode(20, OUTPUT);  // White

  set_gameswitches();
}

void loop() {
  //Analog inputs
  //Joystick.sliderLeft(analogRead(7));
  //Joystick.sliderRight(analogRead(8));


  // Check if the previous state is different from the current
  if (s_power.is_pressed  != s_power.previous_state)
  {
    // State changed, flicker the button while the toggle is on
    Joystick.button(s_power.button_number, 1);
    Joystick.send_now();  //Send control states
    delay(100);  //Slow things down a bit
    Joystick.button(s_power.button_number, 0);
    s_power.previous_state = s_power.is_pressed;
    Joystick.send_now();  //Send control states
    delay(100);  //Slow things down a bit
    Serial.println("Toggle changed state");
    Serial.flush();
  }
    
  //Check status of button and change LED state accordingly
  if (s_power.is_led_on == true) 
    analogWrite(20, INTENSITY);
  else
    analogWrite(20, 0);
  
  //both pin are low; so the toggle is set to on
  if (digitalRead(s_power.pin1) == LOW && digitalRead(s_power.pin2) == LOW)
  {
    Serial.println("button ON");
    Serial.println(s_power.is_pressed);
    Serial.println(s_power.previous_state);
    Serial.flush();
    s_power.is_pressed = true;
    s_power.is_led_on = true;
  }
  // The toggle is off, since the pullup resistor is setting the pins as high
  else
  {
    Serial.println("button OFF");
    Serial.println(s_power.is_pressed);
    Serial.println(s_power.previous_state);
    Serial.flush();
    s_power.is_pressed = false;
    s_power.is_led_on = false;
  }


}
 
Status
Not open for further replies.
Back
Top