Hi Guys,
Im trying to make the Adruino Due work as a multi button joystick with a few functions which I have managed to program such that 2 physical buttons operate 3 joystick buttons.
All this works fine on the Leonardo and the Due - BUT - the Due does not stay in the Joystick state after unplugging and re-plugging.
Only after a reset on the board after it has been plugged back in does it operate as the way I want it to.
I have done some research and found that this is due to the GPNVM and have looked at this:
http://playground.arduino.cc/Bootloader/DueBootloaderExplained
however it does not tell me how to modify the code so that it always stay as the device I am looking to create. (I am completely new to all of this).
Here is the basic part of the coding which I want to use to get the functions as I want. If anyone can help me to get the code to maintain the joystick as a joystick OR point me in the right direction it will be much appreciated.
[code]
#include <Keyboard.h>
#include <Encoder.h>
#include <Joystick.h>
Encoder myEnc(2, 3);
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
// Initialize Joystick Library
Joystick.begin();
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read()/4;
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
// Read pin values
int button3Val =digitalRead(4);
int button4Val =digitalRead(5);
int button6Val =digitalRead(6);
int button7Val =digitalRead(7);
int button9Val =digitalRead(8);
int button10Val =digitalRead(9);
int button12Val =digitalRead(10);
int button13Val =digitalRead(11);
int button15Val =digitalRead(12);
int button16Val =digitalRead(13);
Joystick.setButton(6, !button6Val);
Joystick.setButton(7, !button7Val);
if ((button6Val == 0) || (button7Val == 0)) {
Joystick.setButton(8, 0);
} else {
Joystick.setButton(8, 1);
}
Joystick.setButton(9, !button9Val);
Joystick.setButton(10, !button10Val);
if ((button9Val == 0) || (button10Val == 0)) {
Joystick.setButton(11, 0);
} else {
Joystick.setButton(11, 1);
}
Joystick.setButton(12, !button12Val);
Joystick.setButton(13, !button13Val);
if ((button12Val == 0) || (button13Val == 0)) {
Joystick.setButton(14, 0);
} else {
Joystick.setButton(14, 1);
}
Joystick.setButton(15, !button15Val);
Joystick.setButton(16, !button16Val);
if ((button15Val == 0) || (button16Val == 0)) {
Joystick.setButton(17, 0);
} else {
Joystick.setButton(17, 1);
}
// Joysticx axis
int xaxis = A0;
int val = 0;
val = analogRead(xaxis)>>2;
Joystick.setXAxis((val)-127);
}
[/code]