Problem With DC Motor and Pixy Library

Hello,

I have been using a pixy2 along with the L289P motor driver. I have narrowed the issue down to a problem with the Pixy2.h library. When I run this code:

//#include <Pixy2.h>

int S1 = 10; 
int HM = 12; 
int S2 = 11; 
int VM = 13;

int timer = 0;

//Pixy2 pixy;

void setup() {
  pinMode(VM, OUTPUT);
  pinMode(HM, OUTPUT);
  pinMode(4, OUTPUT);
  delay(200);
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(4, LOW);
  delay(1000);
  timer = millis();

  //pixy.init();
}
void loop() {

  //pixy.ccc.getBlocks();
  
  digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
  
  /*
      digitalWrite(VM, HIGH);
      digitalWrite(HM, HIGH);
      digitalWrite(S1, HIGH);
      digitalWrite(S2, LOW);
      //delay(2000);
      //analogWrite(S1, 0);
      //delay(2000);
      /*digitalWrite(VM, LOW);
      //digitalWrite(HM, HIGH);
      analogWrite(S1, 255);
      //analogWrite(S2, value);
      delay(2000);
      */
      
}

The 12v DC motors I have run fine, but once I uncomment the lines of code that involve the pixy, the motors no longer work. I can hear them trying to turn but for some reason they do not. I am using 2 motors to control motion on a pantilt machine and this is suppose to track an object, but in my main code the horizontal motor is the only one to move and it moves in a clockwise direction only. I know the data from the pixy is correct and it is communicating perfectly and I can make the pan tilt stop if the object is not seen or is in the middle of the pixy's line of sight. Here is my code for controlling that operation:

#include <Pixy2.h>

#define S1  10
#define S2  11
#define HM  12
#define VM  13
//#define POT A0  // for motor speed control (input)
#define BUZZ  4

Pixy2 pixy;
int valx = 255;    // speed calculated from change in distances
int valy = 255;
int x = 0;
int y = 0;
int n = 0;

void setup() {

  Serial.begin(9600);
  Serial.println("Starting...");
  pinMode(HM, OUTPUT);
  pinMode(VM, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);

  pixy.init();

  pixy.ccc.getBlocks();

}

void loop() {

  for (int i = 0; i < pixy.ccc.numBlocks; i++) {
    /*Serial.print(" block ");
    Serial.print(i);
    Serial.print(": ");
    pixy.ccc.blocks[i].print();
    */
    x += pixy.ccc.blocks[i].m_x;
    y += pixy.ccc.blocks[i].m_y;
    n++;
  }

  x = x / n;
  y = y / n;

  if(pixy.ccc.numBlocks){
    Serial.print("X: ");
    Serial.println(x);
    Serial.print("Y: ");
    Serial.println(y);
  }

  followAverage(x, y);

  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);

  pixy.ccc.getBlocks();
  x = 0;
  y = 0;
  n = 0;

}

// -------------- Fuctions --------------

void followAverage(int x, int y) {
  if (x < 135 && y < 90 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
    digitalWrite(HM, HIGH);
    digitalWrite(VM, HIGH);
    Serial.println("UL");
    //delay(100);
  }
  if (x < 135 && y > 110 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
    digitalWrite(HM, HIGH);
    digitalWrite(VM, LOW);
    Serial.println("BL");
    //delay(100);
  }
  if (x > 165 && y < 90 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
    digitalWrite(HM, LOW);
    digitalWrite(VM, HIGH);
    Serial.println("UR");
    //delay(100);
  }
  if (x > 165 && y > 110 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
    digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    Serial.println("BR");
    //delay(100);
  }
  if (x < 135 && y > 90 && y < 110 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(HM, HIGH);
    digitalWrite(VM, HIGH);
    Serial.println("L");
    //delay(100);
  }
  if (x > 165 && y > 90 && y < 110 && pixy.ccc.numBlocks) {
    digitalWrite(S1, HIGH);
    digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    Serial.println("R");
    //delay(100);
  }
  if (x < 165 && x > 135 && y < 90 && pixy.ccc.numBlocks) {
    digitalWrite(S2, HIGH);
    digitalWrite(HM, HIGH);
    digitalWrite(VM, HIGH);
    Serial.println("U");
    //delay(100);
  }
  if (x < 165 && x > 135 && y > 110 && pixy.ccc.numBlocks) {
    digitalWrite(S2, HIGH);
    digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    Serial.println("B");
    //delay(100);
  }
  if (x < 165 && x > 135 && y < 110 && y > 90) {
    digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    digitalWrite(S1, LOW);
    digitalWrite(S2, LOW);
    Serial.println("on target");
    //delay(100);
  }
  if (pixy.ccc.numBlocks == 0){
    digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    digitalWrite(S1, LOW);
    digitalWrite(S2, LOW);
    //delay(100);
  }
  delay(100);
}

If anyone has dealt with this before or has used some form of DC motors with the pixy library and got it to work, any information or suggestions would be great. Thank you.

Please post the minimal sketch with the pixy code enabled that causes the motor to stop working. In the first sketch you posted, there is a block of code that appears to be associated with the motor, not with the Pixy, that is commented out. I don't know whether you are uncommenting that code at the same time as you are uncommenting the Pixy code, or if you just left some extra commented code in the sketch to confuse matters.

Sorry, here is the code with the pixy enabled:

#include <Pixy2.h>

int S1 = 10; 
int HM = 12; 
int S2 = 11; 
int VM = 13;

int timer = 0;

Pixy2 pixy;

void setup() {
  pinMode(VM, OUTPUT);
  pinMode(HM, OUTPUT);
  pinMode(4, OUTPUT);
  delay(200);
  digitalWrite(4, HIGH);
  delay(1000);
  digitalWrite(4, LOW);
  delay(1000);
  timer = millis();

  pixy.init();
}
void loop() {

  pixy.ccc.getBlocks();
  
  digitalWrite(HM, LOW);
    digitalWrite(VM, LOW);
    digitalWrite(S1, HIGH);
    digitalWrite(S2, HIGH);
  
  /*
      digitalWrite(VM, HIGH);
      digitalWrite(HM, HIGH);
      digitalWrite(S1, HIGH);
      digitalWrite(S2, LOW);
      //delay(2000);
      //analogWrite(S1, 0);
      //delay(2000);
      /*digitalWrite(VM, LOW);
      //digitalWrite(HM, HIGH);
      analogWrite(S1, 255);
      //analogWrite(S2, value);
      delay(2000);
      */
      
}

Which Arduino board are you using?

Arduino Uno with a L289P motor driver.

The Pixy2 library uses the SPI bus to communicate with the Pixy2. On the Uno, the SPI bus is connected to pins 11, 12, 13. The Pixy2 library also uses pin 10 as the chip select (CS) pin for the Pixy2. You used those same 4 pins for your motor driver. When using the SPI bus, you can only use those pins for SPI devices. Connect your motor driver to any other pins on your Uno.

That worked! I ended up using two Unos and used one to communicate with the Pixy2 and then transmit the motion that was needed to take to the other Uno that has the motor shield on it. Works perfectly, thank you for your help!

You're welcome. I'm glad to hear it's working now. Enjoy!
Per