I'm using the Pixy2 camera from Charmed Labs, and an Arduino Uno R3 from Elegoo.
The goal is to get an LED to light up when the Pixy camera sees a red or blue object. The LED should be off when objects aren't recognized. The Pixy camera uses Pixymon software, where I've labeled red and blue objects as signatures 1 and 6. When I upload the code, the LED just stays on the whole time, even if I cover the camera which means it should stop recognizing signatures entirely.
I'll provide the code and a Fritzing diagram. I wasn't able to find an image for the Pixy camera for Fritzing, but the camera is just connected to the six pin icsp connector using the cable provided, and powered by USB.
I'm very new to doing coding and working with Arduino, and this is my first post, so I hope I've provided all the information necessary.
#include <Pixy2.h>
Pixy2 pixy;
int led = 13;
void setup() {
pinMode(led, OUTPUT);
pixy.init();
}
void loop() {
pixy.ccc.getBlocks();
if (pixy.ccc.blocks[1].m_signature)
{
digitalWrite(led, HIGH);
Serial.println(m_signature);
}
else
{
digitalWrite(led, LOW);
}
}
Edit: I managed to rework the code using a suggestion from Daveculp on the Pixycam forum here-
After some editing, the code worked. When the Pixy picks up a red or blue object, it turns on an LED. This is it-
#include <Pixy2.h>
int led = 12;
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 if (pixy.ccc.blocks[i].m_signature == sigBlue)
{
digitalWrite(led, HIGH);
Serial.println("blue");
}
}// end for
}// end if(blocks)
delay(50); // dont poll thge pixy too fast
}// end while
}