BeesBeesBees
Member
Trying to make a button box with buttons, encoders, and potentiometers. Everything is working, but I'm running into an issue with the potentiometers.
For some reason they don't seem to output the full range. The anlogueRead output was 15 through 998, but then randomly when I switched to from "Joystick.Z" to "Joystick.Zrotate", the anlogueRead output range jumped to be 960 through 63929 and then randomly went back to the lower range after I uploaded the code again. ( found out I get the higher numbers when I choose the joystick option without serial)
The major issue right now is that all axes don't seem to be using their full range - I'm not sure how to fix this.
I'm very new to all of this and am very close to being done. Looking for help
Additional details
- using windows 10
- using Teensy 4.1
For some reason they don't seem to output the full range. The anlogueRead output was 15 through 998, but then randomly when I switched to from "Joystick.Z" to "Joystick.Zrotate", the anlogueRead output range jumped to be 960 through 63929 and then randomly went back to the lower range after I uploaded the code again. ( found out I get the higher numbers when I choose the joystick option without serial)
The major issue right now is that all axes don't seem to be using their full range - I'm not sure how to fix this.
I'm very new to all of this and am very close to being done. Looking for help
Additional details
- using windows 10
- using Teensy 4.1
Code:
//Six rows on pins 0, 1, 2, 3, 4, 5
//Seven columns on pins 23, 22, 21, 20, 19, 18, 17,
//Four rotary encoders, pins (33,34) (36,37) (38,39) (40,41)
//Three potentiometers, pins 24, 25, 26
#include <Keypad.h>
#define JOYSTICK_SIZE 64 // 12 = normal, 64 = extreme joystick
#include "EncoderTool.h"
using namespace EncoderTool;
//Button Matrix Section
const byte ROWS = 6; //four rows
const byte COLS = 7; //four columns
byte rowPins[ROWS] = {0, 1, 2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {23, 22, 21, 20, 19, 18, 17,}; //connect to the column pinouts of the keypad
char keyMap[ROWS][COLS] = {
{1,2,3,4,5,6,7},
{8,9,10,11,12,13,14},
{15,16,17,18,19,20,21},
{22,23,24,25,26,27,28},
{29,30,31,32,33,34,35},
{36,37}
};
Keypad kpd = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
//Encoder Section
PolledEncoder encoderA;
PolledEncoder encoderB;
PolledEncoder encoderC;
PolledEncoder encoderD;
void setup()
{
encoderA.begin(33, 34);
encoderB.begin(36, 37);
encoderC.begin(38, 39);
encoderD.begin(40, 41);
Joystick.X(512); // same as Windows default
Joystick.Y(512); // same as Windows default
Joystick.Zrotate(512); // same as Windows default
Joystick.useManualSend(true);
}
void loop()
{
Joystick.hat(1, 361); //For some reason this is Hat #4
Joystick.hat(2, 361); // This is Hat #3
Joystick.hat(3, 361); // This is Hat #2
Joystick.hat(4, 361); // Hat #1
//Joystick.X(analogRead(24));
//Joystick.Y(analogRead(25));
//Joystick.Z(analogRead(26));
int xValue;
int yValue;
int zValue;
int deadZone = 3;
xValue = analogRead(24);
yValue = analogRead(25);
zValue = analogRead(26);
if (xValue < (15 + deadZone)) xValue = 15;
if (xValue > (998 - deadZone)) xValue = 998;
if (yValue < (15 + deadZone)) yValue = 15;
if (yValue > (998 - deadZone)) yValue = 998;
if (zValue < (15 + deadZone)) zValue = 15;
if (zValue > (998 - deadZone)) zValue = 998;
Joystick.X(xValue);
Joystick.Y(yValue);
Joystick.Zrotate(zValue);
encoderA.tick();
encoderB.tick();
encoderC.tick();
encoderD.tick();
Joystick.send_now();
if ( kpd.getKeys() )
{
for (int i=0; i<LIST_MAX; i++)
{
if ( kpd.key[i].stateChanged )
{
if ( kpd.key[i].kstate == PRESSED || kpd.key[i].kstate == HOLD)
{
Joystick.button(kpd.key[i].kchar, 1);
} else if ( kpd.key[i].kstate == RELEASED )
{
Joystick.button(kpd.key[i].kchar, 0);
}
Joystick.send_now();
}
}
}
if (encoderA.valueChanged())
{
if (encoderA.getValue() == 1)
{
Joystick.button(38, 1);
encoderA.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(38, 0);
}
else if (encoderA.getValue() == -1)
{
Joystick.button(39, 1);
encoderA.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(39, 0);
}
else
{
encoderA.setValue(0);
Joystick.button(38, 0);
Joystick.button(39, 0);
Joystick.send_now();
}
}
if (encoderB.valueChanged()) // do we have a new value?
{
if (encoderB.getValue() == 1)
{
Joystick.button(40, 1);
encoderB.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(40, 0);
}
else if (encoderB.getValue() == -1)
{
Joystick.button(41, 1);
encoderB.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(41, 0);
}
else
{
encoderB.setValue(0);
Joystick.button(40, 0);
Joystick.button(41, 0);
Joystick.send_now();
}
}
if (encoderC.valueChanged()) // do we have a new value?
{
if (encoderC.getValue() == 1)
{
Joystick.button(42, 1);
encoderC.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(42, 0);
}
else if (encoderC.getValue() == -1)
{
Joystick.button(43, 1);
encoderC.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(43, 0);
}
else
{
encoderC.setValue(0);
Joystick.button(43, 0);
Joystick.button(44, 0);
Joystick.send_now();
}
}
if (encoderD.valueChanged()) // do we have a new value?
{
if (encoderD.getValue() == 1)
{
Joystick.button(45, 1);
encoderD.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(45, 0);
}
else if (encoderD.getValue() == -1)
{
Joystick.button(46, 1);
encoderD.setValue(0);
Joystick.send_now();
delay(30);
Joystick.button(46, 0);
}
else
{
encoderD.setValue(0);
Joystick.button(45, 0);
Joystick.button(46, 0);
Joystick.send_now();
}
}
delay(5);
}