I am trying to use my Leonardo as joystick by using the Library I found here: http://www.imaginaryindustries.com/blog/?p=80. I tried to compile the example from there but only got this error message:
wheel_pedals_demo.ino: In function 'void loop()':
wheel_pedals_demo:57: error: 'Joystick' was not declared in this scope
I took a look at the .h and .cpp files, but since i'm not very knowledgable when it comes to debugging libraries I can't figure out what went wrong there. Please help!
Edit: I should really stop rushing stuff and properly read the instructions:
Modified arduino files (replace the files in {arduino executable dir}\hardware\arduino\cores\arduino\ with these):
USBAPI.h
HID.cpp
Well, that's embarrassing
Still got one question: How do i get it done that a key sent by the Leonardo stays pressed, but doesn't repeat itself all the time? Cant figure that out for days now. Yes, I tried using Google.
I'm trying to make my own game controller that's recognized as a joystick. The demo code works, I did just put the files into the wrong directory. I haven't written my own joystick code yet, but here's my not so well working keyboard code:
I know, it's awful...
The problem is that it's stuttering because of the repeating which gets me kicked off the server. It makes the game think that I must have got a horrible connection.
Okay, I'll try that after having lunch :). I worked trough that about nine months ago and then had my Arduino collecting dust most off the time. I just have to get back into the matter!
I tried using switch states, but my sketch only works right if only a single function is called. If there's more than one function it repeats the keystrokes as fast as an if-loop :(. So walking into a single direction works fine, but that's pretty much pointless in a 3D game. Or in video games in general :). Anyway, here's the code I didn't complete for all directions as two won't even work:
void forward(){
int val_W = 0;
int oldval_W = 1; //You want this to be static (and probably initialised to 0) (static int oldval_W=0;) otherwise it gets initialised to 1 every time you enter the function
val_W = digitalRead(BUTTON_W);
if (val_W != oldval_W) { // You only want to do this when val_W!=oldval_W and val_W is HIGH (at the moment you will get a button press at each rising and falling edge
Keyboard.press('w');
}
else { //You want this to be an else if, checking if val_W!=oldval_W and val_W is LOW (falling edge)
oldval_W = val_W; //This should be outside the else statement, you still want it to happen when you get a rising edge
Keyboard.releaseAll(); // Make this Keyboard.release('w') to stop it releasing all of the other keys
}
}
The other suggestion I would make, especially as I imagine you will be adding extra buttons, is to make a struct for storing button states, and then make a generic function for checking button presses:
typedef struct{
char pin;
char character;
char lastState;
} JoystickButton;
//Declare the buttons: (this could be an array)
JoystickButton wButton;
JoystickButton aButton;
//in setup() initialise them:
setup(){
wButton.character='w';
wButton.pin=BUTTON_W;
aButton.character='a';
aButton.character=BUTTON_A;
}
//the button function:
void checkButton(JoystickButton* button){
char currentState;
currentState=digitalRead(button->pin);
if(currentState!=button->lastState&¤tState==HIGH){
Keyboard.press(button->character);
}
// and so on...
}
//in you loop:
void loop(){
checkButton(&wButton);
checkButton(&aButton);
}
Doing it this way makes your code much easier to maintain, and will significantly reduce the amount you write and the amount of program space it takes up.
Well, that looks promising! Of course there will be more inputs, ten when it's done. Arcade style. As i have guessed: I've made dumb logical errors. oldVal is initialised to 1 on purpose, that seems to be the only proper way to use the internal pullups on an Olimexino-32u4. And the Keyboard.releaseAll(); was pure lazyness, I didn't spent much (if any) time thinking about that... facepalm
Yeah if you're using pull up resistors on your buttons then they will be in an active low configuration (pin pulled low by button being pressed) so all the logic will be inverted.
Exactly. I got used to that. But I still can't get it working, regardless of what i'm trying. Don't even ask about the struct. Always just one of the functions. I feel like a complete idiot =(. Seriously, it really can't be that hard!
Anyway, here's the code I didn't complete for all directions as two won't even work:
That is because you keep defining a new value for the last state and so you wipe out that state when you do, therefore you never see a change.
make the defining of oldval_W global, that is outside the function definition. Then use that variable inside.
Putting int in front of a variable name creates a completely new variable of the same name and puts the old one out of reach to the rest of the code.
Or maybe i'll stick with this, what doesn't kill me just makes me stronger :D. It doesn't seem to be my day today, I keep missing out on the simplest of stuff...
Try adding in some Serial.print() for debugging, put it in places such as detecting a button press to make sure you are actually detecting the button press properly. This will help you pinpoint your errors.
Hi, I am new to arduino,
what I want is to map my multicopter receiver's PWM signal to a joystick port, is there already a sketch by someone or similar I can use in the beginning?
I have a Leonardo board a Pro Micro and I have 4 input channels: throttle, roll, pitch, yaw, both are standard pwm: 1000 lowest 1500 middle and 2000 the maximum value of the sticks positions the receiver gets.
Could you help me to find what code to start with?
Just a standard PWM input to a joystick axis mapping would be great!