Home Forums PS2X PlayStation 2 Controller Library Support Forum Recognizing simultaneous key presses PS2X library

This topic contains 2 replies, has 3 voices, and was last updated by  Gary_G 4 years, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #3151

    nnenna
    Member

    Hi,

    I am new to arduino and working on a project to use a dance mat to control a pc game.

    At the moment, I have connected the dance mat to arduino Uno and it works fine but only for individual key press. But I need the dance mat to recognize simultaneous key presses. For example, if I stepped on the right button and then Fire button, I want to be able to move right and fire at the same time. then if I stopped stepping on the fire button to continue moving right. here is a sample code I have. it doesn’t work as i want it at the moment:

    #include <PS2X_lib.h>  //for v1.6

     

    PS2X ps2x; // create PS2 Controller Class for the first dancemat

    PS2X ps2x2; // create PS2 Controller Class for the second dancemat

    uint8_t keyNone[8] = { 0, 0, 0, 0, 0, 0, 0 };

    uint8_t keyC[8] = { 0, 0, 6, 0, 0, 0, 0 };

    uint8_t keyZ[8] = { 0, 0, 29, 0, 0, 0, 0 };

    uint8_t keyX[8] = { 0, 0, 27, 0, 0, 0, 0 };

    uint8_t keyB[8] = { 0, 0, 5, 0, 0, 0, 0 };

    uint8_t keyM[8] = { 0, 0, 16, 0, 0, 0, 0 };

    uint8_t keyN[8] = { 0, 0, 17, 0, 0, 0, 0 };

     

    void setup(){

    Serial.begin(9600);

    ps2x.config_gamepad(13, 11, 10, 12, true, true);   // setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?)

    delay(3000);

    }

     

    void loop(){

    // We read entered value for the first dancemat

    ps2x.read_gamepad();

    if(ps2x.Button(PSB_PAD_UP)) // if this value matches with top button;

    {

    Serial.write(keyX, 8);

    delay(100);

    }

    else if(ps2x.ButtonReleased(PSB_PAD_UP))

    {

    Serial.write(keyNone, 8);

    delay(100);

    }

    if(ps2x.Button(PSB_PAD_LEFT)) // if this value matches with left button

    {

    Serial.write(keyZ, 8);

    delay(100);

    }

    else if(ps2x.ButtonReleased(PSB_PAD_LEFT))

    {

    Serial.write(keyNone, 8);

    delay(100);

    }

    if(ps2x.Button(PSB_PAD_RIGHT)) // if this value matches with right button

    {

    Serial.write(keyC, 8);

    delay(100);

    }

    else if(ps2x.ButtonReleased(PSB_PAD_RIGHT))

    {

    Serial.write(keyNone, 8);

    delay(100);

    }

    }

     

     

    • This topic was modified 5 years, 2 months ago by  nnenna.
    #3157

    Bill
    Member

    ps2x.ButtonDataByte() will return 2 bytes representing all the current buttons pressed.

    You could use that to test multiple button presses at once.

     

    Here is the bits in that word and what buttons they represent:

     

    //These are our button constants
    #define PSB_SELECT 0x0001
    #define PSB_L3 0x0002
    #define PSB_R3 0x0004
    #define PSB_START 0x0008
    #define PSB_PAD_UP 0x0010
    #define PSB_PAD_RIGHT 0x0020
    #define PSB_PAD_DOWN 0x0040
    #define PSB_PAD_LEFT 0x0080
    #define PSB_L2 0x0100
    #define PSB_R2 0x0200
    #define PSB_L1 0x0400
    #define PSB_R1 0x0800
    #define PSB_GREEN 0x1000
    #define PSB_RED 0x2000
    #define PSB_BLUE 0x4000
    #define PSB_PINK 0x8000
    #define PSB_TRIANGLE 0x1000
    #define PSB_CIRCLE 0x2000
    #define PSB_CROSS 0x4000
    #define PSB_SQUARE 0x8000
    #3293

    Gary_G
    Member

    Hi Bill,

    I am trying to use this function for a few dual button combos and trying to get a clean compile.

    For instance I want to test for this combination:  PSB_L1  and the PSB_START buttons

    These do not seem to work… the first one compiles, the other two do not compile

    if(ps2x.ButtonDataByte(PSB_L1 && PSB_START))

    if(ps2x.ButtonDataByte(0x0408))

    if(ps2x.ButtonDataByte(0408))

     

    In the example program it seems like the button names are bring passed through the function call. I was surprised that did not see to work  for me. Did I miss something or am I just doing it wrong ?

    • This reply was modified 4 years, 3 months ago by  Gary_G.
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.