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);
}
}
}
}
}