USB Host \ Digital Pot SPI Issue

I’m not usually one who goes asking for help right away on an issue and I feel bad asking since this seems like a basic question.

I am running an UNO with the USB Host shield (controlling a xbox 360 wireless receiver) from Circuits@Home and a digital potentiometer MCP4131. They both communicate with SPI.

I am able to get the digital pot to work just fine in its own sketch. I am able to get the xbox 360 controller to print out the button presses to the serial monitor in its own sketch. When I combine the two I am only able to ever get one to work at a time. In my original sketch the controller worked. So I wrote a simple one and now only the digital pot works.

Thoughts?

The usb host has a select pin of 10, and the digital pot is on 6.

Thanks for your help in advance!

#include <SPI.h>
#include <XBOXRECV.h>;

//Setup USB devices
USB Usb;
XBOXRECV Xbox(&Usb);


int potAddress = 0;
int potCS= 6;

void setup()
{
//SPI.begin();
    Serial.begin(115200);
      if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
  pinMode (potCS, OUTPUT);
digitalWrite(potCS, HIGH);
}

void loop()
{

Usb.Task();


//Get XBOX controller signals
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));
        }
      }
    }
}

//Raise and lower potentiometer signal          
for (int i = 0; i <= 128; i++)
{
  digitalPotWrite(i);
  delay(10);
}
delay(100);
for (int i = 128; i >= 0; i--)
{
  digitalPotWrite(i);
  delay(10);
}
}

void digitalPotWrite(int value)
{
digitalWrite(10, HIGH);
delay(10);
digitalWrite(potCS, LOW);
SPI.transfer(potAddress);
SPI.transfer(value);
digitalWrite(potCS, HIGH);
digitalWrite(10, LOW);
delay(100);
}

I know the code above is messy. Sorry about that.

I think the usb host shield\xbox 360 controller wasn't working because it needs to be refreshed at a fairly quick pace? I played around with some working controller code and adding in about 1 second delays caused it to act sporadically.

Now I am working on a different test bit with known good usb\xbox code to get the digital potentiometer to brighten\dim an led at 1 second intervals using millis. Still can't get the digital pot and usbhost\xbox to work at the same time.

:angry:

The application for this is to drive a speed controller which uses a 5v signal and potentiometer to control motor speed.

I finally got it to work, so I made some code to map the L2 trigger to a value to send to the potentiometer.

I don't see any noticeable differences between this and my prior sketches. Any one have thoughts on this? It seems like as I add features to the sketch it breaks.

I don't see any noticeable differences between this and my prior sketches.

We can only see the prior sketches.

Any one have thoughts on this?

Yes.

It seems like as I add features to the sketch it breaks.

Hmmm...

Whoops. Looks like I forgot to add the code that works. My bad!

#include <XBOXRECV.h>

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

//Setup USB and XBOX
USB Usb;
XBOXRECV Xbox(&Usb);

//Value to send to pot from L2 trigger on controller
int potvalue = 0;

void setup() {

  //Set pot pin
  pinMode (6, OUTPUT);
  digitalWrite(6, HIGH);

  //Rest of setup code taken from xboxrecv.h example
  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 Wireless Receiver Library Started"));
}
void loop() {

  //get xbox presses
  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));
        }
        Serial.println();
      }
    }
  }

//Raise and lower potentiometer signal    
potvalue = Xbox.getButtonPress(L2,0);
digitalPotWrite(potvalue/2);
}

//Send signal to pot
void digitalPotWrite(int value)
{
  Serial.print(value);
  digitalWrite(10, HIGH);
  digitalWrite(6, LOW);
  delay(1);
  SPI.transfer(0);
  SPI.transfer(value);
  digitalWrite(6, HIGH);
  digitalWrite(10, LOW);
}

Ok so here are my thoughts\findings in case anyone in the future has a similar issue.

Remove SPI.begin and include SPI.H (That was my first step, and didn't fix it)

Pin mode setup must be before Serial.begin (This was a critical step that I found by accident)

The USB Host Shield and or the xbox wireless receiver needs to be read\written to within a certain interval. If the time between the last reading and the current reading is too long, the system becomes unstable. I'm not sure why. (This was part of my initial thought, but didn't prove until I added a few high delays in the code)

It was a combination of the above that I believe caused my issues. This is my first step into arduino\hardware so my know-how won't let me prove it out technically, but I think its accurate.