Sensor and 4 Servos working together on Arduino

does anyone have a codes to run a sonar sensor and 4 servos together? i am working on a project and the idea behind it is that when you comes close to the project its 4 parts moves up amd when the sonar doesn't detects anything it moves back to its original position. thank you so much

Yes, I’ve got four servos and four SRF02’s.
I’m sure you could easily modify it to chop out three of the sensors.

yeas i know i can do it but i dont know how to combined codes for sensor and servos

/*Servo Motor Demo


  • Rotates servo through 180 degrees, using “servoPulse” function
  • adapted from “Temporary Servo Function” by Tom Igoe and Jeff Gray
    */

int servoPin = 9; // R/C Servo connected to digital pin 13
int myAngle; // angle of the servo (roughly in degrees) 0 - 180
int pulseWidth; // function variable

void setup()
{
pinMode(9, OUTPUT); // sets pin D9 as output
}

void servoPulse(int servoPin, int myAngle) // servo function
{ // this is a function for determining our pulsewidth for the servo
pulseWidth = (myAngle * 6) + 320; // this determines our delay below (for a standard pot)
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount (determined by pulsewidth)
digitalWrite(servoPin, LOW); // set servo low
delay(10); // refresh cycle of typical servos (20 ms)
}

servo code

void loop()
{
for (myAngle=0; myAngle<=180; myAngle++) { // cycle through every angle (rotate the servo 180 slowly)
servoPulse(servoPin, myAngle);
}
}

Sonar code

The library for this sensor can be downloaded here -Sonar SRF08.zip

/*

#include <Wire.h>
#include <Sonar_srf08.h>

Sonar_srf08 MySonar;

#define CommandRegister 0x00
int New_Address = 248; //0xF8
#define ResultRegister 0x02

int DEBUG = 1;
char unit = ‘i’; // ‘i’ for inches , ‘c’ for centimeters
float sensorReading =0;

void setup()
{
MySonar.connect();
MySonar.changeAddress(CommandRegister, New_Address);
if (DEBUG){
Serial.begin(9600);
}
New_Address += 4;
//offset address not sure why this is but it works for this address
}

void loop()
{
// set units for reading out distance
MySonar.setUnit(CommandRegister, New_Address, unit);
//pause
delay(70);
// set register for reading
MySonar.setRegister(New_Address, ResultRegister);
// read data from result register
sensorReading = MySonar.readData(New_Address, 2);
//print out distance
Serial.print(“Distance: “);
Serial.print(sensorReading);
Serial.print(” inches”);
Serial.println();
//pause
delay(70);
}

Your servo code would be much simpler if you used the servo library: http://arduino.cc/en/Reference/Servo - that way you don't need generate the servo PPM pulses yourself.

Hint: Get your sonar code printing out consistent ranges, then decide how these range values relate to how you want to move the servos. Write a simple sketch using the servo library to move the servos how you want them.

ok so far i tried to run two servos connected on pin 9 and 8 only one is running im guessing its power issue.. how can i add more power on arduino so it cant run multiple servos?

how can i add more power on arduino so it cant run multiple servos?

The arduino is made to be able to control a servo, not to power the servo motor from the arduino. You might be able to power a small servo from the arduino, but you really need another power source for a large servo or several servos.

i need to add another power source so that the arduino can control the 4 servos... What im asking is.. what kind of power source do i need and how to hook it up with my arduino more info would be helpful ... thanks

Four AA batteries would do the trick to begin with (it really depends on how long you need to run for) - don't forget to connect the battery pack ground and the Arduino ground together.