Pixy and servo

pixy : https://pixycam.com/

I've done a code for my arduino and it has an error :

The goal is to stop the servo when the pixy x variable is between or equal 150 and 160 and when it is, its not stopping the servo.

Say to me if i'm not clear enough

Code :

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

Pixy2 pixy;
Servo myServo;
Servo myServo2;
Servo myServo3;

void setup() {
// put your setup code here, to run once:
pixy.init();
myServo.attach(9);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
myServo.write(0);
for(int b = 0;b<180;b++)
{
int i;

pixy.ccc.getBlocks();

if (pixy.ccc.numBlocks)
{
int pX = (int32_t)pixy.ccc.blocks[0].m_x;
Serial.println(pixy.ccc.numBlocks);

if(pX<= 150 && pX >= 160)
{
break;
}
else {
delay(5);
myServo.write(b);
}
}
else
{
delay(5);
myServo.write(b);
}

}
delay(10000);

}

Thx :slight_smile:

23 posts and you still don't use code tags to properly post your code? Time to up your game if you want to make it easier for people to help you.

I would suggest re-writing your code and removing your for() loop inside the loop() function. Each time through loop() you should check your values and either update the servo or not rather than a large, convoluted for() loop.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: