Follow up to communication issues between Arduino and Pixy2 camera

This is a follow up to a previous post to the forum.

I’m trying to light up a red LED when a Pixy2 camera senses a red object, and light up a blue LED when it senses a blue object.

Last time I was having trouble getting Arduino to read and print signatures being picked up by the camera. The Pixy2 tracks movement by sensing seven different color signatures. In this case I’m just trying to pick up two, a red signature, and a blue signature. I’m trying to figure out if I’m having a hardware or software issue, and if so, how to diagnose and solve the problem. It seems to be a problem with the Pixy camera not picking up on signatures, however, I’ve managed to get it to perfectly track a red object and recognize as well as print it in the serial monitor. The issue is that the LEDs just flicker continuously no matter what I do, even when the Pixy isn’t picking up signatures at all.

Unfortunately, I don’t have access to Fritzing software at the time of making this post. I will provide a photo of my breadboard for reference. I apologize for any confusion this may cause. Code will also be provided.

Lastly, I am very new to this, so please be patient with me. I’m doing 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
}

You need to think a bit longer about the code.

Now, as soon as the Pixy sees a block that is neither red nor blue, the code turns off both LEDs. Likewise, when a red block is encountered, the blue LED is turned off.

Study the State Change Detection example in the Arduino IDE (Files>Examples>02.Digital>StateChangeDetection) to learn how to determine when some input has changed, rather than whether the input is simply present.

Hey jremington, thank you for the reply.

Good suggestion about studying the state change detection example. I hope that I’ve applied that code correctly, but also that you can indicate what I may have done wrong.

From your suggestion, I included a phrase to compare the current state of blocks to the last state of blocks in the loop. I also went to the Pixy forums, and, based off of a response there, included initializing the variables “blocks” and “lastblocks” as equaling 0.

This seems to have fixed the constant flickering. I only get lights when I detect a signature, not blocks. Only, both LEDs turn on for a red signature, and only the red LED turns on for a blue signature. So I’m still having some communication issue. However, I’m certainly moving in the right direction.

Here is my updated code. I wonder if there’s some change I need to make in the loop?

#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)// before it just said 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 the pixy too fast
  }// end while
}

Again, thank you for your reply!

That change won’t fix the problem, and you forgot to update “lastblocks”. Study the State Change example a bit more.

What you want to do is detect when the red signature first appears, and then turn on the red LED. Remember that, and don’t change the LED state until the red signature disappears for some reasonable number of Pixy read cycles. Likewise for blue.

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