1. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Download Package Now!
  2. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  3. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant here. Do not following these rules will lead to permanent exclusion from this website: Read the forum rules.

Teensy LC/Arduino Scripts

Discussion in 'Electronic and hardware generally' started by Nick Moxley, Jan 29, 2017.

  1. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    I was checking your code...
    Change this..
    int lastButtonState = LOW;
    To this...
    int lastButtonState ;

    Doesn't make a difference but in practice you set LOW or HIGH a bool variable.
    It's just more readable :)
    And i see u used the encoder library.. Just to clarify the library counts all the edges of the signals. So in practice when you make a turn of one click the code will register 4 clicks. If you want to increase or decreasr by one you will have to divide by four
    long newPosition = myEnc.read()/4;
    • Useful Useful x 1
  2. Darius_p

    Darius_p New Member

    Joined:
    Feb 3, 2019
    Messages:
    10
    Balance:
    86Coins
    Ratings:
    +3 / 0 / -0
    My Motion Simulator:
    Arduino
    Thanks a lot for your help. Really without your input i won't be able to resolve it :) You solved another RKJXT1F problem.

    I wanted to ask you about this encoder lib, how to get only one, but you anticipated that :) And yes i saw why it makes sense to use this lib, it pushes back to the right position after unequal turn.
    Can you recommend some lib or algorithm for handling long and short push buttons, i believe its better that the button would react only after it is released? any libs for tracking that properly?
  3. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Glad I could help..
    So you want to modify the code to register the button when it is relesed? Simplest way is with interrupts but they are so fast that button bouncing is registered.. And since you are using an UNO (or nano) you only have two.

    It can be done in software but it's a bit tricky. Post you latest code on github and paste the link and I xan modify it for you
    • Winner Winner x 1
    • Friendly Friendly x 1
  4. Darius_p

    Darius_p New Member

    Joined:
    Feb 3, 2019
    Messages:
    10
    Balance:
    86Coins
    Ratings:
    +3 / 0 / -0
    My Motion Simulator:
    Arduino
    Hi mate, i tried to write a piece of code for it. It catches button release, and randomly catches button presses on button A and B (but not for others), but i cannot put it into the function after release. I can change the state but i cannot direct it inside Switch/case. Do you think it can be issue of debouncing? would it help to add capacitors 100 nF? to make physical debouncing, if so where should i place them? just to replace resistors inside the scheme?
    the code is updated - https://github.com/darjusp/test/blob/master/Button.ino
  5. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    So the code should distinguish between long (more than 1 second) and short (less than 1 second) presses. This code is not written to detect when you release the button. Is rhat correct?
    I don't know how pins A and B catch random pulses? Does this mean it fires even if you don't touch the joystick?
    Hardware debouncing can be used but software should take care of this very nice.
    If you still want to debounce via hardware you can connect 100nF caps between inputs an ground (if you have pullup resistors).

    For this setup you shuldn't need extra resistors but they could be implemented.
    Here is a google image of an example schematic.
    You can try with the values given in the schematic but its best to recalculate them for your timing needs.

    Attached Files:

    • Informative Informative x 1
  6. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Does this code if for example you press buttonA then release it and then press button A again. To me it seems it won't register the second press or did I miss something?:)
  7. Darius_p

    Darius_p New Member

    Joined:
    Feb 3, 2019
    Messages:
    10
    Balance:
    86Coins
    Ratings:
    +3 / 0 / -0
    My Motion Simulator:
    Arduino
    Hey man, thanks it helped. especially to understand that i will need two additional components per button which is a bit too much :) So i will try to do debounce with software. I restructed my code https://github.com/darjusp/test/blob/master/Button.ino
    Sorry i wasn't clear before.
    Now it works properly in the way i want, but i cannot include debounce :
    "if (((millis() - lastDebounceTime) > debounceDelay) && buttonReleased == true) {"
    if i do it skips the switch case altogether until the next cycle and then the value is 0.
    I basically first check whether the button was moved from the middle position (0) if so then it was pushed, and then checking whether it was released (if position moved to middle (0) ), and taking time of it for long presses and engaging into switch case.

    But still do i need to include debouncing and how to do it easier? Don't want to bother too much with the code, but maybe you have some ideas what path i could take?
  8. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    How about if you move "lastButtonState = buttonWhich;"
    Inside the if statement. So the lastButtonstate changes AFTER debouncing.
    Try it.. I's a bit hard for me since i'm only testing the code in my head :)
    • Informative Informative x 1
  9. Darius_p

    Darius_p New Member

    Joined:
    Feb 3, 2019
    Messages:
    10
    Balance:
    86Coins
    Ratings:
    +3 / 0 / -0
    My Motion Simulator:
    Arduino
    I dunno whom you are but you are amazing :) now it works perfectly the way i expected, and short presses, and long presses, and even triggers long press before the button physically is released if time is long enough. Thanks a lot mate for your help
    • Like Like x 2
    • Agree Agree x 1
  10. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Glad I could help :thumbs:cheers
    • Like Like x 1
    • Friendly Friendly x 1
  11. Nick Moxley

    Nick Moxley Well-Known Member SimTools 2.0 Beta Tester

    Joined:
    Dec 13, 2013
    Messages:
    2,746
    Occupation:
    Owner/Operator- Moxleys Rantals
    Location:
    Winnipeg Manitoba Canada
    Balance:
    19,891Coins
    Ratings:
    +2,458 / 29 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK
    Tady, Slaying dragons left right and center.

    you da man cool guy. :cheers
    • Agree Agree x 1
  12. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    146
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    377Coins
    Ratings:
    +92 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    :D:D hey Nick!! How are you? Those scripts working ok?:)
  13. Nick Moxley

    Nick Moxley Well-Known Member SimTools 2.0 Beta Tester

    Joined:
    Dec 13, 2013
    Messages:
    2,746
    Occupation:
    Owner/Operator- Moxleys Rantals
    Location:
    Winnipeg Manitoba Canada
    Balance:
    19,891Coins
    Ratings:
    +2,458 / 29 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK

    All good buddy, thanks for asking eh. Hope all is well with you. :cheers