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

Thread: Teensy LC button Box and Encoder wiring

  1. #1
    Junior Member
    Join Date
    Jan 2017
    Posts
    11

    Teensy LC button Box and Encoder wiring

    Forgive me for being a complete rookie with Code and libraries and everything in general,

    I have a Teensy LC that I want to hook up basic NO switches and some Standard Encoder's and some higher end Alps Encoders.

    Im stuck at the basics tho, how do i load the proper or Which is the proper script to run for what i want to do ?

    Which USB type do i want ?

    Here is the Alps Encoder i want to use as well as some standard 3 pin encoders,

    http://www.alps.com/prod/info/E/PDF/...1F/RKJXT1F.PDF

    Thanks

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    Normally you should use the Bounce library to read pushbuttons, and the Encoder library for the encoder.

    Which USB Type to use depends on how you want Teensy to interact with your computer. If you're unsure, just leave it on "Serial". All modes like you use Serial.print() to send messages to the Arduino Serial Monitor, but it's faster in Serial mode.

  3. #3
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by PaulStoffregen View Post
    Normally you should use the Bounce library to read pushbuttons, and the Encoder library for the encoder.

    Which USB Type to use depends on how you want Teensy to interact with your computer. If you're unsure, just leave it on "Serial". All modes like you use Serial.print() to send messages to the Arduino Serial Monitor, but it's faster in Serial mode.


    Ok so I load up the Bounce library and the teensy reboots as you would think, but now how do i go about checking if said buttons are working ? The LED is solid on the teensy and goes out when i push a button. But where can i visually check said buttons like in the "game controllers"

    Also how do i add the encoder library to the bounce library, Sorry for rookie questions.

    Thanks Paul

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    Hi Nick,

    There is probably no default script (Arduino Sketch), you can run. You will need to program the Teensy LC with a sketch that does what it is you wish to do.

    There are lots of people up here who try to help as they can, but before they can do so, you need to more completely outline what it is you are using and what exactly you wish for it to do.
    Also you will find that members help more when they feel like the person who is asking for help is actively involved trying to solve it.

    For example how do you have the button hooked up? What code did you try? Did you try the bounce library example programs? If you look at the File menu, you will see a menu item that says examples, which when you choose that it shows you a list of libraries which have example programs. This incudes the Bounce library. You can then load one of those... But those programs may or may not work, as they may make different assumptions. You can find out more about their assumptions on the Arduino site: https://www.arduino.cc/en/Tutorial/Button

    But I typically don't use their hook up for buttons, but instead hook up with button to pin and other side to GND and change the program to use pinMode(BUTTON, INPUT_PULLUP) which enables a built-in pull up resistor on the pin and with such a hookup the VALUE of the button will be different (LOW) when button is pressed and HIGH when not pressed...

    So again it would help others help you if you better described what you would like to do.
    Things like:

    I would like to hook up 4 buttons, that when I press them they do BLAH.
    I also have 2 encoders (preferably with link to which one) that are hooked up to BLAH that I wish to count...

    Here is a picture showing my current setup and here is a listing showing what I have tried.

    Also another suggestion is to take baby steps here. That is you should break down what it is you would like to do into a set of smaller tasks that you can try solving one by
    one. For example a simple sketch that handles your buttons. Once you are happy with that, try a simple sketch that tries to get the inputs from your encoders

    or ??? as we don't know your goals.

    Hope that helps

  5. #5
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Hey Kurt, Appreciate the help,

    Basically i will have a multitude of Standard N/O buttons that i want to act as a button of sorts, be it a joystick button or what have you,

    I also have an Alps Encoder that I linked in the first post that I have wired up and got everything working as i would expect on the multimeter but again cant get a basic button to work on the teensyLC, so haven't hooked up the Alps yet.

    The switch i have on for testing has 1 lug on the G and the other on pin 5 or 7 or 8 ive tried a few different pins....I think this is as baby step as i can take it hehe.

    Ive loaded soo many scripts from so many places i honestly dont know what ive all tested. Sorry to be so vague, The pinMode seems to be what im after.

    I loaded the bounce scrips you said and I got the LED to flash when i press the button....the Led also goes out when i Touch both contacts with my fingers..dunno why that is but ok..... So whats next ?

    here's what im seeing

    Click image for larger version. 

Name:	Bounce.png 
Views:	730 
Size:	117.7 KB 
ID:	9534

    Click image for larger version. 

Name:	bounce1.jpg 
Views:	371 
Size:	96.2 KB 
ID:	9535

  6. #6
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    The Other Encoder's i plan to use are the CST 288

    http://www.leobodnar.com/shop/index....roducts_id=191

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    Ok so you have one pin on 5 or 7 or 8 Other lug on ground.

    So if you change the Bounce progam to be:

    Code:
    #include <Bounce.h>
    
    // This code turns a led on/off through a debounced button
    // Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
    
    #define BUTTON 5   // First try pin 5
    #define LED 13
    
    // Instantiate a Bounce object with a 5 millisecond debounce time
    Bounce bouncer = Bounce( BUTTON,5 ); 
    
    void setup() {
      pinMode(BUTTON,INPUT_PULLUP);
      pinMode(LED,OUTPUT);
    }
    
    void loop() {
     // Update the debouncer
      bouncer.update ( );
     
     // Get the update value
     int value = bouncer.read();
     
     // Turn on or off the LED
     if ( value == LOW) {
       digitalWrite(LED, HIGH );
     } else {
        digitalWrite(LED, LOW );
     }
     
    }
    When you run this program is the LED off? If you push the button connected to pin 5 does the LED on pin 13 light up?

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    Also try File > Examples > Encoder > Basic. If necessary, change the pin numbers to whatever pins you connect the encoder signals. Check the back side of the pinout card that came with your Teensy LC, and use only pins with the interrupt capability.

    If you haven't used the Arduino Serial Monitor yet, now's the time to try it. After Teensy is running your program, use Tools > Port to select the serial device. Then click the serial monitor button. As your encoder changes, you should see numbers print to tell you the position.

  9. #9
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by KurtE View Post
    Ok so you have one pin on 5 or 7 or 8 Other lug on ground.

    So if you change the Bounce progam to be:

    Code:
    #include <Bounce.h>
    
    // This code turns a led on/off through a debounced button
    // Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
    
    #define BUTTON 5   // First try pin 5
    #define LED 13
    
    // Instantiate a Bounce object with a 5 millisecond debounce time
    Bounce bouncer = Bounce( BUTTON,5 ); 
    
    void setup() {
      pinMode(BUTTON,INPUT_PULLUP);
      pinMode(LED,OUTPUT);
    }
    
    void loop() {
     // Update the debouncer
      bouncer.update ( );
     
     // Get the update value
     int value = bouncer.read();
     
     // Turn on or off the LED
     if ( value == LOW) {
       digitalWrite(LED, HIGH );
     } else {
        digitalWrite(LED, LOW );
     }
     
    }
    When you run this program is the LED off? If you push the button connected to pin 5 does the LED on pin 13 light up?


    The LED is Lit from the second i plug in the Teensy, If i press the button the light goes out. No recognition in the game Controllers.

  10. #10
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Quote Originally Posted by PaulStoffregen View Post
    Also try File > Examples > Encoder > Basic. If necessary, change the pin numbers to whatever pins you connect the encoder signals. Check the back side of the pinout card that came with your Teensy LC, and use only pins with the interrupt capability.

    If you haven't used the Arduino Serial Monitor yet, now's the time to try it. After Teensy is running your program, use Tools > Port to select the serial device. Then click the serial monitor button. As your encoder changes, you should see numbers print to tell you the position.


    Hey Paul, Yes i seen the specific pins to put the encoder on, and or what pins Not to use. But again I currently have 1 button hooked up and cant seem to get it to act as a button in the game controllers, Its seen as a Serial+Keyboard+Mouse+Joystick

  11. #11
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    Again so far you simply have a program that it simply turns the LED on and off... In the updated program I had, if you now want it to be off when you plug it on and ON when you press the button, change the code above form
    Code:
    if (value == LOW)
    to
    Code:
    if (value == HIGH
    )
    Or Alternatively
    Code:
    if (value != LOW)
    Now if you wish for it to act like a button on a Joystick.

    You might try the example program Buttons, which you can find by first configure Tools->USB to be one of the settings which includes Joystick.

    Then go to File->Examples->Teensy->USB_Joystick->Buttons

    Looks like this program is setup to create 10 buttons on digital pins 0-9...

  12. #12
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Success, I got a Registering button, YAY...

    ok so You say the example button script is set for 10 button's which i can see, from 0 to 9....my Rotary if you looked has 10 wires, 3 of which are grounds. Can i gang those grounds together ? Also Each one of the Rotarty/hatswitch functions will be a "button" including the rotary part right ? How many Buttons plus the Alps Rotary can i get on the Teensy.

    Also how do i go about adding the encoder library or sketch to this ?

  13. #13
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    Quote Originally Posted by Nick Moxley View Post
    Also how do i go about adding the encoder library or sketch to this ?
    The general idea is you copy and paste chunks of code from examples into your program, which usually starts from one of the examples. So if you're using the Buttons example, save it to a new filename. Then start copying piece of the Encoder Basic example.

    Arduino programs usually have 3 main parts, the global variables & objects, then a setup() function which runs once, and then a loop() function which runs over and over again. Usually the general design is to have the loop() check everything that needs to be done, and do each thing as quickly as possible whenever it needs to happen. The idea is to avoid delays, except in the simplest programs, so you can add more stuff into loop to make your program do more. As long as nothing sits there and delays (preventing other things from running as soon as possible), all the stuff gets checked rapidly and works together.

    There's a certain amount of experimentation and self reliance required. We can give you ideas, and we can help you here when you get stuck (follow the "Forum Rule" by posting the complete program), but the possibilities are so great, so numerous, that nobody can give you the exact answers. Even if stuff about the code is mysterious and unknown, you have to take some initiative and chances combining stuff together and trying it. Often things will not work as expected. Usually you can collect quite a lot of info about what your program is doing by adding Serial.print() lines in certain places, and then you view with the Arduino Serial Monitor. When things do go wrong, and you've added Serial.print() and you have some idea but you just can't resolve the problem, ask for help here (again, Forum Rule) and usually someone can comment with suggestions about how to fix your code, or other ways to try.

  14. #14
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    If you're feeling like none of this stuff makes much sense, try doing these tutorials. They'll walk you through the basics.

    https://www.pjrc.com/teensy/tutorial2.html

  15. #15
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    I also second everything Paul mentioned!

    As for your Alps encoder, I took a quick look and it looks like there are 6 wires that are setup to more or less do 5 buttons, like mini-joystick, Left, Right, UP, Down, and PUSH). These can probably be wired up with the common going to GND, and the others wired up like your buttons...

    You should experiment around with the buttons sketch and try to add in those, like your other buttons, and as Paul Mentioned, save this away as your own sketch.

    As for how many buttons you can have. Try looking at the documentation about USB joystick up on PJRC website: https://www.pjrc.com/teensy/td_joystick.html

    It then looks like there are three wires that hook up to do an encoder. Again I suggest you play with one of your encoders stand alone. That is first try to understand how to hook it up and use without trying to integrate it yet into your main code. Take a look at the Encoder documentation up on PJRC: https://www.pjrc.com/teensy/td_libs_Encoder.html

    Once you have it working with simple example code, then you need to figure out how you wish to use it. That is with the USB joystick you have two main things, Buttons and Axis (Axis, Slider, maybe hat), So you somehow then need to figure out how to maybe use an encoder as one of the Axis. The Axis can have values with 0-1023, where 512 is center point (for joystick like). So you need to make sure with your encoder that you keep the stuff in a proper range value and then update the appropriate Axis...

  16. #16
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Appreciate all the advice so far guys, Unfortunately Im still not having much luck adding the Encoder Script to the Buttons script. I lack the knowledge of where and how to even input it....I see so many { and } ))(( ect ect ect and just randomly picking places to add the script isn't getting me anywhere.


    Paul, I sort of get the basics in the 3 parts with the setup and the loop, but all the numbers are rather confusing, sorry. As Kurt has found out, my Alps encoder which ive played with already on my Multi meter and have all the function's mapped out on paper to the colored wire i have soldered on it currently and it all works as i would expect, The Only question i still have is Can I gang ALL the N/O switch grounds together in a daisy chain as well as the Encoder common ground, all to SINGLE G/ground pin on the Teensy

    Ideally im thinking i need the following 2 codes together Or maybe im looking for the "TwoKnobs" encoder script ?....Again im not sure. Also to access more buttons would it be better to use the USB joystick example versus the "buttons" ?





    /* Buttons to USB Joystick Example

    You must select Joystick from the "Tools > USB Type" menu

    This example code is in the public domain.
    */

    #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);
    Bounce button6 = Bounce(6, 10);
    Bounce button7 = Bounce(7, 10);
    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);

    // Please be aware the X, Y, Z, Zr and Slider axes will have default
    // settings, if you only use the buttons. This can give the appearance
    // of the buttons interfering with the axes, if your PC software shows
    // different default assumed values before your first button press.
    // More details here:
    // https://forum.pjrc.com/threads/29320...0275#post80275
    }

    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.
    // 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()) {
    Joystick.button(1, 1);
    }
    if (button1.fallingEdge()) {
    Joystick.button(2, 1);
    }
    if (button2.fallingEdge()) {
    Joystick.button(3, 1);
    }
    if (button3.fallingEdge()) {
    Joystick.button(4, 1);
    }
    if (button4.fallingEdge()) {
    Joystick.button(5, 1);
    }
    if (button5.fallingEdge()) {
    Joystick.button(6, 1);
    }
    if (button6.fallingEdge()) {
    Joystick.button(7, 1);
    }
    if (button7.fallingEdge()) {
    Joystick.button(8, 1);
    }
    if (button8.fallingEdge()) {
    Joystick.button(9, 1);
    }
    if (button9.fallingEdge()) {
    Joystick.button(10, 1);
    }

    // Check each button for "rising" edge
    // Update the Joystick buttons only upon changes.
    // rising = low (pressed - button connects pin to ground)
    // to high (not pressed - voltage from pullup resistor)
    if (button0.risingEdge()) {
    Joystick.button(1, 0);
    }
    if (button1.risingEdge()) {
    Joystick.button(2, 0);
    }
    if (button2.risingEdge()) {
    Joystick.button(3, 0);
    }
    if (button3.risingEdge()) {
    Joystick.button(4, 0);
    }
    if (button4.risingEdge()) {
    Joystick.button(5, 0);
    }
    if (button5.risingEdge()) {
    Joystick.button(6, 0);
    }
    if (button6.risingEdge()) {
    Joystick.button(7, 0);
    }
    if (button7.risingEdge()) {
    Joystick.button(8, 0);
    }
    if (button8.risingEdge()) {
    Joystick.button(9, 0);
    }
    if (button9.risingEdge()) {
    Joystick.button(10, 0);

    }
    }







    /* Encoder Library - Basic Example
    * http://www.pjrc.com/teensy/td_libs_Encoder.html
    *
    * This example code is in the public domain.
    */

    #include <Encoder.h>

    // Change these two numbers to the pins connected to your encoder.
    // Best Performance: both pins have interrupt capability
    // Good Performance: only the first pin has interrupt capability
    // Low Performance: neither pin has interrupt capability
    Encoder myEnc(5, 6);
    // avoid using pins with LEDs attached

    void setup() {
    Serial.begin(9600);
    Serial.println("Basic Encoder Test:");
    }

    long oldPosition = -999;

    void loop() {
    long newPosition = myEnc.read();
    if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    }
    }

  17. #17
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    You should be able to connect all of the button grounds to one ground pin on the Teensy. As for connecting the encoder, I would first get a working sketch before I hard wired it...

    Again it is hard to give you much advice on how to put these pieces with each other. That is it would help to have you sort of map out what each IO pins connects to. For example in the two sketches it look like you have IO pins 5 and 6 both as buttons and connected to the encoder.

    I should note, I am not doing here what I would normally do in my own code. I am resisting from using more advanced things like arrays, and functions with parameters....

    But for now lets assume your encoder is on pins 5 and 6 as that is what the line: Encoder myEnc(5, 6);
    Is specifying in the second sketch.

    So the simplest way to combine these two sketches is to remove all of the code in the first sketch that uses pins 5 and 6. Actually I would remove all of the stuff for any pins you are not using. That is if you only have buttons on 4 pins maybe remove the other pins being defined as buttons...

    But here is a quick and dirty combine of the two sketches. I also maybe put it such that the encoder may drive the left slider value... Again it compiles but have not tried...
    Code:
    // Hacked up combined of two example sketches.  With pins 5 and 6
    // not being used as buttons but instead encoder. 
    #include <Bounce.h>
    #include <Encoder.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 button7 = Bounce(7, 10);
    Bounce button8 = Bounce(8, 10);
    Bounce button9 = Bounce(9, 10);
    Encoder myEnc(5, 6);
    void setup() {
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(7, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);
      pinMode(9, INPUT_PULLUP);
    
      myEnc.write(512); // try to init in middle 
    
    }
    
    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();
      button7.update();
      button8.update();
      button9.update();
    
      // Check each button for "falling" edge.
      // 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()) {
        Joystick.button(1, 1);
      }
      if (button1.fallingEdge()) {
        Joystick.button(2, 1);
      }
      if (button2.fallingEdge()) {
        Joystick.button(3, 1);
      }
      if (button3.fallingEdge()) {
        Joystick.button(4, 1);
      }
      if (button4.fallingEdge()) {
        Joystick.button(5, 1);
      }
      if (button7.fallingEdge()) {
        Joystick.button(8, 1);
      }
      if (button8.fallingEdge()) {
        Joystick.button(9, 1);
      }
      if (button9.fallingEdge()) {
        Joystick.button(10, 1);
      }
    
      // Check each button for "rising" edge
      // Update the Joystick buttons only upon changes.
      // rising = low (pressed - button connects pin to ground)
      // to high (not pressed - voltage from pullup resistor)
      if (button0.risingEdge()) {
        Joystick.button(1, 0);
      }
      if (button1.risingEdge()) {
        Joystick.button(2, 0);
      }
      if (button2.risingEdge()) {
        Joystick.button(3, 0);
      }
      if (button3.risingEdge()) {
        Joystick.button(4, 0);
      }
      if (button4.risingEdge()) {
        Joystick.button(5, 0);
      }
      if (button7.risingEdge()) {
        Joystick.button(8, 0);
      }
      if (button8.risingEdge()) {
        Joystick.button(9, 0);
      }
      if (button9.risingEdge()) {
        Joystick.button(10, 0);
      }
    
      // Play around with Encoder?  
      long enc = myEnc.read();  // get current value
      // make sure it is in the bounds for joystick
      if (enc < 0) {
        enc = 0;
        myEnc.write(0);
      } else if (enc > 1023) {
        enc = 1023;
        myEnc.write(1023);
      }
      Joystick.sliderLeft(enc);
      
    }
    But again this may not match your hardware.

  18. #18
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Awesome!!! That worked as expected Kurt, the Encoder on pin 5/6 adjusted the X/Y Axis slider bar. So now question if I want the encoder to act as button pushes as well so how exactly do i code that ?

    And why do i want to disable pins exactly ? Is that to save in the loop calculation ? ?

    Im most likely going to want to have as many buttons as i can, I see the Joystick offer's 32 buttons, Is that 32 buttons factoring in what im using for the encoder already ? so that makes it 30 buttons ?

    How do i get 32 buttons from 23 digital pins ?

    Again Im terribly sorry for the rookie questions And if you want more info just ask.


    here's how i have the Alps wired, the white/grey, Yellow, Green and brown are the Up, down, left and right I have the Com attached to the Ground terminal and u can see i have the encoder Com on its own ground for now. Problem is when i add all these functions from the Alps, it seems to start to do "double presses" of buttons And im not too sure why.





  19. #19
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,570
    Quick answers:
    Why remove other pins/button code - Don't have to, it is mainly trying to say is maybe you should start to setup in code and/or on paper what all you want to connect to which pins. If you are going to have lots more buttons, then leave in... If you are going to want 30 buttons, then maybe you should start looking into things like arrays and stuff as the copy/paste duplication gets somewhat messy looking. But more on that at some other time.

    Logical Buttons on the encoder. If you have wiring that you can setup to the encoder switches that when you move the joystick in those directions or push, it routes one pin to ground, then you can hook those wires up like any of the other switches you have.

    How to get more buttons than IO pins? there are several ways.
    a) Use an external MUX to convert maybe 5 IO pins into something like 16 signals. Example: https://www.sparkfun.com/products/9056

    b) You could hook a few of them up to one analog pin using a resistor ladder (https://en.wikipedia.org/wiki/Resistor_ladder)

    c) You can hook up several of them into a matrix of pins like a keypad matrix. Example of actual matrix https://www.adafruit.com/products/1824
    You break up the buttons into logical rows and columns where all of the buttons of one row hook up to each other on one pin and likewise the other pin of each switch connects to all of the buttons in the same column. So for example you can connect: 16 buttons using 4 rows and 4 columns using 8 pins. More details in the encoder library: http://playground.arduino.cc/Main/KeypadTutorial

    Hope that gives you a few places to get some ideas.

    Kurt

  20. #20
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    Hello,


    Im looking for a hand to get a CTS-288 Encoder working properly.

    My code guy says he cant find "the timing diagram" and the end result is the Encoders missing clicks and in general not working like how i know them to with more commercial grade boards.

    here's the code and here's a link to the Encoders,

    https://www.digikey.ca/products/en?k...=288T232R161A2

    PS there is the original "alps funky switch" also installed on this, so there's a reason for the multiple encoder stuff.




    // Version 2.1 - Fixed PUSH IN problem when pushing the button forward, reverse, left or right
    // Version 2.2 - Fixed Encoder Debounce time from 1ms to 5ms (datasheet of the encoder states that maximum debounce time is 4ms)
    // Version 2.3 - Test with interrupts
    // Version 2.4 - Test with encoder library
    // Version 2.5 - Added two more encoders

    #include <Bounce.h>
    #include <Encoder.h>
    //#include <avr/io.h>
    //#include <avr/interrupt.h>

    //////////////////////////////////////
    // Added by TadyTheFish

    // This was added so if you want to change a pin that a button is connected to, you do it here and don't have to search the code to make changes
    int PinButton0 = 2; // Button0 is connected to pin 2 on Teensy (it is Mandatory to connect the PUSH IN button to this pin or it will not work, unless you change the bottom of the program)
    int PinButton1 = 0; // Use this and the next three pins for direction buttons (forward,backward,left and right)
    int PinButton2 = 1;
    int PinButton3 = 3;
    int PinButton4 = 4;
    int PinButton7 = 7; // Use this one as you like
    int PinButton8 = 8; // Use this one as you like
    int PinButton9 = 9; // Use this one as you like
    int PinButton12 = 12;
    int PinButton16 = 16;
    int PinButton17 = 17;
    int PinButton18 = 18;
    int PinButton19 = 19;
    int PinButton20 = 20;
    int PinButton21 = 21;
    int PinButton22 = 22;
    int PinButton23 = 23;

    int PinButton5 = 5; // 5 These are encoder inputs
    int PinButton6 = 6; // 6
    int PinButton10 = 10;
    int PinButton11 = 11;
    int PinButton14 = 14;
    int PinButton15 = 15;


    //Current connection:
    /* (Windows) (teensy pin)
    * Button1 = pin 0
    * Button2 = pin 1
    * Button3 = pin 2
    * Button4 = pin 3
    * Button5 = pin 4
    * Button8 = pin 7
    * Button9 = pin 8
    *
    * Encoder:
    * Button11 pulsing if turning in one direction
    * Button12 pulsing if turning in the other direction
    */
    //////////////////////////////////////


    // 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(PinButton0, 10);
    Bounce button1 = Bounce(PinButton1, 10); // 10 = 10 ms debounce time
    Bounce button2 = Bounce(PinButton2, 10); // which is appropriate for
    Bounce button3 = Bounce(PinButton3, 10); // most mechanical pushbuttons
    Bounce button4 = Bounce(PinButton4, 10);
    Bounce button7 = Bounce(PinButton7, 10);
    Bounce button8 = Bounce(PinButton8, 10);
    Bounce button9 = Bounce(PinButton9, 10);
    Bounce button12 = Bounce(PinButton12, 10);
    Bounce button16 = Bounce(PinButton16, 10);
    Bounce button17 = Bounce(PinButton17, 10);
    Bounce button18 = Bounce(PinButton18, 10);
    Bounce button19 = Bounce(PinButton19, 10);
    Bounce button20 = Bounce(PinButton20, 10);
    Bounce button21 = Bounce(PinButton21, 10);
    Bounce button22 = Bounce(PinButton22, 10);
    Bounce button23 = Bounce(PinButton23, 10);
    //////////////////////////////////////
    // Added by TadyTheFish
    //Bounce button5 = Bounce(PinButton5, 5); // Debounce time increased from 1ms to 5ms.
    //Bounce button6 = Bounce(PinButton6, 5); //
    //Bounce button10 = Bounce(PinButton10, 1); // Debounce time increased from 1ms to 5ms.
    //Bounce button11 = Bounce(PinButton11, 1); //
    //Bounce button15 = Bounce(PinButton15, 1); // Debounce time increased from 1ms to 5ms.
    //Bounce button14 = Bounce(PinButton14, 1); //

    long ResetButtonToZeroA1; // This is used to store at what time the "1" bit was sent to the PC. When you turn an encoder you have 50/50 chance that the input will stay "ON". We use this time to compare so we can reset the bit
    long ResetButtonToZeroB1; // to "OFF" after some time if the button was not toggled
    bool ResetOnceA1; // When we reset the button to "OFF", this is used to ensure that the bit is reset only once
    bool ResetOnceB1;
    long ResetButtonToZeroA2; // This is used to store at what time the "1" bit was sent to the PC. When you turn an encoder you have 50/50 chance that the input will stay "ON". We use this time to compare so we can reset the bit
    long ResetButtonToZeroB2; // to "OFF" after some time if the button was not toggled
    bool ResetOnceA2; // When we reset the button to "OFF", this is used to ensure that the bit is reset only once
    bool ResetOnceB2;
    long ResetButtonToZeroA3; // This is used to store at what time the "1" bit was sent to the PC. When you turn an encoder you have 50/50 chance that the input will stay "ON". We use this time to compare so we can reset the bit
    long ResetButtonToZeroB3; // to "OFF" after some time if the button was not toggled
    bool ResetOnceA3; // When we reset the button to "OFF", this is used to ensure that the bit is reset only once
    bool ResetOnceB3;
    long oldPosition;
    long oldPosition2;
    long oldPosition3;
    //long newPosition;
    long oldMillisPosition;
    long oldMillisPosition2;
    long oldMillisPosition3;
    long LoopTime;
    Encoder myEnc(PinButton5, PinButton6);
    Encoder myEnc2(PinButton10, PinButton11);
    Encoder myEnc3(PinButton14, PinButton15);
    //////////////////////////////////////

    void setup() {
    pinMode(PinButton0, INPUT_PULLUP);
    pinMode(PinButton1, INPUT_PULLUP);
    pinMode(PinButton2, INPUT_PULLUP);
    pinMode(PinButton3, INPUT_PULLUP);
    pinMode(PinButton4, INPUT_PULLUP);
    pinMode(PinButton7, INPUT_PULLUP);
    pinMode(PinButton8, INPUT_PULLUP);
    pinMode(PinButton9, INPUT_PULLUP);

    pinMode(PinButton12, INPUT_PULLUP);

    pinMode(PinButton16, INPUT_PULLUP);
    pinMode(PinButton17, INPUT_PULLUP);
    pinMode(PinButton18, INPUT_PULLUP);
    pinMode(PinButton19, INPUT_PULLUP);
    pinMode(PinButton20, INPUT_PULLUP);
    pinMode(PinButton21, INPUT_PULLUP);
    pinMode(PinButton22, INPUT_PULLUP);
    pinMode(PinButton23, INPUT_PULLUP);

    //////////////////////////////////////
    // Added by TadyTheFish
    //pinMode(PinButton5, INPUT_PULLUP); // The encoder is connected the same way as a button
    //pinMode(PinButton6, INPUT_PULLUP);
    //pinMode(PinButton10, INPUT_PULLUP);
    //pinMode(PinButton11, INPUT_PULLUP);
    //pinMode(PinButton15, INPUT_PULLUP);
    //pinMode(PinButton14, INPUT_PULLUP);
    //////////////////////////////////////
    //pinMode(32,INPUT_PULLUP);
    //pinMode(31,INPUT_PULLUP);
    //attachInterrupt(5, Count, FALLING);
    //myEnc.write(512); // try to init in middle
    //Joystick.button(4, 1);

    }

    void loop() {

    //delay(30);
    //LoopTime = micros();
    long newPosition = myEnc.read();
    long newPosition2 = myEnc2.read();
    long newPosition3 = myEnc3.read();
    //long newPosition = myEnc.read()/2;
    //Serial.println(newPosition);
    // ------------- Encoder 1 ------------- //
    if (newPosition > oldPosition ) {
    oldPosition = newPosition;
    Joystick.button(12, 0);
    Joystick.button(11, 1);

    oldMillisPosition = millis();

    }
    if (newPosition < oldPosition ) {
    oldPosition = newPosition;
    Joystick.button(11, 0);
    Joystick.button(12, 1);

    oldMillisPosition = millis();

    }
    if(newPosition == oldPosition){
    if(millis()-oldMillisPosition > 30){
    oldMillisPosition = millis();
    Joystick.button(11, 0);
    Joystick.button(12, 0);

    }
    }
    // ------------- Encoder 2 ------------- //
    if (newPosition2 > oldPosition2 ) {
    oldPosition2 = newPosition2;
    Joystick.button(7, 0);
    Joystick.button(6, 1);

    oldMillisPosition2 = millis();

    }
    if (newPosition2 < oldPosition2 ) {
    oldPosition2 = newPosition2;
    Joystick.button(6, 0);
    Joystick.button(7, 1);

    oldMillisPosition2 = millis();

    }
    if(newPosition2 == oldPosition2){
    if(millis()-oldMillisPosition2 > 30){
    oldMillisPosition2 = millis();
    Joystick.button(6, 0);
    Joystick.button(7, 0);

    }
    }
    // ------------- Encoder 3 ------------- //
    if (newPosition3 > oldPosition3 ) {
    oldPosition3 = newPosition3;
    Joystick.button(15, 0);
    Joystick.button(14, 1);

    oldMillisPosition3 = millis();

    }
    if (newPosition3 < oldPosition3 ) {
    oldPosition3 = newPosition3;
    Joystick.button(14, 0);
    Joystick.button(15, 1);

    oldMillisPosition3 = millis();

    }
    if(newPosition3 == oldPosition3){
    if(millis()-oldMillisPosition3 > 30){
    oldMillisPosition3 = millis();
    Joystick.button(14, 0);
    Joystick.button(15, 0);

    }
    }
    // 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();
    button7.update();
    button8.update();
    button9.update();
    button12.update();
    button16.update();
    button17.update();
    button18.update();
    button19.update();
    button20.update();
    button21.update();
    button22.update();
    button23.update();
    //////////////////////////////////////
    // Added by TadyTheFish
    //button5.update(); // The encoder is connected the same way as a button
    //button6.update();

    //////////////////////////////////////


    // Check each button for "falling" edge.
    // 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() && digitalRead(PinButton1) == HIGH && digitalRead(PinButton2) == HIGH && digitalRead(PinButton3) == HIGH && digitalRead(PinButton4) == HIGH ) { // This prevents an accidental PUSH IN to be activated if we are pushing the button Forward, Reverse, Left or Right
    Joystick.button(1, 1);
    }
    if (button1.fallingEdge()) {
    Joystick.button(1, 0); // If pushed IN button is active, deactivate it
    Joystick.button(2, 1);

    }
    if (button2.fallingEdge()) {
    Joystick.button(1, 0); // If pushed IN button is active, deactivate it
    Joystick.button(3, 1);
    }
    if (button3.fallingEdge()) {
    Joystick.button(1, 0); // If pushed IN button is active, deactivate it
    Joystick.button(4, 1);
    }
    if (button4.fallingEdge()) {
    Joystick.button(1, 0); // If pushed IN button is active, deactivate it
    Joystick.button(5, 1);
    }
    if (button7.fallingEdge()) {
    Joystick.button(8, 1);
    }
    if (button8.fallingEdge()) {
    Joystick.button(9, 1);
    }
    if (button9.fallingEdge()) {
    Joystick.button(10, 1);
    }
    ////////
    if (button12.fallingEdge()) {
    Joystick.button(13, 1);
    }

    if (button16.fallingEdge()) {
    Joystick.button(17, 1);
    }
    if (button17.fallingEdge()) {
    Joystick.button(18, 1);
    }
    if (button18.fallingEdge()) {
    Joystick.button(19, 1);
    }
    if (button19.fallingEdge()) {
    Joystick.button(20, 1);
    }
    if (button20.fallingEdge()) {
    Joystick.button(21, 1);
    }
    if (button21.fallingEdge()) {
    Joystick.button(22, 1);
    }
    if (button22.fallingEdge()) {
    Joystick.button(23, 1);
    }
    if (button23.fallingEdge()) {
    Joystick.button(24, 1);
    }

    // Check each button for "rising" edge
    // Update the Joystick buttons only upon changes.
    // rising = low (pressed - button connects pin to ground)
    // to high (not pressed - voltage from pullup resistor)
    if (button0.risingEdge()) {
    Joystick.button(1, 0);
    }
    if (button1.risingEdge()) {
    Joystick.button(2, 0);
    }

    if (button2.risingEdge()) {
    Joystick.button(3, 0);
    }

    if (button3.risingEdge()) {
    Joystick.button(4, 0);
    }
    if (button4.risingEdge()) {
    Joystick.button(5, 0);
    }
    if (button7.risingEdge()) {
    Joystick.button(8, 0);
    }
    if (button8.risingEdge()) {
    Joystick.button(9, 0);
    }
    if (button9.risingEdge()) {
    Joystick.button(10, 0);
    }
    ///////
    if (button12.risingEdge()) {
    Joystick.button(13, 0);
    }
    if (button16.risingEdge()) {
    Joystick.button(17, 0);
    }
    if (button17.risingEdge()) {
    Joystick.button(18, 0);
    }
    if (button18.risingEdge()) {
    Joystick.button(19, 0);
    }
    if (button19.risingEdge()) {
    Joystick.button(20, 0);
    }
    if (button20.risingEdge()) {
    Joystick.button(21, 0);
    }
    if (button21.risingEdge()) {
    Joystick.button(22, 0);
    }
    if (button22.risingEdge()) {
    Joystick.button(23, 0);
    }
    if (button23.risingEdge()) {
    Joystick.button(24, 0);
    }
    //////////////////////////////////////
    // Added by TadyTheFish
    /*if (button5.risingEdge()) { // This works in the opposite way that the upper code does. It turns off the button. If this was removed the button would be constantly ON
    if(digitalRead(PinButton6) == LOW){
    Joystick.button(11, 0);
    }
    }
    if (button6.risingEdge()) { // This works in the opposite way that the upper code does. It turns off the button. If this was removed the button would be constantly ON
    if(digitalRead(PinButton5) == LOW){
    Joystick.button(12, 0);
    }
    }
    if(millis() - ResetButtonToZeroA1 > 100 && ResetOnceA1 == false){ // This rutine is called when (current time) - (last time the button was active) is more than 100ms, and reset is flase
    Joystick.button(11, 0);
    ResetOnceA1 = true; // Here we set the reset to TRUE so when the code loops again it does not send another "0" to the PC. This prevents false triggers
    }
    if(millis() - ResetButtonToZeroB1 > 100 && ResetOnceB1 == false){
    Joystick.button(12, 0);
    ResetOnceB1 = true;
    }
    //////////////////////////////////
    */
    ///////////////////////////////////
    /*if (button15.risingEdge()) { // This works in the opposite way that the upper code does. It turns off the button. If this was removed the button would be constantly ON
    if(digitalRead(PinButton14) == LOW){
    Joystick.button(14, 0);
    }
    }
    if (button14.risingEdge()) { // This works in the opposite way that the upper code does. It turns off the button. If this was removed the button would be constantly ON
    if(digitalRead(PinButton15) == LOW){
    Joystick.button(15, 0);
    }
    }
    if(millis() - ResetButtonToZeroA3 > 100 && ResetOnceA3 == false){ // This rutine is called when (current time) - (last time the button was active) is more than 100ms, and reset is flase
    Joystick.button(14, 0);
    ResetOnceA3 = true; // Here we set the reset to TRUE so when the code loops again it does not send another "0" to the PC. This prevents false triggers
    }
    if(millis() - ResetButtonToZeroB3 > 100 && ResetOnceB3 == false){
    Joystick.button(15, 0);
    ResetOnceB3 = true;
    }*/
    //////////////////////////////////////
    //Serial.println(micros()- LoopTime);
    }

  21. #21
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    Quote Originally Posted by Nick Moxley View Post
    Im looking for a hand to get a CTS-288 Encoder working properly.
    ....
    here's the code and here's a link to the Encoders,

    https://www.digikey.ca/products/en?k...=288T232R161A2
    The datasheet documents two types. The "2 bit binary" type looks exactly like a regular quadrature encoder. I'd recommend trying the example from File > Examples > Encoder > Basic. Connect the middle pin ("B" in the datasheet) to GND, and the two outside pins ("A" and "B" in the datasheet) to pins 5 and 6.

    Please, do yourself a favor and just test with this example first to make sure the hardware is working.

    Some mechanical encoders have chatter. You might need to add small capacitors (to GND) on both the signal pins. But try it first the normal way...

Posting Permissions

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