<< return to Pixycam.com

Pixycam (with Arduino and Servo) detects objects when they're not there


#1

Hello!
I have a problem and I need your help!

I’m doing a project where I use a Pixycam and three servo motors and an Arduino.
The project is to pick up an object, show it to the Pixycam, and then put it in the appropriate box depending on what colour the object has.

My problem is that when I have Pixycam write out the signature of the object (= the colour code) in the Serial monitor in Arduino, it writes out that an object was detected when it is there (like it should do), but then also the first loop AFTER I TAKE IT AWAY. The second loop it works just as it should do.
If i replace the object with another one it detects that one instead, but when I take that one away and the Pixycam is suppose to write out “No blocks detected” in the Serial Monitor it shows the same as the one before, but then it shows “No blocks detected”.

This is my code if anyone wants to help me :slight_smile:

#include <Servo.h>
#include <SPI.h>
#include <Pixy.h>

int servoPin = 30;
int servoPin2 = 40;
int servoPin3 = 26;

Servo Servo1;
Servo Servo2;
Servo Servo3;
Pixy pixy;

void setup() {

Servo1.attach(servoPin);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);

Serial.begin(57600);
Serial.print(“Starting…\n”);
pixy.init();

Servo2.writeMicroseconds(700);
delay(500);
Servo1.writeMicroseconds(1300);
delay(500);
}

void loop() {

int colour = 0;
static int i = 0;
int j = 0;
uint16_t blocks = 0;
uint16_t blockq = 0;
char buf[32];

delay(2000);
Servo3.writeMicroseconds(700);
delay(1000);

int a = 0;

for (a=0; a<80; a++)
{
blocks = pixy.getBlocks();

  if(blocks)
  {
    blockq = blocks;
  }
  delay(50);

}

if(blockq)
{
colour = pixy.blocks[j].signature;

    sprintf(buf, "Block: ", j);
      Serial.print(buf);
      pixy.blocks[j].print();
      
  Serial.print ("The colour is nr. ");
  Serial.print (colour);
  Serial.print ("\n\n");  

}
else
{
Serial.print(“No blocks detected\n\n”);
}

delay (2000);

Servo1.writeMicroseconds(1100);
delay(1000);

}


#2

Hello Vanessa,
I’m trying to understand your code. It does some things that don’t make sense to me, for example, this code probably goes through 250 frames with the added delay, but throws out all but 1 of them. Is this intended to be a delay?

for (a=0; a<80; a++)
{
blocks = pixy.getBlocks();

if(blocks)
{
blockq = blocks;
}
delay(50);
}

You might start with the ccc_hello_world code and get Pixy detecting what you want it to (and not detecting when it shouldn’t). Once you get it working the way you like with ccc_hello_world, move to your program with full confidence that Pixy is doing what it should will help debug your program.

This page may help you improve Pixy’s recognition accuracy:
https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:some_tips_on_generating_color_signatures_2

Edward