<< return to Pixycam.com

Control a led with pixy

Hello to the community. I try to control a led with pixy and arduino.When the center of an object is over x>160 i want the led to be turn on and when less than 160 the led to be off.I have a problem.When no object is detected my led is on. When i pass an object the led blinks whatever the position.it never turns off. Here is the code i wrote. Thanks for the help.
#include <SPI.h>
#include <Pixy.h>

Pixy pixy;

void setup()
{
Serial.begin(9600);
Serial.print(“Starting…\n”);
pinMode(13, OUTPUT);

pixy.init();
}

void loop()
{
int x;
int y;
static int i = 0;
int j;
uint16_t blocks;
char buf[32];

blocks = pixy.getBlocks();
digitalWrite(13,LOW);

if (blocks)
{
i++;

if (i%50==0)
{  
  sprintf(buf, "Detected %d:\n", blocks);
  Serial.print(buf);
  for (j=0; j<blocks; j++)
  {
    sprintf(buf, "  block %d: ", j);
    Serial.print(buf); 
    pixy.blocks[j].print();
    x=pixy.blocks[j].x;
  y=pixy.blocks[j].y;
  if(x>160){
   digitalWrite(13, HIGH);
   delay(500);

}
else{
digitalWrite(13,LOW);
delay(500);

}
}
}
}
}

Hello John,
One issue I see with your code is that your digitalWrite for your LED is inside the for-loop. You might want to just look at the object at blocks[0], which is the largest detected object for signature 1. There are some more details about the Arduio-Pixy API here:

http://cmucam.org/projects/cmucam5/wiki/Arduino_API

Edward