Software Support > Automated Projects
Yet another Rotary Joystick project
Ginsonic:
First tests with one stick attached worked like a charm ;D
The only thing, that is urgently needed, is a small capacitor between ground and analog input port for debouncing !
melvinbates:
That's an interesting idea with the resistors, very cool.
Ginsonic:
--- Quote from: melvinbates on February 15, 2017, 10:57:51 am ---That's an interesting idea with the resistors, very cool.
--- End quote ---
And it really works great, today I simultaneously tested with two rotary sticks, no glitches, no false moves regardless how fast I rotate the handle.
For now the only thing with Arduino keyboard is, that it can only send regular ASCII characters but no special chars. I have to check MAME, which free keys I could use for the movements of both players (sticks), or I have to find a way to get another keyboard implementation.
Ginsonic:
I guess, that's why they call it micro ;D
Cute !
Ginsonic:
Well, project succesfully finished ! All I have to do now is, to create a new control panel for my cab to add the two rotary sticks. As promised before, here is the sketch. Currently it is implemented for up to two sticks, but it can be easily modified for more sticks. The "If debug" parts can be removed, they are for testing only !
You have to include two Arduino libraries:
https://github.com/NicoHood/HID
https://github.com/donid/akuhell
It was much fun, thanks for watching ;D !
--- Quote ---#include "HID-Project.h"
#include "akuhell_languages.h"
#define KEYBOARD_LAYOUT_LANG KBD_LAYOUT_EN_US
#include "akuhell.h"
boolean debug = false;
char str[512];
const int CW = 1, CCW = -1;
const int rotaryPins = 12; // number of used pins on rotary switch
const int players = 2; // for one player set to 1, for more than 2 players set to desired value, raise maxPlayers and add array values
const int maxPlayers = 2; // if more than two players are needed, raise value and add corresponding values to the arrays below
int idxLastPin[maxPlayers] = {0, 0}; // variable for storing the last pin. If adding new array values, set their value to 0
int analogPort[maxPlayers] = {0, 1}; // portnumber of used analog port for the different rotary sticks
String keyCw[maxPlayers] = {"]", "/"}; // sent keypress for clockwise rotation for the different rotary sticks
String keyCcw[maxPlayers] = {"[", "\\"}; // sent keypress for counterclockwise rotation for the different rotary sticks
void setup()
{
Serial.begin(9600);
Keyboard.begin();
// store selected pins at startup
for (int i = 0; i < players; i++) {
idxLastPin = getActiveRotaryPin(analogPort);
if (debug) {
sprintf(str, "<setup> player: %d / active pin: %d", i + 1, idxLastPin + 1);
Serial.println(str);
}
}
}
void loop()
{
// check rotation for all sticks/players
for (int idxPlayer = 0; idxPlayer < players; idxPlayer++) {
handleRotation(idxPlayer);
}
delay(10);
}
int getActiveRotaryPin (int port)
{
float pinVal = 0.0;
int pinValRaw = 0 ;
int pinIndex = 0;
float divider = 1023.0 / (float)(rotaryPins - 1);
pinValRaw = analogRead(port);
pinVal = round((float)pinValRaw / divider);
pinIndex = pinVal;
return pinIndex;
}
void handleRotation(int idxPlayer) {
// get current selected pin
int idxActivePin = getActiveRotaryPin(analogPort[idxPlayer]);
// stick was rotated ?
if (idxActivePin != idxLastPin[idxPlayer]) {
if (debug) {
sprintf(str, "<handleRotation> player: %d / pin %d to %d", idxPlayer + 1, idxLastPin[idxPlayer] + 1, idxActivePin + 1);
Serial.println(str);
}
// send key
if (idxLastPin[idxPlayer] > idxActivePin && idxLastPin[idxPlayer] - idxActivePin > 3) {
sendKey(idxPlayer, CCW);
}
else if (idxActivePin > idxLastPin[idxPlayer] && idxActivePin - idxLastPin[idxPlayer] > 3) {
sendKey(idxPlayer, CW);
}
else if (idxActivePin > idxLastPin[idxPlayer]) {
sendKey(idxPlayer, CCW);
}
else if (idxActivePin < idxLastPin[idxPlayer]) {
sendKey(idxPlayer, CW);
}
// store current pin as source for next movement
idxLastPin[idxPlayer] = idxActivePin;
}
}
void sendKey(int idxPlayer, int dir) {
switch (dir) {
case CW:
if (debug) {
sprintf(str, "<sendKey> player %d: / CW ", idxPlayer + 1);
Serial.println(str);
}
Keyboard_writeUtf8Character(keyCw[idxPlayer]);
break;
case CCW:
if (debug) {
sprintf(str, "<sendKey> player %d: / CCW ", idxPlayer + 1);
Serial.println(str);
}
Keyboard_writeUtf8Character(keyCcw[idxPlayer]);
break;
default:
break;
}
}
--- End quote ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version