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:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Yes I made about 3 posts about interrupts:) I was trying to advise you to using interrupts. But you said that you don't want to mess with resistors and capacitors:) so I stopped mentioning them:)
    With interrupts you would chatch every pulse. Regardless of the loop speed :)BUT you need hardware debouncing and not software. That means you would need to add one fapacitor andcone resistor for A and one resistor and one cap for B :) i can show you a schematic on how to do that

    If you don't use debouncing you would most likley get false pulses (if you turn the encoder by one click you could get 2 or 3 or maybe 4 pulses)
    Just say the word and I can make a interrupt driven code:)
    • Friendly Friendly x 1
  2. 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,055Coins
    Ratings:
    +2,505 / 30 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK
    ok lets try some interrupts and get me a diagram for resistors and cap, Sorry lacking the knowledge on what all does what is the only reason i didn't do it the proper way the first time.
  3. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    No problem.. Just ask :) I can give you some lectures on these things:)
    In a few hours I will be home and I will try to rewrite the code for interrupts :)
    • Winner Winner x 1
  4. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Try it... This is a quick mashup.
    I'm cheating a little.. in the interrupt rutine im using a delay of 10ms. This is not quite the way to do it. but it should be for a test.

    Attached Files:

    • Informative Informative x 1
  5. 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,055Coins
    Ratings:
    +2,505 / 30 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK
    That made it worse and not recognize more then half of the rotary clicks. :(

    I have a friend who is a master at this stuff, and agrees with your Debounce idea, I guess we should proceed with that way then. So I would need a little PCB board and some Resistors...what else ?

    On a side note as well, Buddy found this and thought if we stepped up the Clock speed it might see those rotary clicks a little better....what do you think ? I see a bunch more code stuff....errr.:confused:o_O

    https://www.pjrc.com/teensy/prescaler.html
  6. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Wait.. Step up the CPU clock... Teensy LC defaults to 48MHz clock speed. You can only decrease it to 24MHz. That is plenty of power :)
    Look the main problem here is this...
    If you turn the encoder by one click you expect one pulse of the button in the game... So the main thing here is to define the lenght of the pulse...
    If i turn 1 click how long should the pulse be before the button is off again
    This is a bit complicated.. I will write something else and we will test :)
    • Like Like x 1
  7. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    We are forgetting the main question here... We have been looking only at the encoder part of the code but what about the josstick part and how fast can windows detect joystick pulses.
    We are making the code if you turn the encoder one of the inputs goes ON and then OFF.
    I made the code so as if you turn the encoder and the code detect a transition from HIGH to LOW, it then checks the other pulse of the encoder. If it is HIGH you are turning in one direction and if it is LOW you are turning in the other.
    This works.
    If it detects turn in one direction it triggers one of the inputs and then it waits 10ms and deactivates it. So the pulse width is 10ms.
    What I don't know is how fast can eindows detect pulses? Is 10ms enough lenght? Maybe the pulses are to short (for windows).
    Belive me when I say thet runing 48MHz is wayyy enough speed even for pooling mode :)
    • Winner Winner x 1
  8. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    @Nick Moxley
    Sorry for a long response I had some work to do :) Anyway here is version 2.4.. thisone uses Encoder library. With my encoder it works but it some times activates the wrong button ( if i'm turning left i see button 11 blinking and sometimes button 12 blink... and the same if I turn in the opposite direction)
    Anyway this is because my encoder had a detent point exactley at the edge of one of the outputs. So if I turn the encoder in one direction and stop, one of the outputs "jumps around that edge.
    Your encoder however should work because in the datasheed I can see the detent points are in the middle of the pulse ... So please give it a try.
    As for interrupt and pooling mode... Encoder library works with interrupts. But this program is not time consuming. I measured the loop time of the program and it was 40uS. That means that the program when it is running can read the state of an input 25.000 times per second. So here if we use interrupts or pooling it doesnt matter.
    You do need to make hardware debounce. If you don't you will most certanly have strange behaviour!! I will draw you a simple schematic for encoder debouncing!

    Attached Files:

    • Friendly Friendly x 1
  9. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Simple debounce... Resistors are 22K and capacitor is 100nF this gives us 3ms debounce delay... should work. If debounce time is too smal you can try increasing the resistor or capacitor

    Attached Files:

    • Like Like x 2
  10. narbi

    narbi New Member

    Joined:
    Feb 18, 2018
    Messages:
    7
    Location:
    France
    Balance:
    189Coins
    Ratings:
    +3 / 0 / -0
    Hi,
    I just found this project and I think it best suits what I intend to do.
    The main difference is that I will need keystroke outputs from the teensy LC to a computer, say one button will send CTRL + L, another CTRL + P, something like that.
    Could this be adapted easily to this script and can you point me to what I should modify to get it work ?
    Thank you
  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,055Coins
    Ratings:
    +2,505 / 30 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK

    That can be done with Joy2key if im not mistaken.
  12. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Hmmm i don't know if it is possible... Since we use the usb as joystick the firmware would need to switch between joystick and keyboard. I dont know if windows can handle it... We will need to test. Do you want this implemented in 2.6 version
    • Friendly Friendly x 1
  13. narbi

    narbi New Member

    Joined:
    Feb 18, 2018
    Messages:
    7
    Location:
    France
    Balance:
    189Coins
    Ratings:
    +3 / 0 / -0
    I'm really new to this so my apologies on that for maybe upcoming newb questions.
    The purpose is to use this switch as a multimedia car controller to either connect to a computer and remotely command the playing software, or to control an android head unit.
    I will use 2 separate rotary encoders (the same ones as in this project, alps) and 2 separate teensy boards, one for each task.
    The goal for the most important one at least is : turn clockwise = volume up ("CTRL" + "=" ), volume down counter clockwise ("CTRL" + "-"), directions used for fast forward, next, prev... with each its own key command, push for pause (another keystroke). Each rotary tick should be one occurrence of the keystrokes.
    The other one for the android head unit would use standard keys like left right up enter...
    Do you think this can be easily adapted ?
  14. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Ok i will look in to it.. Do you want this function on all scripts?
    • Friendly Friendly x 1
  15. narbi

    narbi New Member

    Joined:
    Feb 18, 2018
    Messages:
    7
    Location:
    France
    Balance:
    189Coins
    Ratings:
    +3 / 0 / -0
    Much appreciated.
    What do you mean by on all scripts ?
    The way I understand it is one script like the one provided here is enough, as I only need to modify in the script the value of the keystroke sent by each button/position.
  16. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    Ooohh is this a seperate project?
    • Funny Funny x 1
  17. narbi

    narbi New Member

    Joined:
    Feb 18, 2018
    Messages:
    7
    Location:
    France
    Balance:
    189Coins
    Ratings:
    +3 / 0 / -0
    It has nothing to do with the OP's original project, I just thought this one was the one closest enough to what I intended to do.
    So yes separate project :)
  18. 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,055Coins
    Ratings:
    +2,505 / 30 / -2
    My Motion Simulator:
    2DOF, 3DOF, DC motor, JRK

    Tady this isn't our PM convo. Lol
    • Like Like x 1
  19. tadythefish

    tadythefish Active Member

    Joined:
    Jul 8, 2009
    Messages:
    148
    Occupation:
    Process automation engineer
    Location:
    Slovenia
    Balance:
    677Coins
    Ratings:
    +95 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino
    I'm so sorry! I tought I was talking to Nick Moxley :) silly me:) ok what kind of computer are you running? Custom windows PC?:))
    • Like Like x 1
  20. narbi

    narbi New Member

    Joined:
    Feb 18, 2018
    Messages:
    7
    Location:
    France
    Balance:
    189Coins
    Ratings:
    +3 / 0 / -0
    No problem ;)
    Classical windows 10 computer, and for the android part there should be no difference, standard HID communication.