<< return to Pixycam.com

Pixy2 how to decode blocks

Hi, Just got the Pixy2, I have it working with an Arduino. It’s picking up colour blocks well and sending back block details to the Serial Print page. What I need to do now is extract the x, y, width, height etc data from these blocks (to manage my servo positions) but I keep getting error messages saying blocks do not have ‘width’ or ‘x’ or ‘y’ etc properties. The only one that is recognised is the ‘print’ property. I had little hair to start with, now it’s much less as I’ve been trying everything for days now.
Please, can someone help?
Thank you
Richard

Hello Richard,
Bear in mind that you need to refer to the ccc object within the Pixy object. See the code below – esp the “for example” comment and lines following:

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);
    for (i=0; i<pixy.ccc.numBlocks; i++)
    {
      Serial.print("  block ");
      Serial.print(i);
      Serial.print(": ");
      pixy.ccc.blocks[i].print();
      // for example....
      Serial.println(pixy.ccc.blocks[i].m_x);
      Serial.println(pixy.ccc.blocks[i].m_y); // etc...
    }
  }  
}

Here’s the API – it shows you what fields are available in each block:

https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:ccc_api

Edward

Hi Edward,
That’s wonderful, many thanks. It was the ‘.m_x’ bit that I was missing. So now I can do:
myvariable=pixy.ccc.blocks[I].m_x; (…y, width, height etc)
and then the maths in the normal way.
Thank you
Richard

I made the same mistake, refering to blocks, but you have to use blocks[#].x instead.