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

Please add the code tags you forgot.

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();
    _}*
    in this loop i am stuck_

just a wild guess but is the value of 'width' changing? what value is being printed to monitor?

the same value detected first is printing again and again it doesn't update

Please, now, the code tags.

Open Navigation
Show Cart


Arduino Forum >  Using Arduino >  Networking, Protocols, and Devices >  pixy code stuck in while loop Alerts 13 Unread Posts Updated Topics
 Topic: pixy code stuck in while loop (Read 168 times)
munish2411

pixy code stuck in while loop
Today at 10:42 am 
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");
}

Plsease help

If your code is getting stuck in a while loop, my advice would be to get rid of the while loop