Using ultrasonic sensor, pixy cam, and servos

Hi,

I'm doing this project of my own to move make an autonomous drone. I'm trying to use pixy cam to capture an object and use servos to move to drone's controller. Also, I have 4 ultrasonic in all four ways so that if something gets closer to the drone, the corresponding servos that move the controller move. However, I've noticed that when the Pixy cam is captures data and the ultrasonic sensor detects something, the servos that each correspond to pixy cam and ultrasonic sensor move at a very slow rate, compared to when I only have one sensor moving. Can somebody suggest a solution to allow multiple sensors' input change servos simultaneously? Thank you so much.

If servos move slowly when multiple servos are moving but quicker when only one is, that suggests a lack of power for the servos. How are they powered? Otherwise, something in the program is taking a lot of time (blocking). Can't help with the code without seeing it. Please read the "how to use the forum" stickies to see how to format and post code.

Thanks for the reply!

My code is as follows (pretty simple):

#include <SPI.h>  
#include <Pixy.h>
#include <Servo.h>

// This is the main Pixy object 
Pixy pixy;
Servo servo9;
Servo servo10;
int servopos;
int servopos10;
const int trigPin =10;
const int echoPin =9;
long duration;
int distance;

void setup()
{
  //trigPin = 3;
  //echoPin = 4;
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servopos = 0;
  servopos10 = 0;
  Serial.begin(9600);
  Serial.print("Starting...\n");
  servo9.attach(6);
  servo10.attach(5);
  servo10.write(servopos10);  
  servo9.write(servopos);

  pixy.init();
}

void loop()
{ 
  static int i = 0;
  int j;
  uint16_t blocks;
  char buf[32]; 
  
  // grab blocks!
  blocks = pixy.getBlocks();
  
  // If there are detect blocks, print them!
  if (blocks)
  {
    i++;
    
    // do this (print) every 50 frames because printing every
    // frame would bog down the Arduino
    //if (i%50==0)
    //{
      sprintf(buf, "Detected %d:\n", blocks);
      Serial.print(buf);
      for (j=0; j<blocks; j++)
      {
        sprintf(buf, "  block %d: ", j);
        Serial.print(buf); 
        pixy.blocks[j].print();
        Serial.println("Y POS");
        Serial.println(pixy.blocks[j].y);
        
        if(blocks>0)
        {
          if(pixy.blocks[0].y < 90)
          {
            servopos++;
            servo9.write(servopos);
          }
          else if(pixy.blocks[0].y > 120)
          {
            servopos--;
            servo9.write(servopos);
          }
        }
      }
    //}
  }
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  Serial.println(distance);
  if(distance>50)
  {
    servopos10++;
    servo10.write(servopos10); 
  }
  else if(distance<5)
  {
    servopos10--;
    servo10.write(servopos10);
  }
  
}

The code isn't polished fully mainly because I'm changing here and there to attach different servos and sensors, but I wanted to get this working before I move on to other versions. So what this code basically does is that in Pixy cam's input, if an object that's detected move beyond certain x axis from the center of the camera, it tells one of the servos to move. And the other servo moves if the ultrasonic sensor senses an object at a certain distance.

Power is likely the problem. Post a wiring diagram and describe the power supply.

It was the power supply. Problem has been solved. Thank you.