Dual MC33926 Shield for UNO with USB Host Shield

Continuing the discussion from Maestro serial control with Arduino & USB Shield:

After remapping some pins on both the MC33926 shield as well as the USB Host Shield, I have been progressing quite well with my goal of controlling a robot with a wired Xbox controller. The hardware seems to be under control, but now I have hit what I think is a software related snag.

To break it down, here is an excerpt from the sample code from the USB Host Shield Library for controlling the xbox. Note that there is more to loop() than I have reproduced here, but I have cut it for brevity/relevance.

/*
 Example sketch for the Xbox 360 USB library - developed by Kristian Lauszus
 */

#include <XBOXUSB.h>

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

USB Usb;
XBOXUSB Xbox(&Usb);

void setup() {
  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"));
}
void loop() {
  Usb.Task();
  if (Xbox.Xbox360Connected) {
    if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
      Serial.print("L2: ");
      Serial.print(Xbox.getButtonPress(L2));
      Serial.print("\tR2: ");
      Serial.println(Xbox.getButtonPress(R2));
      Xbox.setRumbleOn(Xbox.getButtonPress(L2), Xbox.getButtonPress(R2));
    } else
      Xbox.setRumbleOn(0, 0);
 } 
} 

With the above code, if I hold down either trigger, the rumble is activated, and the controller shakes until I release the trigger. I tried to modify this code to control motors1 and 2 with the left and right triggers, respectively. It works, but only briefly. After about three seconds of continuously holding in the triggers, the speed gets locked in. Even if I release the triggers, the motors continue to move. Could this be an issue with the timer? Or maybe I need to add in a longer delay? The troublesome portion of the code follows below.

#include <XBOXUSB.h>

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

USB Usb;
XBOXUSB Xbox(&Usb);

#include "DualMC33926MotorShield.h"
#include <PololuMaestro.h>
#include <SoftwareSerial.h>

SoftwareSerial maestroSerial(16,17);

DualMC33926MotorShield md;
MicroMaestro maestro(maestroSerial);

long int XboxLT;
long int XboxRT;

void setup() {
  Serial.begin(9600);
  maestroSerial.begin(9600); 
  md.init(); 
  md.setM1Speed(0); 
  md.setM2Speed(0); 
  #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"));
}
 
void loop() { 
  
  Usb.Task();
  if (Xbox.Xbox360Connected) {
     if (Xbox.getButtonPress(L2) || Xbox.getButtonPress(R2)) {
      Serial.print("\tL2:");
      Serial.print(Xbox.getButtonPress(L2));
      XboxLT = map((Xbox.getButtonPress(L2)), 0, 255, 0, 300);
      md.setM2Speed(XboxLT);    
      Serial.print("\tXboxLT:");
      Serial.print(XboxLT);
      
      Serial.print("\tR2:");
      Serial.print(Xbox.getButtonPress(R2));
      XboxRT = map((Xbox.getButtonPress(R2)), 0, 255, 0, 300);
      md.setM1Speed(XboxRT);    
      Serial.print("\tXboxRT:");
      Serial.print(XboxRT);

      Serial.println();
      delay(50);
    }else
      md.setM1Speed(0); 
      md.setM2Speed(0); 
}
}

Thanks!
Mike

Hello, Mike.

I moved your thread to the " Software and Microcontrollers " subforum, which seems more appropriate.

I am not very familiar with the XBOXUSB library, and I do not know if the problem is caused by a timing issue. I briefly looked at your code and noticed in loop() that you are trying to stop both motors (by setting them to speed 0) when the left and right triggers (L2 and R2) are not being pressed. If that is the case, you probably will need to enclose md.setM1Speed(0) and md.setM2Speed(0) in curly braces. Other than that, the code looks fine to me.

When the issue occurs, I recommend verifying that your main loop is running, that Xbox.Xbox360Connected is still returning true, and looking at what is being returned from Xbox.getButtonPress() at each point where the function is called in your program. You might consider simplifying your code to make sure that one trigger works before implementing both.

- Amanda

Thanks for the great suggestions Amanda! I will test them out and post my results!

Turns out the delay was causing the problems. Apparently there was no reason for me to have put a delay there in the first place, as the USB device needs to be read continuously. I removed the delays and now all is well. Sources: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay and https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing.

Thanks again!

Great! I am glad you figured out the issue and fixed it; thanks for letting us know.

- Amanda