Arudino, USB Host Shield, and Motor Shield Issues

Hi, I have an Arduino Duemilanove, a sainsmart USB Host Shield, and a ladyada motor shield. I am running some code listed below, and I am having an issue with the back left wheel continuing to spin backward after I release the left joystick on the PS3 controller. I believe the issue is with the pins. I have discussed this with someone over at adafruit, and they believe the issue is with pin 12. How can I suppress this pin or get it to stop interacting with the motor shield?

#include <PS3USB.h>
#include <AFMotor.h> 

USB Usb;
PS3USB PS3(&Usb);

AF_DCMotor g_motor1(1, MOTOR12_8KHZ); 
AF_DCMotor g_motor2(2, MOTOR12_8KHZ);
AF_DCMotor g_motor3(3, MOTOR12_8KHZ); 
AF_DCMotor g_motor4(4, MOTOR12_8KHZ); 

//y = PS3.getAnalogHat(LeftHatY);
//x = PS3.getAnalogHat(LeftHatX);
void setup() {
  Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }  
  Serial.print(F("\r\nPS3 USB Library Started"));
}

void loop() {
  Usb.Task();

  if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
    //print X and Y values on serial monitor
      Serial.print(F("\r\nLeftHatX: ")); 
      Serial.print(PS3.getAnalogHat(LeftHatX));
      Serial.print(F("\tLeftHatY: ")); 
      Serial.print(PS3.getAnalogHat(LeftHatY));
      
    /*int direction = PS3.getAnalogHat(LeftHatY);
    direction < 120 = 1;
    direction > 135 = 2;
    switch (direction) {
    case 1:
      g_motor1.run(FORWARD); 
      g_motor2.run(FORWARD); 
      g_motor3.run(FORWARD); 
      g_motor4.run(FORWARD);
      break;
    case 2:
      g_motor1.run(BACKWARD); 
      g_motor2.run(BACKWARD); 
      g_motor3.run(BACKWARD); 
      g_motor4.run(BACKWARD);
      break;
    }*/

    //set motor directions as forward for all Y < 120
    if(PS3.getAnalogHat(LeftHatY) < 120) {
      g_motor1.run(FORWARD); 
      g_motor2.run(FORWARD); 
      g_motor3.run(FORWARD); 
      g_motor4.run(FORWARD);
    }
    //set motor directions as backward for all Y > 135
    if(PS3.getAnalogHat(LeftHatY) > 135) {
      g_motor1.run(BACKWARD); 
      g_motor2.run(BACKWARD); 
      g_motor3.run(BACKWARD); 
      g_motor4.run(BACKWARD);
    }
    //set speeds for when the joystick is pushed directly up
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125); 
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125);
    }
    //set speeds for when the joystick is pushed directly down
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125); 
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
    }
    //set speeds for when joystick is pushed directly left
    if(PS3.getAnalogHat(LeftHatX) < 120 && PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135) {
      g_motor1.setSpeed(0);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-120)*-2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-120)*-2.125); 
      g_motor4.setSpeed(0);
    }
    //set speeds for when joystick is pushed directly right
    if(PS3.getAnalogHat(LeftHatX) > 135 && PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX)-135)*2.125);
      g_motor2.setSpeed(0);
      g_motor3.setSpeed(0); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX)-135)*2.125);
    }
    
    //set speeds for when joystick is pushed up and right
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) > 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-255)*-2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-255)*-2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125);
    }
    //set speeds for when joystick is pushed up and left
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) < 120) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
    }
    //set speeds for when joystick is pushed down and left
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) < 120) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
    }
    //set speeds for when joystick is pushed down and right
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) > 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-255)*-2.125); 
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-255)*-2.125); 
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
    }
    
    //stopping
    if(PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed(0); 
      g_motor2.setSpeed(0); 
      g_motor3.setSpeed(0); 
      g_motor4.setSpeed(0);
    }
  }
}

Parts
Arduino Thread
https://www.adafruit.com/forums/viewtopic.php?f=31&t=29313

USB Host Shield

Motor Shield
http://www.ladyada.net/make/mshield/index.html

direction < 120 = 1;

Hmmm.

Can you please elaborate on what you mean? Is there something wrong with that line of code or something?

What do you think that the aforementioned line of code does? (Although, interestingly it is commented out)

Can I make the suggestion that you change this:

void loop() {
  Usb.Task();

  if(PS3.PS3Connected || PS3.PS3NavigationConnected) {

To this:

void loop() {
  Usb.Task();

  if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
    y = PS3.getAnalogHat(LeftHatY);
    x = PS3.getAnalogHat(LeftHatX);

And then in all of the following if() statements you change "PS3.getAnalogHat(LeftHatY)" to be "Y", and "PS3.getAnalogHat(LeftHatX)" to be "X". It will make your code much faster and easier to follow. Plus it will make sure that if the analog value changes while running through the if() logic that you don't get strange results.

Well, I have already tried the code when it was uncommented and I got the following error on that code line that you mentioned

value required as left operand of assignment

and on the next line with direction > 135 = 2;

value required as left operand of assignment

and it will not let me upload it to the arduino due to these two errors.

I know that it won't compile/upload, I can see by just looking at it, but I am interested to know what you thought it would do as it will help us to understand what you were trying to do. EDIT: But actually looking at what you have done with the if statements following, I've worked out what you were trying to do, so never mind.

well if the joystick is moved up, then all four motors will move forward, and if it is case 2, then all four motors will move backward.

I think the problem you have is that the two shields cannot be used together on an Arduino Uno/Duemilanove. The host shield requires SPI, which is on pins 11-13, and pin 10 or pin 9. Three of those 4 pins are being used by the motor shield.

You can't use them together unless you hack apart the motor shield and remap some of the pins to analog pins. But even if you do that you lose some PWM functionality which will probably render the motor shield useless.

It could be done with an Arduino Mega as that has the SPI pins on 50-53 which the host shield has access to via the ISP header. You would then only have to remap the SS pin of the host shield to pin 13 or pin 2.

Could I replace the USB shield with the WiFi shield and then issue commands from a computer which has a connected PS3 controller?

It's probably a good idea to keep this URL handy when trying to decide on which shields work together and which ones don't (without modifications). http://shieldlist.org/

I have checked that, but the issue is that the shields I have are not listed on this website. So here is my question, can I replace the usb host shield with the wifi shield, so the arrangement looks like this below

1st level (closest to robot)- Arduino Uno 2nd Level-Wifi Shield- 3rd Level-Motor Shield

Could I then connect the PS3 controller to my computer and then send commands over wifi?

If you mean an Arduino Wifi Sheild, No. It too uses SPI.

One option would be an xBee shield which uses Serial, so connects to D0 and D1. You would have no conflicts with that.

So I think I would like to just change out the motor shield, instead of using an xbee shield. Do you know of any shields that work with 4WD DC motors and support a USB shield and WiFi shield?

I will use the xbee shield as an absolute final opportunity.

jashtiani:
Hi, I have an Arduino Duemilanove, a sainsmart USB Host Shield, and a ladyada motor shield. I am running some code listed below, and I am having an issue with the back left wheel continuing to spin backward after I release the left joystick on the PS3 controller. I believe the issue is with the pins. I have discussed this with someone over at adafruit, and they believe the issue is with pin 12. How can I suppress this pin or get it to stop interacting with the motor shield?

#include <PS3USB.h>

#include <AFMotor.h>

USB Usb;
PS3USB PS3(&Usb);

AF_DCMotor g_motor1(1, MOTOR12_8KHZ);
AF_DCMotor g_motor2(2, MOTOR12_8KHZ);
AF_DCMotor g_motor3(3, MOTOR12_8KHZ);
AF_DCMotor g_motor4(4, MOTOR12_8KHZ);

//y = PS3.getAnalogHat(LeftHatY);
//x = PS3.getAnalogHat(LeftHatX);
void setup() {
  Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  } 
  Serial.print(F("\r\nPS3 USB Library Started"));
}

void loop() {
  Usb.Task();

if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
    //print X and Y values on serial monitor
      Serial.print(F("\r\nLeftHatX: “));
      Serial.print(PS3.getAnalogHat(LeftHatX));
      Serial.print(F(”\tLeftHatY: "));
      Serial.print(PS3.getAnalogHat(LeftHatY));
     
    /int direction = PS3.getAnalogHat(LeftHatY);
    direction < 120 = 1;
    direction > 135 = 2;
    switch (direction) {
    case 1:
      g_motor1.run(FORWARD);
      g_motor2.run(FORWARD);
      g_motor3.run(FORWARD);
      g_motor4.run(FORWARD);
      break;
    case 2:
      g_motor1.run(BACKWARD);
      g_motor2.run(BACKWARD);
      g_motor3.run(BACKWARD);
      g_motor4.run(BACKWARD);
      break;
    }
/

//set motor directions as forward for all Y < 120
    if(PS3.getAnalogHat(LeftHatY) < 120) {
      g_motor1.run(FORWARD);
      g_motor2.run(FORWARD);
      g_motor3.run(FORWARD);
      g_motor4.run(FORWARD);
    }
    //set motor directions as backward for all Y > 135
    if(PS3.getAnalogHat(LeftHatY) > 135) {
      g_motor1.run(BACKWARD);
      g_motor2.run(BACKWARD);
      g_motor3.run(BACKWARD);
      g_motor4.run(BACKWARD);
    }
    //set speeds for when the joystick is pushed directly up
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-120)-2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-120)
-2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-120)-2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-120)
-2.125);
    }
    //set speeds for when the joystick is pushed directly down
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-135)2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-135)2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-135)2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-135)2.125);
    }
    //set speeds for when joystick is pushed directly left
    if(PS3.getAnalogHat(LeftHatX) < 120 && PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135) {
      g_motor1.setSpeed(0);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-120)
-2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-120)
-2.125);
      g_motor4.setSpeed(0);
    }
    //set speeds for when joystick is pushed directly right
    if(PS3.getAnalogHat(LeftHatX) > 135 && PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX)-135)2.125);
      g_motor2.setSpeed(0);
      g_motor3.setSpeed(0);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX)-135)2.125);
    }
   
    //set speeds for when joystick is pushed up and right
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) > 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-120)
-2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-255)
-2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-255)
-2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-120)
-2.125);
    }
    //set speeds for when joystick is pushed up and left
    if(PS3.getAnalogHat(LeftHatY) < 120 && PS3.getAnalogHat(LeftHatX) < 120) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX))2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-120)
-2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-120)*-2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
    }
    //set speeds for when joystick is pushed down and left
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) < 120) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatX))*2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatX))2.125);
    }
    //set speeds for when joystick is pushed down and right
    if(PS3.getAnalogHat(LeftHatY) > 135 && PS3.getAnalogHat(LeftHatX) > 135) {
      g_motor1.setSpeed((PS3.getAnalogHat(LeftHatY)-135)2.125);
      g_motor2.setSpeed((PS3.getAnalogHat(LeftHatX)-255)
-2.125);
      g_motor3.setSpeed((PS3.getAnalogHat(LeftHatX)-255)
-2.125);
      g_motor4.setSpeed((PS3.getAnalogHat(LeftHatY)-135)*2.125);
    }
   
    //stopping
    if(PS3.getAnalogHat(LeftHatY) > 120 && PS3.getAnalogHat(LeftHatY) < 135 && PS3.getAnalogHat(LeftHatX) > 120 && PS3.getAnalogHat(LeftHatX) < 135) {
      g_motor1.setSpeed(0);
      g_motor2.setSpeed(0);
      g_motor3.setSpeed(0);
      g_motor4.setSpeed(0);
    }
  }
}





Parts
Arduino Thread
https://www.adafruit.com/forums/viewtopic.php?f=31&t=29313

USB Host Shield
http://www.sainsmart.com/sainsmart-compatible-usb-host-shield-for-arduino.html

Motor Shield
http://www.ladyada.net/make/mshield/index.html

Good morning, friend, I saw your topic and I have a similar question.
In this code you only control the direction of the engine, but do not control the speed at all?

I am developing a Tank-type theft and am using your code as a base
My H-Bridges are IBT2
Because the IBT2 has two PWM pins for speed and direction!

Could you implement a MAP function in this code so that the acceleration of the motors is according to the movement of the JOYSTICK?

I am also using Just the Left JoyStick to control all the motors.

I’m using google translator!