Joystick schalter für pc

hallo ich möchte eine mehrfachschalter so schalten das er von aus (stufe 0) an (stufe1) an (stufe2(stufe1 ist auch an)) beim zurück schalten soll er von (stufe 2 an) auf aus (stufe1 ist noch an ) von (stufe 1 an) auf aus

ich hoffen man versteht was ich mein es soll für eine blinkerschalter sein dem man am pc nutzen kann als joystick es geht schon alles nur das wenn ich das licht im spiel aus schalten will erst wieder aus und dann wieder einschalten muss am joystick

ich arbeite mit <Joystick.h>

ich nutze einen Arduino Leonardo

#include <Joystick.h>

const uint8_t buttonCount = 13;
Joystick_ controller(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, buttonCount,
                     0, true, true, false,
                     false, false, false,
                     false, false, false,
                     false, false);
int const BTN_A_PIN = 1;
int const BTN_B_PIN = 2;
int const BTN_C_PIN = 3;
int const BTN_D_PIN = 4;
int const BTN_E_PIN = 5;
int const BTN_F_PIN = 6;
int const BTN_G_PIN = 7;
int const BTN_K_PIN = 8;
int const BTN_I_PIN = 9;
int const BTN_J_PIN = 10;
int const BTN_O_PIN = 11;
int const BTN_P_PIN = 12;
int const BTN_W_PIN = 13;
int const AXIS_X_PIN = A0;
int const AXIS_Y_PIN = A1;

int const buttonPins[buttonCount] = {
  BTN_A_PIN,
  BTN_B_PIN,
  BTN_C_PIN,
  BTN_D_PIN,
  BTN_E_PIN,
  BTN_F_PIN,
  BTN_G_PIN,
  BTN_K_PIN,
  BTN_I_PIN,
  BTN_J_PIN,
  BTN_O_PIN,
  BTN_P_PIN,
  BTN_W_PIN
};

 
  

int lastButtonValue[buttonCount];
int lastXAxisValue = -1;
int lastYAxisValue = -1;


void setup()

{
  controller.setYAxisRange(0, 1023);
  controller.setYAxisRange(1023, 0);
  controller.begin(false);

  for (int i = 0; i < buttonCount; i++)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
    lastButtonValue[i] = -1;
  }

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
  bool sendUpdate = false;
  for (int i = 0; i < buttonCount; i++)
  {
    const int buttonValue = digitalRead(buttonPins[i]);

    if (buttonValue != lastButtonValue[i])
    {
      controller.setButton(i, !buttonValue);
      lastButtonValue[i] = buttonValue;
      sendUpdate = true;
    }
  }

  const int currentXAxisValue = analogRead(AXIS_X_PIN);
  if (currentXAxisValue != lastXAxisValue)
  {
    controller.setXAxis(currentXAxisValue);
    lastXAxisValue = currentXAxisValue;
    sendUpdate = true;
  }

  const int currentYAxisValue = analogRead(AXIS_Y_PIN);
  if (currentYAxisValue != lastYAxisValue)
  {
    controller.setYAxis(currentYAxisValue);
    lastYAxisValue = currentYAxisValue;
    sendUpdate = true;
  }

  if (sendUpdate)
  {
    controller.sendState();
  }
  delay(50);
}

ich hoffe ihr könnt mir helfen lg Jens

Setze Deinen Sketch bitte in Codetags. Wie das geht, steht hier.
Außerdem formatiere den Code bitte ordentlich. Strg+T in der IDE hilft Dir dabei.
Das kannst Du auch noch nachträglich ändern.

Gruß Tommy

is der Sketch von KI generiert?

welche von

gehören zum Mehrfachschalter?

Um das Verständnis zu erhöhen: Was macht der JoyStick? Was machen die 13 Buttons? Was sind deine Ausgänge? Wie soll das zurückschalten passieren?

Bitte kommentiere mal deinen Code, so dass man sieht, was für was da ist.

du meinst wohl Standlicht soll an bleiben während Abblendlicht eingeschaltet wird, denn beim Blinker gibt es nichts was mitgeschaltet werden soll.

nein es ist ein fertiger den ich aus dem netz hab der was die joystick.h gemacht hat

lg

nein sind alles eigeneschalter und auch der mehrfach schalter dabei

ja genau

ich brauch es nur für 4 pins welche sind egal sind noch nicht definiert lg jens

Poste mal einen Schaltplan.

Finde ich voll ok (nicht nur, weil ich auch sowas tue)
Denn dann kann sowas einem nicht ins Essen spucken, selbst wenn alle Warnungen deaktiviert sind

if(buttonValue=HIGH) tuwas();

// oder
buttonValue=42;

Ist das ein Schalter oder ein Taster?


Nimm einfach so was + eine Diode und du bist fertig.

Da brauchst du keinen Joystick und keinen µC.

#include <Joystick.h>

const uint8_t buttonCount = 13;
Joystick_ controller(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, buttonCount,
                     0, true, true, false,
                     false, false, false,
                     false, false, false,
                     false, false);
byte const AXIS_X_PIN = A0;
byte const AXIS_Y_PIN = A1;

byte const buttonPins[buttonCount] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};//Pin 13 ist Schalter2, Pin 12 - Schalter1

byte lastButtonValue[buttonCount];
int lastXAxisValue = -1;
int lastYAxisValue = -1;

void setup() {
  controller.setYAxisRange(0, 1023);
  controller.setYAxisRange(1023, 0);
  controller.begin(false);

  for (int i = 0; i < buttonCount; i++)  {
    pinMode(buttonPins[i], INPUT_PULLUP);
    lastButtonValue[i] = -1;
  }

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  bool sendUpdate = false;
  for (int i = 0; i < buttonCount; i++) {
    bool buttonValue = digitalRead(buttonPins[i])==LOW;
    if (buttonValue != lastButtonValue[i]) {
      controller.setButton(i, buttonValue);
      lastButtonValue[i] = buttonValue;
      if (i == 12 && buttonValue)controller.setButton(11, true); // wenn Pin13(entspricht Joysticks Taste12) ist aktiviert (LOW) dann Joysticks Taste11 soll auch mit aktiviert werden
      sendUpdate = true;
    }

  }

  int currentXAxisValue = analogRead(AXIS_X_PIN);
  if (currentXAxisValue != lastXAxisValue) {
    controller.setXAxis(currentXAxisValue);
    lastXAxisValue = currentXAxisValue;
    sendUpdate = true;
  }

  int currentYAxisValue = analogRead(AXIS_Y_PIN);
  if (currentYAxisValue != lastYAxisValue) {
    controller.setYAxis(currentYAxisValue);
    lastYAxisValue = currentYAxisValue;
    sendUpdate = true;
  }

  if (sendUpdate) controller.sendState();
  delay(50);
}

Falls das ein 8-Bit-AVR ist (der MC wurde noch nicht genannt?), ist könnte Pin 1 (=TX) tödlich für den MC sin, wenn daran ein Taster/Schalter hängt (egal ob nach GND oder 5V).

Gruß Tommy

ich habe eine Arduino Leonardo

Ok, da ist 1 nicht TX.
Infos über den MC gehören ins Eröffnungsposting.

Gruß Tommy

vielen dank aber das funktioniert leider nicht der schalter hast 4 kabel 2+ 2- hab das jetzt bei pin 12 und 13 und grd angeschlossen oder hab ich was falsch gemacht lg jen

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.