Using Pixy2 Cam with Ultrasonic and L298N for Obstacle Avoidance

Hey everyone, I'm new here, and fairly new to coding with Arduino. I'm working on a robot that uses the Pixy2 Cam and has an ultrasonic sensor for obstacle avoidance and the ultrasonic is works with an L298N H-Bridge. I made sure that the obstacle avoidance works first, and it does, and now I'm trying to integrate the Pixy2 into the mix and I'm coming across a few issues. I took the base code that we used for obstacle avoidance, and integrated the Pixy Pan and Tilt Demo code into our original code. It compiles and uploads just fine. However, the serial monitor is reading back "error: no response" and I know that's usually associated with the Pixy not having any recognized objects, but that's not the case here as I taught it to follow something. The pan and tilt demo code works just fine alone. Here's the code, if anyone could provide any insight that would be most helpful.

#include <Pixy2.h>
#include <PIDLoop.h>


PIDLoop panLoop(400, 0, 400, true);
PIDLoop tiltLoop(500, 0, 500, true);

int trigPin = 9;
int echoPin = 8; 

int revleft4 = 4;
int fwdleft5 = 5;
int revright6 = 6; 
int fwdright7 = 7; 

long duration, distance;

Pixy2 pixy;

void setup() {

  Serial.begin(115200);
  Serial.print("Starting...\n");
 
  // We need to initialize the pixy object 
  pixy.init();
  // Use color connected components program for the pan tilt to track 
  pixy.changeProg("color_connected_components");

  delay(random(500,2000));
  Serial.begin(115200);
  pinMode(revleft4, OUTPUT);
  pinMode(fwdleft5, OUTPUT);
  pinMode(revright6, OUTPUT);
  pinMode(fwdright7, OUTPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
}

void loop() {

  static int i = 0;
  int j;
  char buf[64]; 
  int32_t panOffset, tiltOffset;
  
  // get active blocks from Pixy
  pixy.ccc.getBlocks();
  
  if (pixy.ccc.numBlocks)
  {        
    i++;
    
    if (i%60==0)
      Serial.println(i);   
    
    // calculate pan and tilt "errors" with respect to first object (blocks[0]), 
    // which is the biggest object (they are sorted by size).  
    panOffset = (int32_t)pixy.frameWidth/2 - (int32_t)pixy.ccc.blocks[0].m_x;
    tiltOffset = (int32_t)pixy.ccc.blocks[0].m_y - (int32_t)pixy.frameHeight/2;  
  
    // update loops
    panLoop.update(panOffset);
    tiltLoop.update(tiltOffset);
  
    // set pan and tilt servos  
    pixy.setServos(panLoop.m_command, tiltLoop.m_command);
   
#if 0 // for debugging
    sprintf(buf, "%ld %ld %ld %ld", rotateLoop.m_command, translateLoop.m_command, left, right);
    Serial.println(buf);   
#endif

  }  
  else // no object detected, go into reset state
  {
    panLoop.reset();
    tiltLoop.reset();
    pixy.setServos(panLoop.m_command, tiltLoop.m_command);
  }

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  duration = pulseIn(echoPin, HIGH); 
  distance = duration / 58.2;
  delay(10);

  if (distance > 19)
  {
    Serial.print ("distance");
    digitalWrite(fwdright7, HIGH); 
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, HIGH);
    digitalWrite(revleft4, LOW);
  }

  if (distance < 18)
  {
    Serial.print ("obstacle avoidance");
    digitalWrite(fwdright7, LOW);
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, LOW);
    digitalWrite(revleft4, LOW);
    delay(500);
    digitalWrite(fwdright7, LOW);
    digitalWrite(revright6, HIGH);
    digitalWrite(fwdleft5, LOW);
    digitalWrite(revleft4, HIGH);
    delay(500);
    digitalWrite(fwdright7, LOW);
    digitalWrite(revright6, LOW);
    digitalWrite(fwdleft5, LOW);
    digitalWrite(revleft4, LOW);
    delay(100);
    digitalWrite(fwdright7, HIGH);
    digitalWrite(revright6, LOW);
    digitalWrite(revleft4, LOW);
    digitalWrite(fwdleft5, LOW);
    delay(500);
  }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.