So it feels like I almost got it to work. But not quite. I did some changes to my Setup. I learned that it should be possible for multiple devices to connect to the Arduino over SPI. All devices can share the SCK, MOSI and MISO pins. But they each need to have there own SS pin. That's what I did.
Here is the test code I'm using:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <XBOXUSB.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#define CE_PIN_RADIO 7 //CE : set high to receive wireless data, low to send
#define CSN_PIN_RADIO 8 //Same as SS: SPI enable pin, set low to listen to SPI bus.
//SS Pin USB needs to stay standard (PIN D10) I don't know how to change it
#define SS_PIN_USB 10
int i = 0;
const uint64_t pipe = 0x1; // This is the transmit pipeline
int sendData[1]; // One element array holding our random number
RF24 radio(CE_PIN_RADIO, CSN_PIN_RADIO); // Activate the Radio
USB Usb;
XBOXUSB Xbox(&Usb);
void setup()
{
//pinMode(CE_PIN_RADIO, OUTPUT);
//pinMode(CSN_PIN_RADIO, OUTPUT);
//pinMode(SS_PIN_USB, OUTPUT);
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\nXBOX USB Library Started"));
//Start Radio after USB
radio.begin();
radio.openWritingPipe(pipe);
}
void loop()
{
Usb.Task();
if (Xbox.Xbox360Connected) {
Serial.println("XBOX");
if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
Serial.print("L2: ");
Serial.print(Xbox.getButtonPress(L2));
Serial.print("\tR2: ");
Serial.println(Xbox.getButtonPress(R2));
sendData[0] = Xbox.getButtonPress(R2);
Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2));
} else
Xbox.setRumbleOn(0, 0);
}
//sendData[0] = i++;
digitalWrite(CSN_PIN_RADIO, LOW);
radio.write( sendData, sizeof(sendData)); //Nach dem Write Befehl wird CSN wieder auf HIGH gesetzt (siehe RF24.cpp Zeile 98)
digitalWrite(CSN_PIN_RADIO, HIGH);
Serial.println(sendData[0]);
delay(1);
}
I also have connected a XBOX 360 Controller to the USB Shield.
The sender side COM shows this:
XBOX USB Library Started0
0
XBOX
0
XBOX
0
L2: 0 R2: 2
2
XBOX
L2: 0 R2: 179
179
XBOX
L2: 0 R2: 255
and so on
So it seems to see the USB devise and that it seems to work as intended.
BUT it also gets stuck sometimes... The COM only shows one stuck value and the controller keeps rumbleing. Here as an example:
XBOX
L2: 0 R2: 9
9
XBOX
L2: 0 R2: 9
9
XBOX
L2: 0 R2: 9
I really don't know why...
The receiving side COM just shows the correctly send data.
0
0
0
0
0
2
179
255
255
255
255
and so on
Soooo I have a few questions about this:
- In void setup(){} I had to write the radio.begin(); code last. Otherwise the radio wouldn't work. Why though??
- Why does it sometimes work, and sometimes get stuck?
- I feel like my code is still pretty hackey... how can I improve it?