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