1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. 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 Now a Download Plan!
  3. 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
  4. 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. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company 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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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:
    194Coins
    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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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:
    194Coins
    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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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:
    194Coins
    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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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:
    194Coins
    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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Glad I could help :thumbs:cheers
    • Like Like x 1
    • Winner Winner x 1
    • Friendly Friendly x 1
  11. Nick Moxley

    Nick Moxley Well-Known Member

    Joined:
    Dec 13, 2013
    Messages:
    2,779
    Occupation:
    Owner/Operator- Moxleys Rantals
    Location:
    Winnipeg Manitoba Canada
    Balance:
    17,054Coins
    Ratings:
    +2,504 / 30 / -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:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 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

    Joined:
    Dec 13, 2013
    Messages:
    2,779
    Occupation:
    Owner/Operator- Moxleys Rantals
    Location:
    Winnipeg Manitoba Canada
    Balance:
    17,054Coins
    Ratings:
    +2,504 / 30 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK

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

    kaiser turtle New Member

    Joined:
    Dec 17, 2019
    Messages:
    3
    Balance:
    30Coins
    Ratings:
    +0 / 0 / -0
    hi! one question:
    is your code supposed to work with pull up or pull down resistors?
  15. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Huh. Honetly I can't remember.. But probably pullup in most cases encoders and buttons give you a low signal.. So in those cases you need pullup resistors.
  16. kaiser turtle

    kaiser turtle New Member

    Joined:
    Dec 17, 2019
    Messages:
    3
    Balance:
    30Coins
    Ratings:
    +0 / 0 / -0
    Thank you!
    I was asking because the scheme in darius github (where he also uploaded your sketch) is with pulldow resistors.. so I'm a bit confused..
    I haven't yet ordered this 7 way encoder/switch and also wanted to design and order a pcb for a buttonbox without testing this encoder first.. so I guess I'll have to test it on a breadbord first..
  17. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    676Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Share a link so I can take a look and maybe I can be more helpfull
    • Friendly Friendly x 1
  18. kaiser turtle

    kaiser turtle New Member

    Joined:
    Dec 17, 2019
    Messages:
    3
    Balance:
    30Coins
    Ratings:
    +0 / 0 / -0
  19. Lexins

    Lexins New Member

    Joined:
    Oct 20, 2021
    Messages:
    1
    Balance:
    17Coins
    Ratings:
    +0 / 0 / -0
    My Motion Simulator:
    AC motor, Arduino
    I am using a funky switch (RKJXT1F) in Arduino Pro Micro with matrix connection, what code should I use to make all 5 buttons work?
  20. wittycoder

    wittycoder New Member

    Joined:
    Nov 11, 2021
    Messages:
    9
    Occupation:
    Software Engineering Manager
    Location:
    Phoenix, AZ
    Balance:
    87Coins
    Ratings:
    +2 / 0 / -0
    My Motion Simulator:
    AC motor, 4DOF
    The main issue with that switch is that every press will be a common button press along with the direction that is pushed. You have to manually compensate for that or have software/simulator that can handle 2 buttons at the same time and differentiate. Here is a discussion on it (there is a github link to some test code as well)

    https://forum.arduino.cc/t/push-buttons-issues-on-alps-rkjxt1f42001/635069/16