<< return to Pixycam.com

Pixy2 DC Motor control

Hello, I want to use the Pixy2 to recognize things and control the DC motor.
For example, if the Pixy2 recognizes red, it sends a signal to the motor and the motor moves by this signal.
I’m using Arduino Uno, a Pixy2, and AFMotor to make this.
However, there is no error in the code compile, but on Arduino’s serial monitor, the motor does not work even if there is object information recognized by the Pixy2.
How can I operate the motor?
This is the code that I used

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

AF_DCMotor motor_1(1);
Pixy2 pixy;

void setup() {
Serial.begin(115200);
Serial.print(“Starting…\n”);

pixy.init();

motor_1.setSpeed(2000);

}

void loop() {
int i;
// grab blocks!
pixy.ccc.getBlocks();

// If there are detect blocks, print them!
if (pixy.ccc.numBlocks)
{
Serial.print("Detected ");
Serial.println(pixy.ccc.numBlocks);
motor_1.run(FORWARD);

delay(1000);


for (i=0; i<pixy.ccc.numBlocks; i++)
{
  Serial.print("  block ");
  Serial.print(i);
  Serial.print(": ");
  pixy.ccc.blocks[i].print();

  
 
}

}
}

It’s not a pixy issue but a code issue. (See https://www.arduino.cc/reference/en/language/structure/control-structure/if/)

You can’t do if (pixy.ccc.numBlocks) because pixy.ccc.numBlocks is an array and not a boolean

So How should I fix it?
I’m sorry. I’m not good at making cords. Ask for advice.

For code issues you should ask on an Arduino/C++ community directly.
You can check if the length of the array is bigger than 0