Hallo,
Und zwar möchte ich mittels einem Xbox Controller den Tasten RT (R2) und LT (L2) jeweils 2 Motoren steuern.
Jedoch klappt das nicht so ganz. Es dreht sich immer nur ein Motor und der andere Moto gibt nur Quitschende Geräusche von sich.
Beide Motoren sind mittel L293D (H-Bridge) mit dem Arduino verbunden. Der Arduino ist am PC angeschlossen und der L293D wird mit 5V von einem Netzteil mit Strom versorgt.
Hoffe ihr habt irgendwelche Tips oder Ratschläge parat.
#include <XBOXRECV.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
USB Usb;
XBOXRECV Xbox(&Usb);
int motor1_A=11;
int motor1_B=10;
int motor1_Speed=9;
int motor2_A=5;
int motor2_B=4;
int motor2_Speed=3;
int a=0;
int b=0;
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
pinMode(motor1_A,OUTPUT);
pinMode(motor1_B,OUTPUT);
pinMode(motor2_A,OUTPUT);
pinMode(motor2_B,OUTPUT);
}
Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
}
void loop() {
Usb.Task();
if (Xbox.XboxReceiverConnected) {
for (uint8_t i = 0; i < 4; i++) {
if (Xbox.Xbox360Connected[i]) {
if (Xbox.getButtonPress(L2, i) && Xbox.getButtonPress(R2, i)) {
Serial.print("L2: ");
Serial.print(Xbox.getButtonPress(L2, i));
Serial.print("\tR2: ");
Serial.println(Xbox.getButtonPress(R2, i));
//Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i);
a = 254;
b = 254;
}
else if (Xbox.getButtonPress(L2, i) < Xbox.getButtonPress(R2, i)){
a = 254;
b = 0;
}
else if (Xbox.getButtonPress(L2, i) > Xbox.getButtonPress(R2, i)){
// motor1
a = 0;
b = 254;
}
else{
// motor1
a=0;
b=0;
}
if (Xbox.getButtonClick(A, i))
Serial.println(F("A"));
}
}
}
// motor1
digitalWrite(motor1_A,HIGH); // A = HIGH and B = LOW means the motor will turn right
digitalWrite(motor1_B,LOW);
analogWrite(motor1_Speed,a); // speed counts from 0 to 255
// motor2
digitalWrite(motor2_A,HIGH); // A = HIGH and B = LOW means the motor will turn right
digitalWrite(motor2_B,LOW);
analogWrite(motor2_Speed,b); // speed counts from 0 to 255
}