Als jetzt alles da war habe ich alles zusammengesteckt und mit Arduino IDE folgenden Code aufgespielt.
// Program used to test the Dual Simple USB Joystick object
// on the Arduino Leonardo or Arduino Micro.
//
// Matthew Heironimus
// 2015-04-05
//------------------------------------------------------------
#include "Joystick2.h"
// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
//const bool testAutoSendMode = true;
const bool testAutoSendMode = false;
const unsigned long gcCycleDelta = 1000;
const unsigned long gcAnalogDelta = 25;
const unsigned long gcButtonDelta = 500;
unsigned long gNextTime = 0;
unsigned int gCurrentStep = 0;
int gJoystickId = 0;
void testSingleButtonPush(int joystickId, unsigned int button)
{
if (button > 0)
{
Joystick[joystickId].releaseButton(button - 1);
}
if (button < 16)
{
Joystick[joystickId].pressButton(button);
}
}
void testMultiButtonPush(int joystickId, unsigned int currentStep)
{
for (int button = 0; button < 16; button++)
{
if ((currentStep == 0) || (currentStep == 2))
{
if ((button % 2) == 0)
{
Joystick[joystickId].pressButton(button);
} else if (currentStep != 2)
{
Joystick[joystickId].releaseButton(button);
}
} // if ((currentStep == 0) || (currentStep == 2))
if ((currentStep == 1) || (currentStep == 2))
{
if ((button % 2) != 0)
{
Joystick[joystickId].pressButton(button);
} else if (currentStep != 2)
{
Joystick[joystickId].releaseButton(button);
}
} // if ((currentStep == 1) || (currentStep == 2))
if (currentStep == 3)
{
Joystick[joystickId].releaseButton(button);
} // if (currentStep == 3)
} // for (int button = 0; button < 32; button++)
}
void testXYAxis(int joystickId, unsigned int currentStep)
{
if (currentStep < 255)
{
Joystick[joystickId].setXAxis(currentStep - 127);
Joystick[joystickId].setYAxis(-127);
}
else if (currentStep < 510)
{
Joystick[joystickId].setYAxis(currentStep - 255 - 127);
}
else if (currentStep < 765)
{
Joystick[joystickId].setXAxis(127 - (currentStep - 510));
}
else if (currentStep < 1020)
{
Joystick[joystickId].setYAxis(127 - (currentStep - 765));
}
else if (currentStep <= 1020 + 127)
{
Joystick[joystickId].setXAxis(currentStep - 1020 - 127);
Joystick[joystickId].setYAxis(currentStep - 1020 - 127);
}
}
void setup() {
if (testAutoSendMode)
{
Joystick[0].begin();
Joystick[1].begin();
}
else
{
Joystick[0].begin(false);
Joystick[1].begin(false);
}
pinMode(A0, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop() {
// System Disabled
if (digitalRead(A0) != 0)
{
digitalWrite(13, 0);
return;
}
// Turn indicator light on.
digitalWrite(13, 1);
if (millis() >= gNextTime)
{
if (gCurrentStep < 17)
{
gNextTime = millis() + gcButtonDelta;
testSingleButtonPush(gJoystickId, gCurrentStep);
}
else if (gCurrentStep < (17 + 4))
{
gNextTime = millis() + gcButtonDelta;
testMultiButtonPush(gJoystickId, gCurrentStep - 17);
}
else if (gCurrentStep < (17 + 4 + 1024 + 128))
{
gNextTime = millis() + gcAnalogDelta;
testXYAxis(gJoystickId, gCurrentStep - (17 + 4));
}
if (testAutoSendMode == false)
{
Joystick[gJoystickId].sendState();
}
gCurrentStep++;
if (gCurrentStep == (17 + 4 + 1024 + 128))
{
gNextTime = millis() + gcCycleDelta;
gCurrentStep = 0;
gJoystickId = (gJoystickId == 0 ? 1 : 0);
}
}
}
Danach wird der Arduino Leonardo auch als Gamecontroller erkannt.
Mach ich ein Rechts klick und gehe auf Gamecontrolereinstellungen auf den Controller,
werden mir auch 2 Arduinos angezeigt. Nur leider Funktionieren sie nicht.
Und jeder der hier fragt, braucht Hilfe.
Also ist das nicht neues.
Tausche deinen Titel durch eine aussagekrätige Information aus, die dein Problem zeigt. Dann findet man auch deinen Thread und du hast mehr Helfer.
was hast du gegen "Joystick.h" ? wieso bist du auf einen Testsketch gestoßen? gefällt dir nicht der im IDE Beispiel Menu ?
ok, nicht in Beispiel, hier:
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
// by Matthew Heironimus
// 2016-11-24
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
1, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
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);
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}
// Last state of the buttons
int lastButtonState[5] = {0,0,0,0,0};
void loop() {
// Read pin values
for (int index = 0; index < 5; index++)
{
int currentButtonState = !digitalRead(index + 2);
if (currentButtonState != lastButtonState[index])
{
switch (index) {
case 0: // UP
if (currentButtonState == 1) {
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
break;
case 1: // RIGHT
if (currentButtonState == 1) {
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
break;
case 2: // DOWN
if (currentButtonState == 1) {
Joystick.setYAxis(1);
} else {
Joystick.setYAxis(0);
}
break;
case 3: // LEFT
if (currentButtonState == 1) {
Joystick.setXAxis(-1);
} else {
Joystick.setXAxis(0);
}
break;
case 4: // FIRE
Joystick.setButton(0, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
}
}
delay(10);
}
man kann sehr einfach für deine Dual Joysticks adoptieren
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
// by Matthew Heironimus
// 2016-11-24
//--------------------------------------------------------------------
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
8, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
true, true, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
const byte Acht = 8;
const byte Pins[Acht] = {2, 3, 4, 5, 6, 7, Acht, 9}; // ich weiss nicht an welche Pins deine Taster angeschlossen sind
void setup() {
for (int i = 0; i < Acht; i++) pinMode(Pins[i], INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin(false);
Joystick.setXAxisRange(-512, 511);
Joystick.setYAxisRange(-512, 511);
Joystick.setRxAxisRange(-512, 511);
Joystick.setRyAxisRange(-512, 511);
}
void loop() {
Joystick.setXAxis(analogRead(A0)-512);
Joystick.setYAxis(analogRead(A1)-512);
Joystick.setRxAxis(analogRead(A2)-512);
Joystick.setRyAxis(analogRead(A3)-512);
// Read pin values
for (int i = 0; i < Acht; i++)Joystick.setButton(i, !digitalRead(Pins[i] ));
Joystick.sendState();
delay(20);
}