Teensy 4.0 with multiple encoders and analogue input

Status
Not open for further replies.

Shumey

New member
Hi all,

Very new to the Teensy world and looking for some advise and perhaps a guide in the right direction. Sorry if my terminology is not correct.

I am building a gaming Sim Wheel. I am wanting to run 11 encoders in which they would make adjustments to in car controls in a sim game. Basically an up and down button i guess you might say.
Encoders are connected as code below with all centre pins daisy chained to ground

I have uploaded the Teensy/ encoder/ twoknobs example and have edited it to show my 11 encoders. With the serial monitor i can see all encoders and make them count up and down however when rotated i get the odd jump in my steps (count)?

I am also lost as to how to add an analogue input which i would use a potentiometer as a paddle clutch.

The other issue i have is that this doesnt make the teensy a windows USB game device.

As i said i am very new to this and just looking for some assistance please.

Code i am using follows -

*******************************************************************************************

#include <Encoder.h>

// Change these pin 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 encoder1(0, 1);
Encoder encoder2(2, 3);
Encoder encoder3(4, 5);
Encoder encoder4(6, 7);
Encoder encoder5(8, 9);
Encoder encoder6(10, 11);
Encoder encoder7(12, 14);
Encoder encoder8(15, 16);
Encoder encoder9(17, 18);
Encoder encoder10(19, 20);
Encoder encoder11(21, 22);

// avoid using pins with LEDs attached

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

long position1 = -999;
long position2 = -999;
long position3 = -999;
long position4 = -999;
long position5 = -999;
long position6 = -999;
long position7 = -999;
long position8 = -999;
long position9 = -999;
long position10 = -999;
long position11 = -999;

void loop() {
long enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8, enc9, enc10, enc11;
enc1 = encoder1.read();
enc2 = encoder2.read();
enc3 = encoder3.read();
enc4 = encoder4.read();
enc5 = encoder5.read();
enc6 = encoder6.read();
enc7 = encoder7.read();
enc8 = encoder8.read();
enc9 = encoder9.read();
enc10 = encoder10.read();
enc11 = encoder11.read();

if (enc1 != position1 || enc2 != position2 || enc3 !=position3 || enc4 != position4 || enc5 !=position5 || enc6 != position6 || enc7 !=position7 || enc8 != position8 || enc9 !=position9 || enc10 != position10 || enc11 !=position11) {
Serial.print("Encoder1 = ");
Serial.print(enc1);
Serial.print(", Encoder2 = ");
Serial.print(enc2);
Serial.print(", Encoder3 = ");
Serial.print(enc3);
Serial.print(", Encoder4 = ");
Serial.print(enc4);
Serial.print(", Encoder5 = ");
Serial.print(enc5);
Serial.print(", Encoder6 = ");
Serial.print(enc6);
Serial.print(", Encoder7 = ");
Serial.print(enc7);
Serial.print(", Encoder8 = ");
Serial.print(enc8);
Serial.print(", Encoder9 = ");
Serial.print(enc9);
Serial.print(", Encoder10 = ");
Serial.print(enc10);
Serial.print(", Encoder11 = ");
Serial.print(enc11);
Serial.println();
position1 = enc1;
position2 = enc2;
position3 = enc3;
position4 = enc4;
position5 = enc5;
position6 = enc6;
position7 = enc7;
position8 = enc8;
position9 = enc9;
position10 = enc10;
position11 = enc11;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
encoder1.write(0);
encoder2.write(0);
encoder3.write(0);
encoder4.write(0);
encoder5.write(0);
encoder6.write(0);
encoder7.write(0);
encoder8.write(0);
encoder9.write(0);
encoder10.write(0);
encoder11.write(0);
}
}

I have also uploaded the examples/teensy/usb_joystick sketch and can then see teensy as a usb game controller however the button do very wierd and wonderful things when encoder turned and obviously dont count correctly.

Any help would be greatly appreciated
 
Last edited:
I think most encoders are pretty rubbish. The cheap ones at least. You could try fixing it in software e.g. something like if (abs(position1 - enc1) > 4){ignore();} but I think the root cause will still be there.

In terms of analogue input, the function is analogRead(pin). Connect the wiper of a potentiometer to the pin and the other terminals to GND and +3.3V. Simple.

I don't know about Windows game device, but if you change the USB type to keyboard + mouse + joystick, does that work?

And in terms of your code, if you want to develop your programming, learn about arrays and for loops. e.g.:



Code:
const int numberOfEncoders = 11;

Encoder allTheEncoders[numberOfEncoders] = {
    {0, 1},
    {2, 3},
    {4, 5},
    {6, 7},
    {8, 9},
    {10, 11}.
    {12, 14}.
    {15, 16}.
    {17, 18}.
    {19, 20}.
    {21, 22}.
};

long encoderValues[numberOfEncoders];

for(int i=0; i<numberOfEncoders ; i++){
    encoderValues[i] = allTheEncoders[i].read();
}

That's not complete code and I've not checked it compiles, but it should hopefully give you an idea get going with.

Hope that helps.
 
if you are getting "jumps" meaning 1 click on your encoder produces several counts, it's most likely a bouncing issues. Meaning inside encoder the mechanical contacts literally bounce and cause extra fires. This happens super fast, but these MCU's are fast enough to detect the bouncing.

a few ways you can address this
1) There are libraries that can be use to handle bouncing.
2) maybe put a cap across the signal pin and ground (experiment but 10uF to 100uF may do the trick, This will reduce/eliminate irregular voltages within a short time period
3) do you own software debouncing, generally when you get a pulse, compare that time (millis()) with the previous pulse. If it's greater that some limit, consider it a legitimate pulse.

Something like...

if (pulse && (millis() - oldTime) > 20){ // consider anything pulses over 20 ms apart a legitimate pulse
// its legit
oldTime = millis();

// your pulse processing code

}
 
Status
Not open for further replies.
Back
Top