Whil, Pixy, Servo.

Hi everyone, I’ve done a simple code that is supposed to stop the ServoMotor while he is turning to 180 degrees when the pixy sensor detect an object. It doesn’t work. I mean, when I present an object and the object is detected by pixy(verified), its doing nothing, nothing stops.

#include <Pixy2.h>
#include <Servo.h>

Pixy2 pixy;
Servo myServo;
void setup()
{
Serial.begin(9600);
myServo.attach(9);
pixy.init();
}
int b = 0;
void loop()
{
int b = 0;
while(b > 180)
{
myServo.write(b);
int i;

pixy.ccc.getBlocks();
delay(5);

if (pixy.ccc.numBlocks)
{

myServo.detach();
Serial.print(“Detected”)
break;

}
b++;
}

}

int b = 0;
while (b > 180)
{
...
}

You initialise b as 0, then immediately test to see if it’s greater than 180. It’s not, so the while() is never entered.

As a side note, you already have a “b” so you actually have two variables of the same name, one with global scope and one visible only inside loop():

int b = 0;
void loop()
{
  int b = 0;
  ...
}