Pixy 2 Camera to stop 4 DC motors when object signature is detected

Hello,

I am trying to get the Pixy2 Camera to control 4 DC Motors. When an object that is taught to the Pixy and its signature is detected i am trying to get this to stop the 4 motors. I am using an arduino uno along with a motor shield. I am able to get the motors running forward and also able to get the x and y coordinates of the object detected to publish in the serial monitor but i am unable to get the motors to stops when it detects the object with a certain signature. In my case the signature = 1. I have pasted my code below. Any help is appreciated.

#include <AFMotor.h>
#include <Pixy2.h>
#include <SPI.h>

AF_DCMotor leftMotor1(1, MOTOR12_1KHZ); // Creates motor 1 using M1 output on MS set to 1KHZ PWM frequency
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ); // Creates motor 2 using M2 output on MS set to 1KHZ PWM frequency
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ); // Creates motor 3 using M3 output on MS set to 1KHZ PWM frequency
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ); // Creates motor 4 using M4 output on MS set to 1KHZ PWM frequency

Pixy2 pixy; // Instance of a Pixy Object



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); // Opens serial communication at this baud rate
  Serial.print("Starting...\n");

  pixy.init(); // need to initialize pixy object

  pixy.changeProg("color_connected_components"); // use color connected components programme

  leftMotor1.setSpeed(255); // Sets the speed to 255/255 and initializes them
  leftMotor2.setSpeed(255);
  rightMotor1.setSpeed(255);
  rightMotor2.setSpeed(255);

}

void loop() {
  // put your main code here, to run repeatedly:

  leftMotor1.run(FORWARD); // Runs the motor
  leftMotor2.run(FORWARD); // Runs the motor
  rightMotor1.run(FORWARD); // Runs the motor
  rightMotor2.run(FORWARD); // Runs the motor
  {

    if (pixy.ccc.getBlocks()); // Get the blocks
    {
      if (pixy.ccc.blocks[0].m_signature == 1); // If has seen a block with a signature of = 1
      {
        leftMotor1.run(RELEASE); // Stops the motor
        leftMotor2.run(RELEASE); // Stops the motor
        rightMotor1.run(RELEASE); // Stops the motor
        rightMotor2.run(RELEASE); // Stops the motor
        Serial.print(pixy.ccc.blocks[0].m_x); // Then print its x position
        Serial.print("\t");
        Serial.println(pixy.ccc.blocks[0].m_y); // Then print its y  position
      }
    }
  }
}

If the "if" statement does work to stop the motors, it appears that the loop function starts them back up again immediately.

You need a state variable to remember what the motors should be doing at all times.

Your if statements are both broken - lose the semicolons at the end.

The problem with the motors is that immediately after you release them, loop runs again and you start them up again.