So I am trying to write a function (blobSize) that will return the area of a certain blob. For example if you call blobSize(1,3) it would return the size of the third largest blob of signature 1.
Here is the code of my project:
====================================================
#include <SPI.h>
#include <Pixy.h>
int blocks;
Pixy pixy;
int blobSize(int model, int blob)
{
int area=0, j=0, found=0, num=0;
delay(3);
for (j=0; j<blocks && num<blob; j++)
{
if (pixy.blocks[j].signature == model)
{
num++;
if (num == blob){
area = pixy.blocks[j].width * pixy.blocks[j].height;
}
}
}
return area;
}
void setup()
{
Serial.begin(9600);
Serial.print(“Starting…\n”);
}
void loop()
{
blocks=pixy.getBlocks();
// Serial.print(" BlobS");
//Serial.print(pixy.getBlocks());
Serial.print(“Blob Size1,1 “);
Serial.print(blobSize(1,1));
Serial.print(” Blob Size1,2:”);
Serial.print(blobSize(1,2));
Serial.print(" Blob Size1,3:");
Serial.print(blobSize(1,3));
Serial.print(" Blob Size1,4:");
Serial.println(blobSize(1,4));
}
====================================================
The output looks like this:
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1288 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1316 Blob Size1,2:665 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1288 Blob Size1,2:665 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1316 Blob Size1,2:665 Blob Size1,3:627 Blob Size1,4:242
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1288 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
Blob Size1,1 1316 Blob Size1,2:665 Blob Size1,3:627 Blob Size1,4:242
Blob Size1,1 0 Blob Size1,2:0 Blob Size1,3:0 Blob Size1,4:0
==================================================
As you can see sometimes it works, sometimes it only reads the 1st object, sometimes 2 of them, and most frequently none of them. The image is unchanging, and I can always see the 4 objects in the Pixymon. It is fairly consistent that it will get 4 or 5 tries in a row with no readings before seeing anything.
Any ideas what causes this strange behavior or how to fix it?