Object following robot with pixy2 and HC-SR04

Hello everyone,

I have built a robot for my senior project to perform color tracking in real time. The robot uses arduino uno, a pixy 2 camera, 2 DC motors and a L298N motor controller. The robot works fine, the only problem is that the robot is still trying to move after reaching the object. So I have decided to use an ultrasonic sensor in order to stop the motors before reaching the object. Due to limitation of time I need some help with the commands and where to add them in my existing code which you can find here. Thank you

#include <PIDLoop.h> //###############################################
#include <Pixy2.h> //IMPORTING LIBRARIES
Pixy2 pixy; //###############################################

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

// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10};
float deadZone = 0.15; //dead zone tha defines where the object will not move at all equals to 15%
int baseSpeed = 250; // base speed that the robot will run

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

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

void setup() {
Serial.begin(9600);
Serial.print("Starting...\n");
pixy.init();
for (int i = 0; i < 6; i++) {
pinMode(myPins*, OUTPUT); //declaring all pins as output using a for loop*

  • }*
    }
    void loop() {
  • float turn = pixyCheck(); //checking the value of the turn. how much deviation we have from the center using the pixy check function *
  • if (turn > -deadZone && turn < deadZone) { //applying deadzone*
  • turn = 0;*
  • }*
  • if (turn < 0) {*
  • moveRobot(170, -80); //deciding how much to move the robot*
  • }*
  • else if (turn > 0) { //looking if the turn is positive or negative and moving, otherwise keep going forward*
  • moveRobot(-80, 170);*
  • }*
  • else {*
  • moveRobot(70, 70); //takes the value of the left speed and the right speed*
  • }*
  • 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; //height of the object*
  • width = pixy.ccc.blocks[0].m_width; //width of the object*
  • x = pixy.ccc.blocks[0].m_x;//x value of the object*
  • y = pixy.ccc.blocks[0].m_y;//y value of the object*
  • cx = (x + (width / 2)); //center x position*
  • cy = (y + (height / 2)); //center y position*
  • cx = mapfloat(cx, 0, 320, -1, 1); // aplying normalization. If value is from 0-320 change from -1 to 1. This helps in the computation*
  • 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(" cy: ");*
  • //Serial.println(cy);*
  • }*
  • else {*
  • cont += 1;*
  • if (cont == 100) {*
  • cont = 0;*
  • cx = 0;*
  • }*
  • }*
  • return cx; //sending back the x location to tell our robot to turn in a particular direction whether is positive or negative*
    }
    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));*
    }

I need some help with the commands

Which commands?

and where to add them in my existing code

In the proper place(s), of course.

Where do you read the distance sensor? Where should you read the distance sensor? On every pass through loop() seems reasonable to me.

What should you do, on every pass through loop(), if the distance is less than some minimum value? Doesn't stopping seem like a good idea? If the distance is greater than the minimum value, then the relative location of the object is important, and your code should steer the robot.

Yes you are right. The distance should start reading after the camera find the object. Reading the distance in every pass through loop() would be perfect. If the distance is less than the minimum values the motors should stop. If the distance is greater than the minimum value the robot should keep following the object as it does now.

Thank you

Then put them into your code exactly where you want them. Are you using the NewPing.h library? It comes with examples. Write some code and if/when you get stuck, post back [using code tags for your code] and people will help. People on this forum will help with your code, not write code according to your specifications.