Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 18 of 18

Thread: How do I script a macro to toggle hold/release and send commands during hold?

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    11

    How do I script a macro to toggle hold/release and send commands during hold?

    Hi everyone, I'm another new guy here. I'll try to make up for the lack of clarity in the title. I have a teensy 3.2 running as usb keyboard. The project I'm working on involves using this with switches attached to an xbox one controller to perform different macros for each switch via the Titan Two console tuner. More specifically, I want them to help me navigate the quick equip menu in Blakout: (COD4 battle royale mode).
    Two of them seem simple enough, where one switch will tell the controller to press 3 buttons in a series. The one I have no idea where to begin with is more complicated. To repair your armor in the game, you have to press up, which opens the quick equip menu. Then, left 1-5 times ( depending on how many perks you have) until you end up on the armor plates. Then, you hold x for a few seconds to complete one repair. Lastly, press down to close the menu.
    I would like a macro that allows me to hold down one switch (tactile button) that will basically press up, left, left, left,... then hold X until i let go of the button (open switch), and then press down upon release to close the menu.
    So,
    press and hold= up, left, left, left, left.. Hold X
    Release = release x, down

    I don't know any of the language used to write the script and I've tried to find resources with no luck. I guess its a pretty specific application. Any help or nudge in the right direction would be appreciated. If i think of any more useful details later, I'll add them. Currently at work. Don't tell my boss

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    With Teensy and Arduino it would all be written in C/C++. Get the hardware setup to see the buttons and respond in the Serial Monitor with a Serial.print( "Message button#" ); perhaps when each button is triggered. Then extend that to track the time each button is held until release and respond accordingly? Given that the next step would be to work in the sending of the appropriate keyboard output.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Thank you defragster. I'll try to read up on that when i get home. Just curious, since holding down a key would type the same character repeatedly extremely fast, would serial.print(xxxxxxxxxxxxxxxxxx) simulate a held down xbox button after being translated through the Titan2?

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    Good question to try. I assume if a KeyPress was done on button press and left until the button was Released it would repeat at the normal keyboard repeat rate as it would be seen as a held key. There should be a keyboard sample to try that with.

    You'll want to get the buttons working reliably and debounced as needed so any on/off/on transitions as the switch settles don't add confusion - there are notes about that on PJRC.com. Also perhaps important will be 'blink without delay' type examples to keep looping for known times while monitoring the button state - perhaps with an elapsedMillis variable. Given that the key could be repeatedly pressed then releases as long as the button was held at a repeat rate under Teensy control - either faster or slower than provided.

  5. #5
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,138
    Quote Originally Posted by defragster View Post
    ...there are notes about that on PJRC.com...
    https://www.pjrc.com/teensy/td_libs_Bounce.html
    https://www.pjrc.com/teensy/td_keyboard.html

    ...and if you need to handle timing issues....
    https://www.pjrc.com/teensy/td_timin...pedMillis.html

    In case you've not located already.
    Last edited by oddson; 07-03-2019 at 04:35 PM.

  6. #6
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Thank you guys. I spent the 2 days getting the titan 2 software to work so now i can start tinkering. These tips and links will be helpful!

  7. #7
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Can't seem to get the delay working properly. I just copued and pasted the buttons example and changed the falling edge for button zero to println(123). That was too fast for the xim apex to translate, so i added delays of (5), (10), and (15) between each stroke and it didnt seem to make a difference.

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    Quote Originally Posted by Admoore1024 View Post
    Can't seem to get the delay working properly. I just copued and pasted the buttons example and changed the falling edge for button zero to println(123). That was too fast for the xim apex to translate, so i added delays of (5), (10), and (15) between each stroke and it didnt seem to make a difference.
    If the evolved example is short and clear but failing for you - posting it might allow follow up suggestions or testing to see it work.

    I mentioned a few parts that @oddson kindly linked in follow up - properly ordered and assembled should lead to a good solution.

  9. #9
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    I changed the delays to 80,160 and 240 and definitely made a difference. Testing it on the notepad returned perfect results each time, but in-game, it was still buggy. Ill try later to copy and paste the script. It'll probably look really sloppy because i have no clue what I'm doing. Of course, it could be the xim apex that's struggling to interpret. I havent fully set up the titan two yet so maybe I'll have better luck with that

  10. #10
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Code:
    #include <Bounce.h>
    
    // Create Bounce objects for each button.  The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.
    Bounce button0 = Bounce(0, 10);
    Bounce button1 = Bounce(1, 10);  // 10 = 10 ms debounce time
    Bounce button2 = Bounce(2, 10);  // which is appropriate for
    Bounce button3 = Bounce(3, 10);  // most mechanical pushbuttons
    Bounce button4 = Bounce(4, 10);
    Bounce button5 = Bounce(5, 10);  // if a button is too "sensitive"
    Bounce button6 = Bounce(6, 10);  // to rapid touch, you can
    Bounce button7 = Bounce(7, 10);  // increase this time.
    Bounce button8 = Bounce(8, 10);
    Bounce button9 = Bounce(9, 10);
    
    void setup() {
      // Configure the pins for input mode with pullup resistors.
      // The pushbuttons connect from each pin to ground.  When
      // the button is pressed, the pin reads LOW because the button
      // shorts it to ground.  When released, the pin reads HIGH
      // because the pullup resistor connects to +5 volts inside
      // the chip.  LOW for "on", and HIGH for "off" may seem
      // backwards, but using the on-chip pullup resistors is very
      // convenient.  The scheme is called "active low", and it's
      // very commonly used in electronics... so much that the chip
      // has built-in pullup resistors!
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);
      pinMode(6, INPUT_PULLUP);  // Teensy++ LED, may need 1k resistor pullup
      pinMode(7, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);
      pinMode(9, INPUT_PULLUP);
    }
    
    void loop() {
      // Update all the buttons.  There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
      button0.update();
      button1.update();
      button2.update();
      button3.update();
      button4.update();
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
    
      // Check each button for "falling" edge.
      // Type a message on the Keyboard when each button presses
      // Update the Joystick buttons only upon changes.
      // falling = high (not pressed - voltage from pullup resistor)
      //           to low (pressed - button connects pin to ground)
      if (button0.fallingEdge()) {    
        +;Keyboard.println("1");delay(200);Keyboard.println("2");delay(300);Keyboard.println("3")
        ;  }
      if (button1.fallingEdge()) {
        Keyboard.println("B1 press");
      }
      if (button2.fallingEdge()) {
        Keyboard.println("B2 press");
      }
      if (button3.fallingEdge()) {
        Keyboard.println("B3 press");
      }
      if (button4.fallingEdge()) {
        Keyboard.println("B4 press");
      }
      if (button5.fallingEdge()) {
        Keyboard.println("B5 press");
      }
      if (button6.fallingEdge()) {
        Keyboard.println("B6 press");
      }
      if (button7.fallingEdge()) {
        Keyboard.println("B7 press");
      }
      if (button8.fallingEdge()) {
        Keyboard.println("B8 press");
      }
      if (button9.fallingEdge()) {
        Keyboard.println("B9 press");
      }
    
      // Check each button for "rising" edge
      // Type a message on the Keyboard when each button releases.
      // For many types of projects, you only care when the button
      // is pressed and the release isn't needed.
      // rising = low (pressed - button connects pin to ground)
      //          to high (not pressed - voltage from pullup resistor)
      if (button0.risingEdge()) {
        ;
        Keyboard.println("");
    ;  }
      if (button1.risingEdge()) {
        Keyboard.println("B1 release");
      }
      if (button2.risingEdge()) {
        Keyboard.println("B2 release");
      }
      if (button3.risingEdge()) {
        Keyboard.println("B3 release");
      }
      if (button4.risingEdge()) {
        Keyboard.println("B4 release");
      }
      if (button5.risingEdge()) {
        Keyboard.println("B5 release");
      }
      if (button6.risingEdge()) {
        Keyboard.println("B6 release");
      }
      if (button7.risingEdge()) {
        Keyboard.println("B7 release");
      }
      if (button8.risingEdge()) {
        Keyboard.println("B8 release");
      }
      if (button9.risingEdge()) {
        Keyboard.println("B9 release");
      }
    }
    Last edited by defragster; 07-05-2019 at 11:22 PM. Reason: added # CODE markup

  11. #11
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    button 0 falling edge and rising edge are what I edited

  12. #12
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    This, by the way, is just a simple 3 button macro I'm trying to create. Not the more complicated hold x macro I was asking about before

  13. #13
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    Added CODE or tool:# above.

    This is odd:
    Code:
      if (button0.fallingEdge()) {    
        +;Keyboard.println("1");delay(200);Keyboard.println("2");delay(300);Keyboard.println("3")
        ;  }
    Not sure if the '+' makes sense ?

    Is "Keyboard.println" the right way to send keys? Not familiar with the usage …

    The sample would be easier to parse without the many extra buttons … that is get one working first.

  14. #14
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    I don't know if that's the right way or not. I just copied and pasted an "buttons" example and made adjustments to one button for testing. That "+" must have been a typo. I don't even know where to begin. Ive3tried to study different scripts people have posted and they all have what look like some sort of introductory parameters and they're all different and for specific uses, so I'm completely overwhelmed at the moment

  15. #15
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    Some confusion on my part - I just followed the keyboard link and see a sample like this:
    Code:
    void loop() {
      // Update all the button objects.
      button1.update();
      button2.update();
      button3.update();
    
      // Check each button for "falling" edge.
      // falling = high (not pressed - voltage from pullup resistor)
      //           to low (pressed - button connects pin to ground)
      if (button1.fallingEdge()) {
        Keyboard.println("B1 press");
      }
      if (button2.fallingEdge()) {
        Keyboard.println("B2 press");
      }
      if (button3.fallingEdge()) {
        Keyboard.println("B3 press");
      }
    That is a less cluttered example - but getting that working with just recognizing the button press would be a confidence building first step. Once that is resolved and working - only then would it be good to modify the code to try repeating keys.

  16. #16
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Ok. I tried to paste that but it said 'button1' was not declared

  17. #17
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,022
    Quote Originally Posted by Admoore1024 View Post
    Ok. I tried to paste that but it said 'button1' was not declared
    Yeah - that is an incomplete sample from the web page - no setup() or includes - just loop().

    For practice save your current sketch with a new name - delete the loop and put that in place then remove stuff from that setup() that isn't needed and then remove all the buttons above but those three?

    Make sure the buttons point to the right pins.

    Going through that will give a shorter example that might make more sense going forward with a chance to see what is going on in the sketch?

  18. #18
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    Okay. I'm gonna get some sleep and work on it later. I'm pretty drained now. I appreciate all the help though so far

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •