Page 1 of 2

Pixy camera and arduino uno!!!

PostPosted: Tue Jan 12, 2016 6:14 pm
by azril1109
hye, i have project with pixy cmucam.. it was fantastic when see the robot move wisely.. but i have problem..
this coding detect 1 colour only.. i have try modify to 2 signiture but fail.. i need your opinion on how to detect 2 colour at once.. i give u example of coding. :D :D :D :D
CODE: SELECT_ALL_CODE
#include <SPI.h> 
#include <Pixy.h>

Pixy pixy;

int signature = 0;
int x = 0;                     
int y = 0;                     
unsigned int width = 0;         
unsigned int height = 0;       
unsigned int area = 0;
unsigned int newarea = 0;
int Xmin = 70;                 
int Xmax = 200;                 
int maxArea = 0;
int minArea = 0;
int motor1 = 4;                 
int enable1 = 5;               
int motor2 = 7;                 
int enable2 = 6;               
int Speed = 70;                 
static int i = 0;

void setup()
{
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(enable1, OUTPUT);
  pinMode(enable2, OUTPUT);
  Serial.begin(9600);
  Stop();
  pixy.init();
}

void loop()
{
  while(millis()<5000)
  {
    scan();
    area = width * height;
    maxArea = area + 1000;
    minArea = area - 1000;
  }

    scan();

  if(signature == 2)
  {
    newarea = width * height;
   
      if (x < Xmin)
      {     
       left();
      }
      else if (x > Xmax)
      {
       right();
      }
      else if(newarea < minArea)
      {
       forward();
      }
      else if(newarea > maxArea)
      {
       backward();
      }
     
      //else stop
      else
      {
        Stop();
      }
   }
   else
   {
    Stop();
     }
}

void backward()//backward
  {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, LOW);
  analogWrite(enable1, Speed);
  analogWrite(enable2, Speed);
  }

void forward()//forward
  {
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, HIGH);
  analogWrite(enable1, Speed);
  analogWrite(enable2, Speed);
  }

void right()//turn right
  {
  digitalWrite(motor1, HIGH);
  digitalWrite(motor2, LOW);
  analogWrite(enable1, Speed);
  analogWrite(enable2, Speed);
  }

void left()//turn left
  {
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, HIGH);
  analogWrite(enable1, Speed);
  analogWrite(enable2, Speed);
  }

void Stop()//stop
  {
  digitalWrite(enable1, LOW);
  digitalWrite(enable2, LOW); 
  }
void scan()
  {
  uint16_t blocks;
  blocks = pixy.getBlocks();
  signature = pixy.blocks[i].signature;   
  x = pixy.blocks[i].x;                   
  y = pixy.blocks[i].y;                   
  width = pixy.blocks[i].width;           
  height = pixy.blocks[i].height;         
  }

Re: Pixy camera and arduino uno!!!

PostPosted: Thu Jan 14, 2016 8:48 am
by Apis210
Hi, try refer to this article.

Re: Pixy camera and arduino uno!!!

PostPosted: Thu Jan 14, 2016 1:29 pm
by azril1109
thanks for u reply.. i have see the article that u give to me but its same.. my diy cannot seen or move according my statement.. if the statement can loop to another condition can be sure?for example, i put 2 signature and put 2 signature in difference condition.. 1 signiture ti forward and 1 signature tu reverse. but in this coding, they looping in 1 signature only.. they cannot run one per one condition..

Re: Pixy camera and arduino uno!!!

PostPosted: Thu Jan 14, 2016 3:26 pm
by Apis210
Have you teach your pixy camera to detect that object or colour?

Re: Pixy camera and arduino uno!!!

PostPosted: Sat Jan 16, 2016 5:09 pm
by azril1109
i have teach but usefull.. they can teach one only..
CODE: SELECT_ALL_CODE
if(signature == 2)
{
newarea = width * height;

if (x < Xmin)
{
left();
}
else if (x > Xmax)
{
right();
}
else if(newarea < minArea)
{
forward();
}
else if(newarea > maxArea)
{
backward();
}

//else stop
else
{
Stop();
}
}
else
{
Stop();
}
}


this can detect one only.. but i double up the coding, the pixy cannot teach... why?
and on arduino library, 'for' stand what function?

Re: Pixy camera and arduino uno!!!

PostPosted: Mon Jan 18, 2016 10:37 am
by suadanwar
Hi Azril1109,

Firstly, you have to teach the pixy camera the new signature colour. For example, red for signature 1 and then orange for signature 2. Please teach the pixy according to this tutorial http://tutorial.cytron.com.my/2015/08/28/colour-tracking-mobile-robot-pixy/ step by step. If you had teach it properly using the PixyMon software, you will see that the pixyCam can detect both colour. It will show red colour s=1 and orange s=2. If not, you are not teaching it properly and you cannot proceed with arduino programming.

Pixy can learn up to seven color signatures. Here are the signatures in order:
Red
Orange
Yellow
Green
Cyan (light blue)
Blue
Violet

Only this colour can be detected by the Pixy camera.

Next, you just have to make two condition for the Arduino Programming. For example:

CODE: SELECT_ALL_CODE
if(signature == 1)
{
newarea1 = width1 * height1;

if (x1 < Xmin1)
{
left();
}
else if (x1 > Xmax1)
{
right();
}
else if(newarea1 < minArea1)
{
forward();
}
else if(newarea1 > maxArea1)
{
backward();
}

//else stop
else
{
Stop();
}
}
else
{
Stop();
}
}

if(signature == 2)
{
newarea2 = width2 * height2;

if (x2 < Xmin2)
{
left();
}
else if (x2 > Xmax2)
{
right();
}
else if(newarea2 < minArea2)
{
forward();
}
else if(newarea2 > maxArea2)
{
backward();
}

//else stop
else
{
Stop();
}
}
else
{
Stop();
}
}

All the signature that you declare must have their own variable declaration. I think this should work for your programming. :)

Re: Pixy camera and arduino uno!!!

PostPosted: Mon Jan 18, 2016 12:56 pm
by azril1109
Hi suadanwar, thanks for your poasting..

My another question, does pixy can read on arduino on another communication protocol like uart and i2c?
What difference the coding on spi,uart or i2c on communication pixy.

I beginning on programming so i lack on coding as well..
Thanks for your reply.

Re: Pixy camera and arduino uno!!!

PostPosted: Wed Jan 20, 2016 12:52 pm
by suadanwar
Hi azril1109,

Based on your question "does pixy can read on arduino on another communication protocol like uart and i2c?", the answer is yes. This is based on the article that i found here: http://www.cmucam.org/projects/cmucam5/ ... ting_Guide . You may refer to this link for more information.

Re: Pixy camera and arduino uno!!!

PostPosted: Wed Jan 20, 2016 11:31 pm
by azril1109
hi suadanwar, thanks for replying.

i have see their website. but i confusing what difference UART and SPi.. and the source code for UART on arduino cannot add on my arduino library..

thanks for replying.

Re: Pixy camera and arduino uno!!!

PostPosted: Tue Mar 08, 2016 10:36 pm
by adam4667
assalam azril.. boleh bagi no phone??? kita projek yang sama dekat puo tapi saya pelajar kss. tq