Update: I am about 90% there I think,
All the inputs are recognized in Devices--> Game Controller--> Properties however their behavior is unexpected.
All buttons work as expected, so that is good.
The analog input bounces around alot and it's travel is limited to about a third of the total possible travel. I remember seeing something about this but I don't remember exactly what it was or where I saw it. I don't foresee this being a difficult problem to solve.
The other unexplained behavior is with the encoders. They send a button press sporadically. They do not send a pulse every click. When they do work it is correct for the direction in which the knob is turned but it is hit and miss. They will often work on the fourth click or a multiple of four, i.e 4,8,12...etc, but not always. Occasionally it is off by one number in either direction but usually, if it fires at all, it will be on a fourth click. This behavior exists whether I turn the knob fast or slow.
The encoders I am using are CTS288V232R161B2.
http://www.leobodnar.com/files/288-2255.pdf
They are quadrature encoders which the library I am using is designed for.
I am using the Rotate.H library. It can be found here:
Ben Buxtronics Website:
https://github.com/MHeironimus/ArduinoJoystickLibrary
GitHub Rotary.H:
https://github.com/buxtronix/arduino/tree/master/libraries/Rotary
On A Side note, I found a YT video and website from a guy who has done exactly what I want to do minus analog.
He built a button box with pushbuttons and encoders using Teensy LC. I was stoked when I found this. I thought my problems were solved.
AMStudio 32 Button box YT:
https://www.youtube.com/watch?v=Z7Sc4MJ8RPM
GitHub Joystick.H:
https://github.com/MHeironimus/ArduinoJoystickLibrary
I loaded his sketch and added the library he recommended to the IDE but when I verify I get this error:
Arduino: 1.8.9 (Windows 10), TD: 1.53, Board: "Teensy LC, Serial, 48 MHz, Smallest Code, US English"
In file included from C:\Users\John\Documents\Arduino\libraries\ArduinoJoystickLibrary-master\src/Joystick.h:24:0,
from C:\Users\John\Desktop\Sketches\32-FUNCTION-BUTTON-BOX-master\32-FUNCTION-BUTTON-BOX-master\ARDUINO_BUTTON_BOXV2\ARDUINO_BUTTON_BOXV2.ino:5:
C:\Users\John\Documents\Arduino\libraries\ArduinoJoystickLibrary-master\src/DynamicHID/DynamicHID.h:37:28: fatal error: PluggableUSB.h: No such file or directory
compilation terminated.
Error compiling for board Teensy LC.
After a quick internet search I found this error is happening with some people and they can fix it by downgrading to a previous version of the Arduino IDE. I tried that but it did not help.
Anyone know how to fix that error?
For reference, here is the sketch I am using that is close to working but is giving me the encoder and analog input problems I mentioned above. I have also attached the sketch file to this post:
#include <Bounce.h>
#include <elapsedMillis.h>
#include <Rotary.h>
Bounce r_button_1 = Bounce(20,10);
Bounce r_button_2 = Bounce(21,10);
Bounce r_button_3 = Bounce(22,10);
Bounce r_button_4 = Bounce(5,10);
Bounce r_button_5 = Bounce(4,10);
Bounce r_button_6 = Bounce(3,10);
Bounce r_button_7 = Bounce(12,10);
Bounce r_button_8 = Bounce(1,10);
Bounce r_button_9 = Bounce(15,10);
Bounce r_button_10 = Bounce(23,10);
Bounce enc_button_1 = Bounce(9,10);
Bounce enc_button_2 = Bounce(8,10);
elapsedMillis timeElapsed;
unsigned int enc_interval = 20; //interval for encoders
Rotary rotary1 = Rotary(6, 7);
Rotary rotary2 = Rotary(10, 11);
void setup() {
attachInterrupt(6, rotate, CHANGE);
attachInterrupt(7, rotate, CHANGE);
attachInterrupt(10, rotate, CHANGE);
attachInterrupt(11, rotate, CHANGE);
pinMode(1, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
}
void loop() {
//UPDATE
r_button_1.update();
r_button_2.update();
r_button_3.update();
r_button_4.update();
r_button_5.update();
r_button_6.update();
r_button_7.update();
r_button_8.update();
r_button_9.update();
r_button_10.update();
enc_button_1.update();
enc_button_2.update();
// READ ANALOG INPUT A12 (pin 26) AND SET X POSITION
Joystick.X(analogRead(A12));
//HANDLE BUTTONS
if (r_button_1.fallingEdge()) {
Joystick.button(20,1);
}
if (r_button_1.risingEdge()) {
Joystick.button(20,0);
}
if (r_button_2.fallingEdge()) {
Joystick.button(21,1);
}
if (r_button_2.risingEdge()) {
Joystick.button(21,0);
}
if (r_button_3.fallingEdge()) {
Joystick.button(22,1);
}
if (r_button_3.risingEdge()) {
Joystick.button(22,0);
}
if (r_button_4.fallingEdge()) {
Joystick.button(5,1);
}
if (r_button_4.risingEdge()) {
Joystick.button(5,0);
}
if (r_button_5.fallingEdge()) {
Joystick.button(4,1);
}
if (r_button_5.risingEdge()) {
Joystick.button(4,0);
}
if (r_button_6.fallingEdge()) {
Joystick.button(3,1);
}
if (r_button_6.risingEdge()) {
Joystick.button(3,0);
}
if (r_button_7.fallingEdge()) {
Joystick.button(12,1);
}
if (r_button_7.risingEdge()) {
Joystick.button(12,0);
}
if (r_button_8.fallingEdge()) {
Joystick.button(1,1);
}
if (r_button_8.risingEdge()) {
Joystick.button(1,0);
}
if (r_button_9.fallingEdge()) {
Joystick.button(15,1);
}
if (r_button_9.risingEdge()) {
Joystick.button(15,0);
}
if (r_button_10.fallingEdge()) {
Joystick.button(23,1);
}
if (r_button_10.risingEdge()) {
Joystick.button(23,0);
}
//HANDLE ENCODER BUTTONS
if (enc_button_1.fallingEdge()) {
Joystick.button(9,1);
}
if (enc_button_1.risingEdge()) {
Joystick.button(9,0);
}
if (enc_button_2.fallingEdge()) {
Joystick.button(8,1);
}
if (enc_button_2.risingEdge()) {
Joystick.button(8,0);
}
if (timeElapsed > enc_interval) {
Joystick.button(6, 0)
Joystick.button(7,0)
Joystick.button(10,0)
Joystick.button(11,0)
}
void rotate() {
// HANDLE ENCODER 1
unsigned char result1 = rotary1.process();
if (result1 == DIR_CW) {
timeElapsed = 0;
Joystick.button(6,1);
Joystick.button(7,0);
} else if (result1 == DIR_CCW) {
timeElapsed = 0;
Joystick.button(7,1);
Joystick.button(6,0);
}
// HANDLE ENCODER 2
unsigned char result2 = rotary2.process();
if (result2 == DIR_CW) {
timeElapsed = 0;
Joystick.button(10,1);
Joystick.button(11,0);
} else if (result2 == DIR_CCW) {
timeElapsed = 0;
Joystick.button(11,1);
Joystick.button(10,0);
}
}
Thanks, John