Teensy 3.1 analog sticks to generate keyboard input
Hi guys,
at the moment im building a gameboy casemod similar to this: http://www.xodustech.com/projects/ra...gameboy-pocket .
For the keypresses im using my teensy to act as a usb keyboard and mouse controller just as the guy on this website did it, but i want to include two psp replacement analog sticks in my project so i have better ps1 emulation control.
I want them to behave as keyboard keys just like the other buttons so they arent actually analog because i think this is the best way to include them in the teensyduino code provided by the website (and here: https://github.com/WarriorRocker/pi-pocket), as i dont have to specify another input mode for them then.
I did not find someone doing it that way and this is my first time using a c language instead of python.
so i edited the code to include the analog sticks but as im on vacation i cant try it out. so my question is does the approach i chose work at all?
(i dont want you to painstakingly look for syntax mistakes etc, but just ask you to give it a look and see if youre experienced if its complete shit or might work)
Thanks
my idea was to set the right-horizontal-value (rhorival) to 2 so the script knows the stick is alredy in the middle and wont set this to 0 again as there are 2 requirements to execute the code and they are only met when the variable is for example 1 or 3 when the stick is moved
and for the later part i thought as the usb protocol only can send 6 keys plus modifiers at one time i set it up that way as you dont move the right stick and press one of these buttons at the same time. you would have to really hold the controller in a weird way to do this i think
dude youre quick...
yeah maybe ill implement something like this:
Code:
int data =0;
for (int i=0; i<3;i++) {
data = data + analogRead(A0);
}
data = data / 3;
to make measurements more consistent and also do something to limit the times the keys are checked per second a bit so you dont really feel it but the teensy doesnt run it like crazy.
This means that between 400-450 and 550-600 the state won't change and the state remains at it's previous state. This means if you move the stick to 400 and wiggle it a little it'll just set KEY_M once and won't keep toggling. This helps to keep your controller as responsive as possible.
I guess if you put a cat in a box who moved the analogue stick to 425 the key would be both KEY_B and 0
yes its called a deadzone ..its a span for the 'rest' position .... using it in combination with hysterisis is what zeno has suggested (averaging is a good idea too), and it will get you all the way home. I do hysterisis a little differently from zeno ... Here is some code (from memory, untested, no compiler) I use it for an analog slider controlling a motor... effectively loosing resolution (I have an idea to improve resolution but I don't think you need it)
Code:
//global
bool zero=false;
int currentread;
int prevread = 512;
//loop()
currentread = analogRead(A3);
if ((prevread + 3) < currentread) {prevread=currentread;}
else if ((prevread - 3) > currentread) {prevread=currentread;}
else currentread = prevread;
if (currentread < 600 && currentread > 400) { if (zero=false) {Keyboard.set:key3(0); Keyboard.send_now(); zero = true;}}
else if(currentread >= 600 {Keyboard.set:key3(KEY_B);
Keyboard.send_now(); zero=false;}
else {Keyboard.set:key3(KEY_M);
Keyboard.send_now(); zero=false;}
Last edited by adrian; 02-10-2016 at 10:37 PM.
Reason: woops zero is false.
there is the joystick usb type, which is good for analog sticks / gamepads ....
yeah for games only would be better but i want to control xmbc and cli music players and stuff with it too
So its been a while and i finally tested everything.
Thanks to you guys i only had to make very little changes so here is a pic of my test setup with only one stick attached (was alredy disassembling when i thought i could make a picture)
and the final version of the code will be attached below so guys searching for similar things can get it!
but beware.... there are 4 variables with non selfdocumenting names, but im to tired to change them before uploading.
btw. is it possible or common to mark a thread as closed in this forum?