<< return to Pixycam.com

How to stop motors when object becomes near to the pixy

I’m working on a project in which the robot is supposed to use the pixy2 with integration with Arduino to detect table tennis balls on the floor and go for it then stop when it’s near, to allow the collection mechanism to collect the ball.

I have the below code, but it does not deliver the desired outcome.
Desired outcome:
1- To look in the room for the signature
2- To stop when the object is near, (Because I have a robot)
3- To start the robot with 0 speed.

I will appreciate anyone who may anyone assist me with the code.
Whatsapp number : +966 56 366 8519

Code:
#include <SPI.h>
#include <Pixy2.h>

Pixy2 pixy;
////////////////////////////////////////////////////////

// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10}; //ENA 5 //ENB 10
float deadZone = 0.15;
int baseSpeed = 0;

////////////////////////////////////////////////////////

int cont = 0;
int signature, x, y, width, height;
float cx, cy, area;

void setup() {
Serial.begin(115200);
Serial.print(“Starting…\n”);
pixy.init();
for (int i = 0; i < 6; i++) {
pinMode(myPins[i], OUTPUT);
}
}

void loop() {
float turn = pixyCheck();
if (turn > -deadZone && turn < deadZone) {
turn = 0;
}
if (turn < 0) {
moveRobot(-80, 170);
}
else if (turn > 0) {
moveRobot(170, -80);
}
else {

moveRobot(70, 70);

}
delay(1);
}

float pixyCheck() {
static int i = 0;
int j;
uint16_t blocks;
char buf[32];
// grab blocks!
blocks = pixy.ccc.getBlocks();

// If there are detect blocks, print them!
if (blocks)
{
signature = pixy.ccc.blocks[0].m_signature;
height = pixy.ccc.blocks[0].m_height;
width = pixy.ccc.blocks[0].m_width;
x = pixy.ccc.blocks[0].m_x;
y = pixy.ccc.blocks[0].m_y;
cx = (x + (width / 2));
cy = (y + (height / 2));
cx = mapfloat(cx, 0, 320, -1, 1);
cy = mapfloat(cy, 0, 200, 1, -1);
area = width * height;

   //     Serial.print("sig: ");
//        Serial.print(signature);
//        Serial.print(" x:");
 //       Serial.print(x);
//        Serial.print(" y:");
  //      Serial.print(y);
//        Serial.print(" width: ");
//        Serial.print(width);
//        Serial.print(" height: ");
//        Serial.print(height);
//        Serial.print(" cx: ");
        Serial.print(cx);
        Serial.print('%');
 //       Serial.print(" cy: ");
//        Serial.println(cy);

}
else {
cont += 1;
if (cont == 100) {
cont = 0;
cx = 0;
}
}
return cx;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}

void moveRobot(int leftSpeed, int rightSpeed)
{
if (leftSpeed >= 0) {
digitalWrite(myPins[1], 0);
digitalWrite(myPins[2], 1);
}
else {
digitalWrite(myPins[1], 1);
digitalWrite(myPins[2], 0);
}

if (rightSpeed >= 0) {
digitalWrite(myPins[3], 0);
digitalWrite(myPins[4], 1);
}
else {
digitalWrite(myPins[3], 1);
digitalWrite(myPins[4], 0);
}

analogWrite(myPins[0], abs(leftSpeed));
analogWrite(myPins[5], abs(rightSpeed));
}

Hi Zyad,

could you say more about what’s happening currently, and what you expect to be happening? More specifics would be helpful. Thanks!

Jesse

HEY thank you for replying actually my code is now working but my only problem is that my relay is not functioning at all, it is a code error because it works with a simple code.

My relay is on pin 13 and my vacuum is connected to the NO, in the while loop I want the relay to close so I put digitalWrite(13,LOW) ; But it is not working at all.

Below is my code please try to assit

#include <SPI.h>
#include <Pixy2.h>

Pixy2 pixy;

////////////////////////////////////////////////////////

// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10}; // all Digital pins from the motor driver
int enable1 = 5; // Motor Driver Enable 1
int enable2 = 10; // Motor Driver Enable 2
static int i=0;
int signature, x, y, width, height;
unsigned int newarea = 0;

void setup() {
Serial.begin(115200);
Serial.print(“Starting…\n”);
// pixy.init();
pinMode(13, OUTPUT); // for the relay of the collection system

for (int i = 0; i < 6; i++) {
pinMode(myPins[i], OUTPUT);
}
pixy.init();
pixy.setLamp(0,0); // switching off the Pixy built in Lights
// digitalWrite(13,HIGH) ;

}

void loop() {

  // Detection system 
uint16_t blocks;                               // Maximum number of blocks that getBlocks() will return.
blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
x = pixy.ccc.blocks[i].m_x;                    //get x position
y = pixy.ccc.blocks[i].m_y;                    //get y position
width = pixy.ccc.blocks[i].m_width;            //get width
height = pixy.ccc.blocks[i].m_height;          //get hight

/////////////
// digitalWrite(13,HIGH) ;

if(signature == 1) {

// digitalWrite(13,LOW) ;

delay(350);
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);
delay(30);
moveRobot(-110,0);
delay(500); // time to centre the robot to the ball
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);

// code of area
///////////////////
newarea = width * height; //calculate the object area
Serial.print(newarea); // print area of the ball
Serial.print(’\n’);
while (newarea < 3000 && signature == 1)//go forward if object too small
{
// delay(10);
digitalWrite(13,LOW) ; // Start the vacume

moveRobot(100,100);                              //go forward 
delay (10); 
                 
blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
x = pixy.ccc.blocks[i].m_x;                    //get x position
y = pixy.ccc.blocks[i].m_y;                    //get y position
width = pixy.ccc.blocks[i].m_width;            //get width
height = pixy.ccc.blocks[i].m_height;          //get Height
newarea = width * height;                      //calculate the object area

      Serial.print(newarea);                   // print signature area
 //     Serial.print("NEXT --->");
      Serial.print('\n');
    
  }
  delay (50);
   digitalWrite(enable1, LOW);
   digitalWrite(enable2, LOW); 
   //digitalWrite( 13,LOW) ; 

// delay (10);

   delay (5000) ; // time for collection system to work 

digitalWrite(13,HIGH) ;

}

else {
digitalWrite(13,HIGH) ; // Start the vacume

moveRobot(140,0); // keep searching in circle for the ball

delay (10);

}

}

void moveRobot(int leftSpeed, int rightSpeed)
{
if (leftSpeed >= 0) {
digitalWrite(myPins[1], 0);
digitalWrite(myPins[2], 1);
}
else {
digitalWrite(myPins[1], 1);
digitalWrite(myPins[2], 0);
}

if (rightSpeed >= 0) {
digitalWrite(myPins[3], 0);
digitalWrite(myPins[4], 1);
}
else {
digitalWrite(myPins[3], 1);
digitalWrite(myPins[4], 0);
}

analogWrite(myPins[0], abs(leftSpeed));
analogWrite(myPins[5], abs(rightSpeed));
}

Hi there, I think this is a pin conflict: Pixy2 uses pins 10-13 for SPI communication with Arduino, so you’ll need to use a different pin for your relay.

Hope this helps!

Cheers,
Jesse