Forum: Mikrocontroller und Digitale Elektronik Arduino Sketch


von Stephan F. (bitfox)


Lesenswert?

Hallo zusammen,

ich habe mir einen Arduino Controller zugelegt. Es soll über die 
Bluetooth Verbindung ein PS4 Controller betrieben werden, der bei dem 
analogen Tastendruck (R2) den Output Kanal 0 auf High stellen soll. Ich 
habe einen Sketch gefunden in die ich die Funktion einbinden könnte. 
Habe die stellen fett gedruckt. Habt Nachsicht mit mir hab es schon 
Ewigkeiten nicht mehr gemacht. Könnte es so funktionieren? Bekomme den 
Compiler erst heute Abend deswegen frage ich hier mal. Vielen Dank für 
eure Unterstützng.
1
/*
2
 Example sketch for the PS4 Bluetooth library - developed by Kristian Lauszus
3
 For more information visit my blog: http://blog.tkjelectronics.dk/ or
4
 send me an e-mail:  kristianl@tkjelectronics.com
5
 */
6
7
#include <PS4BT.h>
8
#include <usbhub.h>
9
10
// Satisfy the IDE, which needs to see the include statment in the ino too.
11
#ifdef dobogusinclude
12
#include <spi4teensy3.h>
13
#endif
14
#include <SPI.h>
15
16
USB Usb;
17
//USBHub Hub1(&Usb); // Some dongles have a hub inside
18
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
19
20
/* You can create the instance of the PS4BT class in two ways */
21
// This will start an inquiry and then pair with the PS4 controller - you only have to do this once
22
// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in pairing mode
23
PS4BT PS4(&Btd, PAIR);
24
25
// After that you can simply create the instance like so and then press the PS button on the device
26
//PS4BT PS4(&Btd);
27
28
bool printAngle, printTouch;
29
uint8_t oldL2Value, oldR2Value;
30
31
*int R2 = 0*
32
33
void setup() {
34
 *pinmode (R2,output); //Kanal 0 auf output geschaltet*
35
  Serial.begin(115200);
36
#if !defined(__MIPSEL__)
37
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
38
#endif
39
  if (Usb.Init() == -1) {
40
    Serial.print(F("\r\nOSC did not start"));
41
    while (1); // Halt
42
  }
43
  Serial.print(F("\r\nPS4 Bluetooth Library Started"));
44
}
45
void loop() {
46
  Usb.Task();
47
48
  if (PS4.connected()) {
49
    if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) {
50
      Serial.print(F("\r\nLeftHatX: "));
51
      Serial.print(PS4.getAnalogHat(LeftHatX));
52
      Serial.print(F("\tLeftHatY: "));
53
      Serial.print(PS4.getAnalogHat(LeftHatY));
54
      Serial.print(F("\tRightHatX: "));
55
      Serial.print(PS4.getAnalogHat(RightHatX));
56
      Serial.print(F("\tRightHatY: "));
57
      Serial.print(PS4.getAnalogHat(RightHatY));
58
    }
59
60
    if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
61
      Serial.print(F("\r\nL2: "));
62
      Serial.print(PS4.getAnalogButton(L2));
63
      Serial.print(F("\tR2: "));
64
      Serial.print(PS4.getAnalogButton(R2));
65
      *PS4.getAnalogButton(R2)(R2,HIGH);//R2 Button setzt Kanal auf HIGH*
66
      *delay(100);*
67
      *PS4.getAnalogButton(R2)(R2,LOW);R2 Button setzt Kanal auf LOW*
68
    }
69
    if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
70
      PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
71
    oldL2Value = PS4.getAnalogButton(L2);
72
    oldR2Value = PS4.getAnalogButton(R2);
73
74
    if (PS4.getButtonClick(PS)) {
75
      Serial.print(F("\r\nPS"));
76
      PS4.disconnect();
77
    }
78
    else {
79
      if (PS4.getButtonClick(TRIANGLE)) {
80
        Serial.print(F("\r\nTraingle"));
81
        PS4.setRumbleOn(RumbleLow);
82
      }
83
      if (PS4.getButtonClick(CIRCLE)) {
84
        Serial.print(F("\r\nCircle"));
85
        PS4.setRumbleOn(RumbleHigh);
86
      }
87
      if (PS4.getButtonClick(CROSS)) {
88
        Serial.print(F("\r\nCross"));
89
        PS4.setLedFlash(10, 10); // Set it to blink rapidly
90
      }
91
      if (PS4.getButtonClick(SQUARE)) {
92
        Serial.print(F("\r\nSquare"));
93
        PS4.setLedFlash(0, 0); // Turn off blinking
94
      }
95
96
      if (PS4.getButtonClick(UP)) {
97
        Serial.print(F("\r\nUp"));
98
        PS4.setLed(Red);
99
      } if (PS4.getButtonClick(RIGHT)) {
100
        Serial.print(F("\r\nRight"));
101
        PS4.setLed(Blue);
102
      } if (PS4.getButtonClick(DOWN)) {
103
        Serial.print(F("\r\nDown"));
104
        PS4.setLed(Yellow);
105
      } if (PS4.getButtonClick(LEFT)) {
106
        Serial.print(F("\r\nLeft"));
107
        PS4.setLed(Green);
108
      }
109
110
      if (PS4.getButtonClick(L1))
111
        Serial.print(F("\r\nL1"));
112
      if (PS4.getButtonClick(L3))
113
        Serial.print(F("\r\nL3"));
114
      if (PS4.getButtonClick(R1))
115
        Serial.print(F("\r\nR1"));
116
      if (PS4.getButtonClick(R3))
117
        Serial.print(F("\r\nR3"));
118
119
      if (PS4.getButtonClick(SHARE))
120
        Serial.print(F("\r\nShare"));
121
      if (PS4.getButtonClick(OPTIONS)) {
122
        Serial.print(F("\r\nOptions"));
123
        printAngle = !printAngle;
124
      }
125
      if (PS4.getButtonClick(TOUCHPAD)) {
126
        Serial.print(F("\r\nTouchpad"));
127
        printTouch = !printTouch;
128
      }
129
130
      if (printAngle) { // Print angle calculated using the accelerometer only
131
        Serial.print(F("\r\nPitch: "));
132
        Serial.print(PS4.getAngle(Pitch));
133
        Serial.print(F("\tRoll: "));
134
        Serial.print(PS4.getAngle(Roll));
135
      }
136
137
      if (printTouch) { // Print the x, y coordinates of the touchpad
138
        if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
139
          Serial.print(F("\r\n"));
140
        for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
141
          if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
142
            Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
143
            Serial.print(PS4.getX(i));
144
            Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
145
            Serial.print(PS4.getY(i));
146
            Serial.print(F("\t"));
147
          }
148
        }
149
      }
150
    }
151
  }
152
}

--

Quelltext wird lesbarer, wenn er in [ C ] [ /C ] -Tags gepostet wird.

Langen Quelltext bitte als Dateianhang posten.

-rufus

: Bearbeitet durch User
von Stephan F. (bitfox)


Lesenswert?

Jetzt hat es nicht geklappt mit dem fett Druck. Die Änderungen sind in 
Sternchen gefasst.

Beitrag #5203575 wurde von einem Moderator gelöscht.
von Stephan F. (bitfox)


Lesenswert?

Dann versuch ich es mal hier.

Hallo zusammen,

ich habe mir einen Arduino Controller zugelegt. Es soll über die 
Bluetooth Verbindung ein PS4 Controller betrieben werden, der bei dem 
analogen Tastendruck (R2) den Output Kanal 0 auf High stellen soll. Ich 
habe einen Sketch gefunden in die ich die Funktion einbinden könnte. 
Habe die stellen fett gedruckt. Habt Nachsicht mit mir hab es schon 
Ewigkeiten nicht mehr gemacht. Könnte es so funktionieren? Bekomme den 
Compiler erst heute Abend deswegen frage ich hier mal. Vielen Dank für 
eure Unterstützng.

/*
 Example sketch for the PS4 Bluetooth library - developed by Kristian 
Lauszus
 For more information visit my blog: http://blog.tkjelectronics.dk/ or
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <PS4BT.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino 
too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like 
so

/* You can create the instance of the PS4BT class in two ways */
// This will start an inquiry and then pair with the PS4 controller - 
you only have to do this once
// You will need to hold down the PS and Share button at the same time, 
the PS4 controller will then start to blink rapidly indicating that it 
is in pairing mode
PS4BT PS4(&Btd, PAIR);

// After that you can simply create the instance like so and then press 
the PS button on the device
//PS4BT PS4(&Btd);

bool printAngle, printTouch;
uint8_t oldL2Value, oldR2Value;

*int R2 = 0*

void setup() {
 *pinmode (R2,output); //Kanal 0 auf output geschaltet*
  Serial.begin(115200);
#if !defined(_MIPSEL_)
  while (!Serial); // Wait for serial port to connect - used on 
Leonardo, Teensy and other boards with built-in USB CDC serial 
connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); // Halt
  }
  Serial.print(F("\r\nPS4 Bluetooth Library Started"));
}
void loop() {
  Usb.Task();

  if (PS4.connected()) {
    if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 
117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 
117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) 
< 117 || PS4.getAnalogHat(RightHatY) > 137 || 
PS4.getAnalogHat(RightHatY) < 117) {
      Serial.print(F("\r\nLeftHatX: "));
      Serial.print(PS4.getAnalogHat(LeftHatX));
      Serial.print(F("\tLeftHatY: "));
      Serial.print(PS4.getAnalogHat(LeftHatY));
      Serial.print(F("\tRightHatX: "));
      Serial.print(PS4.getAnalogHat(RightHatX));
      Serial.print(F("\tRightHatY: "));
      Serial.print(PS4.getAnalogHat(RightHatY));
    }

    if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These 
are the only analog buttons on the PS4 controller
      Serial.print(F("\r\nL2: "));
      Serial.print(PS4.getAnalogButton(L2));
      Serial.print(F("\tR2: "));
      Serial.print(PS4.getAnalogButton(R2));
      *PS4.getAnalogButton(R2)(R2,HIGH);//R2 Button setzt Kanal auf 
HIGH*
      *delay(100);*
      *PS4.getAnalogButton(R2)(R2,LOW);R2 Button setzt Kanal auf LOW*
    }
    if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) 
!= oldR2Value) // Only write value if it's different
      PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
    oldL2Value = PS4.getAnalogButton(L2);
    oldR2Value = PS4.getAnalogButton(R2);

    if (PS4.getButtonClick(PS)) {
      Serial.print(F("\r\nPS"));
      PS4.disconnect();
    }
    else {
      if (PS4.getButtonClick(TRIANGLE)) {
        Serial.print(F("\r\nTraingle"));
        PS4.setRumbleOn(RumbleLow);
      }
      if (PS4.getButtonClick(CIRCLE)) {
        Serial.print(F("\r\nCircle"));
        PS4.setRumbleOn(RumbleHigh);
      }
      if (PS4.getButtonClick(CROSS)) {
        Serial.print(F("\r\nCross"));
        PS4.setLedFlash(10, 10); // Set it to blink rapidly
      }
      if (PS4.getButtonClick(SQUARE)) {
        Serial.print(F("\r\nSquare"));
        PS4.setLedFlash(0, 0); // Turn off blinking
      }

      if (PS4.getButtonClick(UP)) {
        Serial.print(F("\r\nUp"));
        PS4.setLed(Red);
      } if (PS4.getButtonClick(RIGHT)) {
        Serial.print(F("\r\nRight"));
        PS4.setLed(Blue);
      } if (PS4.getButtonClick(DOWN)) {
        Serial.print(F("\r\nDown"));
        PS4.setLed(Yellow);
      } if (PS4.getButtonClick(LEFT)) {
        Serial.print(F("\r\nLeft"));
        PS4.setLed(Green);
      }

      if (PS4.getButtonClick(L1))
        Serial.print(F("\r\nL1"));
      if (PS4.getButtonClick(L3))
        Serial.print(F("\r\nL3"));
      if (PS4.getButtonClick(R1))
        Serial.print(F("\r\nR1"));
      if (PS4.getButtonClick(R3))
        Serial.print(F("\r\nR3"));

      if (PS4.getButtonClick(SHARE))
        Serial.print(F("\r\nShare"));
      if (PS4.getButtonClick(OPTIONS)) {
        Serial.print(F("\r\nOptions"));
        printAngle = !printAngle;
      }
      if (PS4.getButtonClick(TOUCHPAD)) {
        Serial.print(F("\r\nTouchpad"));
        printTouch = !printTouch;
      }

      if (printAngle) { // Print angle calculated using the 
accelerometer only
        Serial.print(F("\r\nPitch: "));
        Serial.print(PS4.getAngle(Pitch));
        Serial.print(F("\tRoll: "));
        Serial.print(PS4.getAngle(Roll));
      }

      if (printTouch) { // Print the x, y coordinates of the touchpad
        if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and 
carriage return if any of the fingers are touching the touchpad
          Serial.print(F("\r\n"));
        for (uint8_t i = 0; i < 2; i++) { // The touchpad track two 
fingers
          if (PS4.isTouching(i)) { // Print the position of the finger 
if it is touching the touchpad
            Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": 
"));
            Serial.print(PS4.getX(i));
            Serial.print(F("\tY")); Serial.print(i + 1); 
Serial.print(F(": "));
            Serial.print(PS4.getY(i));
            Serial.print(F("\t"));
          }
        }
      }
    }
  }
}

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

Das mit dem "fett" kannst Du in Quelltext so nicht machen.

Schreib' jewwils 'ne Kommentarzeile davor.

von Stephan F. (bitfox)


Lesenswert?

int lagSwi = 0;

void setup() {
  pinMode(lagSwi, OUTPUT);

void loop() {
  Usb.Task();

    if PS4.getAnalogButton(R2) > 10;
       digitalWrite(lagSwi, HIGH);
       delay (100);
    else
       digitalWrite(lagSwi, LOW);
    }

Nehmen wir an PS4.getAnalogButton(R2) gibt ein Analogsignal aus. Kann 
ich es wie oben programmieren, dass ich den Ausgang Kanal 0 auf HIGH 
setze ab dem Wert 10?

von Stefan F. (Gast)


Lesenswert?

> Kann ich es wie oben programmieren

Da fehlen Klammern:
1
if (PS4.getAnalogButton(R2) > 10)
2
{
3
    digitalWrite(lagSwi, HIGH);
4
    delay(100);
5
}
6
else
7
{
8
    digitalWrite(lagSwi, LOW);
9
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.