<< return to Pixycam.com

A potential problem with the Pixy camera

I’m doing an Arduino project using the Pixy camera in an unconventional way. My primary goal is to light up a red LED when I pick up a red signature, and a blue LED for a blue signature.

I already posted to the Arduino forums, however, I thought I’d also post here for good measure.

My primary problem is that my LEDs are only supposed to light up for a red or blue object. However, both stay flickering all the time.

What I seemed to be struggling with was getting the Pixy to accurately pick up a color signature. It was constantly printing red and blue signatures, even when it wasn’t looking at those type of objects. Currently, it perfectly tracks a red object, and doesn’t confuse it with other objects in the room. However, my two LEDs continue to flicker, even when signatures aren’t being picked up at all.

I can’t figure out if I have a hardware or software issue right now. First it seemed to be one, now the other. I’m really hoping this forum can point me in the right direction.

I will post a picture of my breadboard and code to be as clear as possible.

Lastly, I’m very new to this, so please be patient with me. I’m trying my best.

#include <Pixy2.h>

int led = 12;
int led2 = 11;

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{
      digitalWrite (led, LOW);
      Serial.println("NO HEROES HERE");
    }
  if (pixy.ccc.blocks[i].m_signature  == sigBlue)
    {
      digitalWrite(led2, HIGH);
      Serial.println("blue");
    } else{
      digitalWrite(led2, LOW);
      Serial.println("NO SHAPESHIFTERS HERE");
    }
}// end for

}// end if(blocks)
delay(50); // dont poll thge pixy too fast
}// end while
}

Hello,
Look at your for loop that goes though the blocks – an LED will be on only as long as it’s looking at that block in the for loop. In other words, the LED will not stay on if it detects a red or blue object.

Have 2 variables, redBlock and blueBlock and set them to 0 before entering the for loop.
If you detect a red block set redBlock, to 1
If you detect a red block set blueBlock to 1

After the for loop, set the led and led2 to the redBlock and blueBlock values.

Hope this helps.

Edward

Hey edge, I really appreciate your reply. Here’s what I’ve done so far.

I took a suggestion from a user on the Arduino forums to look at the State Change Detection example under digital examples in the Arduino IDE. In that example, they do a sort of comparison between the current and last state of a button being pressed, which looks like this.

if (buttonState != lastButtonState) {
// if the state has changed, increment the counter

They also initialized their variables by having them equal 0. Based on both your suggestion and this code, I decided to initialize the variables “block” and “lastblock” as 0, and then write in the comparison so that it would continue to compare the state of each. I hope that makes sense. Here’s what that looks like.

#include <Pixy2.h>

int led = 12;
int led2 = 11;

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

int blocks = 0; //current state of blocks
int lastblocks = 0; //previous state of blocks

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 != lastblocks)
{
  for (int i = 0; i< blocks;i++)
  {
    if (pixy.ccc.blocks[i].m_signature  == sigRed)
    {
      digitalWrite(led, HIGH);
      Serial.println("red");
    } else{
      digitalWrite (led, LOW);
      Serial.println("NO HEROES HERE");
    }
  if (pixy.ccc.blocks[i].m_signature  == sigBlue)
    {
      digitalWrite(led2, HIGH);
      Serial.println("blue");
    } else{
      digitalWrite(led2, LOW);
      Serial.println("NO SHAPESHIFTERS HERE");
    }
}// end for

}// end if(blocks)
delay(50); // dont poll the pixy too fast
}// end while
}

It has fixed the flickering issue. If it is receiving blocks, but not detecting signatures, there is a little noise but no flicker. If it receives a red signature, both LEDs come on. If it receives a blue signature, only the red LED turns on. I’m not sure why this is the case.

Because I’m new to this I wasn’t sure how to actually write out part of your suggestion, where you said to specify setting a redBlock to 1, or a blueBlock to 1. And sometimes, even when it’s not picking up blocks, the LEDs like to come on. Not sure what’s going on there exactly.

Could you explain how to write those phrases in the IDE? Specifically about setting the redBlock to 1 or the blueBlock to 1. What might you have done differently?

Again thank you for the reply!

Hello,
Your code isn’t doing what I described. You should move the digitalWrite’s outside the for loop. Please read my message carefully.

Edward