<< return to Pixycam.com

Object following robot stuck in while loop

Hi here is my code i am making a object following robot using pixy 2,arduino,l298n code is almost complete i just need help as my width is not updating in operate loop please help
#include <Pixy2.h>

Pixy2 pixy;
int mL= 4;
int mR = 2;
int eL = 5;
int eR = 3;

void setup() {
Serial.begin(115200);
pixy.init();
Serial.println(“Initialising Pixy Camera …”);

for(int i=10;i<13;i++)
pinMode(i, OUTPUT);
pinMode(mL,OUTPUT);
pinMode(mR,OUTPUT);
pinMode(eL,OUTPUT);
pinMode(eR,OUTPUT);
digitalWrite(mL,LOW);
digitalWrite(mR,LOW);

}

void loop() {

int sig,index;
pixy.ccc.getBlocks(); // grabbing blocks

if (pixy.ccc.numBlocks)
{
Serial.print("Detected ");
Serial.println(pixy.ccc.numBlocks);

for(int j=10;j<13;j++)
  digitalWrite(j, LOW);
  
for (int i=0; i<pixy.ccc.numBlocks; i++)
{
  Serial.print("  Block Signature ");
  Serial.print(i);
  Serial.print(": ");
  sig = pixy.ccc.blocks.m_signature;
  index = pixy.ccc.blocks.m_index;
  Serial.println(sig);
  if((sig == 1)||(sig == 2)||(sig == 3))
  operate(i,sig,index);
  break;
}
  find_block();
  delay(10);

}
}

void operate(int i,int sig,int index)
{
int x,width;
width=pixy.ccc.blocks.m_width;

while(width<70)//this is to check width
{

if(x<34)
{slight_left();
width=pixy.ccc.blocks.m_width;}

else if(x>280)
{ slight_right();
width=pixy.ccc.blocks.m_width;}
else
{ forward();
width=pixy.ccc.blocks.m_width;}
delay(10);
Serial.println(width);
width=pixy.ccc.blocks.m_width;
}
stopp();
arm_up();
}

void find_block()
{
pixy.ccc.numBlocks=0;
while(pixy.ccc.numBlocks == 0)
{
rotate();
pixy.ccc.getBlocks();
}
}
void slight_left()
{
digitalWrite(mL,HIGH);
analogWrite(eL,220);
digitalWrite(mR,LOW);
analogWrite(eR,220);
Serial.println(“left”);

delay(100);

}

void slight_right()
{
digitalWrite(mL,LOW);
analogWrite(eL,220);
digitalWrite(mR,HIGH);
analogWrite(eR,220);
Serial.println(“right”);

delay(100);

}

void rotate()
{
digitalWrite(mL,HIGH);
analogWrite(eL,220);
digitalWrite(mR,LOW);
analogWrite(eR,220);

 Serial.println("rotate");
 delay(10);

}
void forward()
{
digitalWrite(mL,HIGH);
analogWrite(eL,220);
digitalWrite(mR,HIGH);
analogWrite(eR,220);
Serial.println(“fwd”);
delay(100);

}
void stopp()
{
digitalWrite(mL,LOW);
analogWrite(eL,0);
digitalWrite(mR,LOW);
analogWrite(eR,0);
Serial.println(“stop”);
delay(100);
}

void arm_up()
{
Serial.println(“pickup”);
}

Hi,

pixy.ccc.blocks is an array. You need to reference an array member in order to get usable data. Like this:

pixy.ccc.blocks[i].m_width
where “i” is an integer.

You might also check your code on lines 43 and 44, since it looks like you’re making the same error there.

can you please tell me where i am wrong

Yes. All of the lines where you define “width”, and these lines:

sig = pixy.ccc.blocks.m_signature;
index = pixy.ccc.blocks.m_index;

The correct code would look like this:

sig = pixy.ccc.blocks[i].m_signature;
index = pixy.ccc.blocks[i].m_index;

And these lines need to be inside of a for loop where the value of “i” is incremented.

Cheers,
Jesse

here i have done the same but the thing is width is not updating rest everything is updating` for (int i=0; i<pixy.ccc.numBlocks; i++)
{
Serial.print(" Block Signature “);
Serial.print(i);
Serial.print(”: ");
sig = pixy.ccc.blocks[i].m_signature;
index = pixy.ccc.blocks[i].m_index;
Serial.println(sig);
if((sig == 1)||(sig == 2)||(sig == 3))
operate(i,sig,index);
break;
}
find_block();
delay(10);
}
}

void operate(int i,int sig,int index)
{
int x,width;
width=pixy.ccc.blocks[i].m_width;
while(width<84)
{
x = pixy.ccc.blocks[i].m_x;
if(x<30)
slight_left();
if(x>285)
slight_right();
else
forward();
delay(10);
}
stopp();
arm_pickup();
}

void find_block()
{
pixy.ccc.numBlocks=0;
while(pixy.ccc.numBlocks == 0)
{
rotate();
pixy.ccc.getBlocks();
}
}`

Hi Munish,

the first part of your code looks good, but everything inside of void operate isn’t updating because again, the value of i is not being updated. You need to wrap that code, too, inside of a for loop to change the value of i.

Hope this helps,
Jesse

Hey Munish,

dit you ever get it to work?

If so, are you willing to share your code with me?