Issue communicating between Arduino and Pixy2 Camera

I'm using the Pixy2 camera from Charmed Labs, and an Arduino Uno R3 from Elegoo.

The goal is to get an LED to light up when the Pixy camera sees a red or blue object. The LED should be off when objects aren't recognized. The Pixy camera uses Pixymon software, where I've labeled red and blue objects as signatures 1 and 6. When I upload the code, the LED just stays on the whole time, even if I cover the camera which means it should stop recognizing signatures entirely.

I'll provide the code and a Fritzing diagram. I wasn't able to find an image for the Pixy camera for Fritzing, but the camera is just connected to the six pin icsp connector using the cable provided, and powered by USB.

I'm very new to doing coding and working with Arduino, and this is my first post, so I hope I've provided all the information necessary.

#include <Pixy2.h>
Pixy2 pixy;
int led = 13;

void setup() {
  pinMode(led, OUTPUT);

  pixy.init();

}

void loop() {
  pixy.ccc.getBlocks();
  
  if (pixy.ccc.blocks[1].m_signature)
  {
    digitalWrite(led, HIGH);
    Serial.println(m_signature);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

Edit: I managed to rework the code using a suggestion from Daveculp on the Pixycam forum here-

After some editing, the code worked. When the Pixy picks up a red or blue object, it turns on an LED. This is it-

#include <Pixy2.h>
int led = 12;

int sigRed = 1; // signature 1 is red objects
int sigBlue = 2; //signature 2 is blue

Pixy2 pixy;  // instantiate a Pixy object

void setup() {
  pinMode(led, OUTPUT);

  Serial.begin(115200);
  Serial.print("Starting...\n");

  pixy.init(); // initialize the pixy

}

void loop() {

  uint16_t blocks;

  while (1)
  {

    blocks = pixy.ccc.getBlocks();

    if ( blocks)
    {
      for (int i = 0; i< blocks;i++)
      {
        if (pixy.ccc.blocks[i].m_signature  == sigRed)
        {
          digitalWrite(led, HIGH);
          Serial.println("red");
        }
      else if (pixy.ccc.blocks[i].m_signature  == sigBlue)
        {
          digitalWrite(led, HIGH);
          Serial.println("blue");
        }
    }// end for
  }// end if(blocks)
  delay(50); // dont poll thge pixy too fast
  }// end while
}
if (pixy.ccc.blocks[1].m_signature)

Did you check that there are any blocks?

What is wrong with block zero?

I haven't used this library before, but looking at the documentation for it the getBlocks() method returns the number of objects detected. I would confirm that it is returning a non-zero value before trying to interpret the signature. Also, I assume if it were to return a 1 (meaning it detected 1 object) then you check the first element of the array (0). If it returns mutliple objects then you would check each element of the array.

Something like this:

void loop() 
{
  int numBlocks = pixy.ccc.getBlocks();
  if (numBlocks)
  {
     int i;
     for (i=0; i<numBlocks; i++)
     {
        int signature = pixy.ccc.blocks[i].m_signature;
        if (signature == 1 || signature == 6)
        {
           digitalWrite(led, HIGH);
           Serial.println(signature );
           break;
        }
     }
     // If no signatures with 1 or 6 found then turn off LED
     if (i >= numBlocks) digitalWrite(led, LOW);
  }
  else
  {
     // No objects found
     digitalWrite(led, LOW);
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.