I would like to use this code but how do i modify to suit servos

/* Rumblebot Driving with object avoidance utiliszing PING Ultrasound Sensor

  • and a bump switch.
  • Reads values (00014-01199) from an ultrasound sensor (Parallax PING)
  • Version 1.1.2 Motor runs and drives full time unless object detected.
    *--------------------------------------------------------------------

*/
int switchPin= 2; // Right bump switch on pin 2
int swval; // Variable for reading switch status
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
int motorpinright = 10; // pin for left motor reverse
int motorpinleft = 11; // pin for left motor forward
int motorpinrevright = 5; // pin for right motor reverse
int motorpinrevleft = 6; // pin for right motor forward

void setup() {
pinMode(switchPin, INPUT); // Sets the digital pin as input
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
pinMode(motorpinright, OUTPUT); // Motor drives-----------
pinMode(motorpinleft, OUTPUT); //------------------------
pinMode(motorpinrevright, OUTPUT); //------------------------
pinMode(motorpinrevleft, OUTPUT); //------------------------
}

void loop() {

/* Start Scan


*/{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor


*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse


*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Lite up LED if any value is passed by the echo pulse


*/

if(timecount > 0){
digitalWrite(ledPin, HIGH);
delay(50); //LED on for 50 microseconds
digitalWrite(ledPin, LOW);
}

/* Delay of program


*/

delay(100);

}
/* Action based on data


*/
{
ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
}
if (ultrasoundValue > 800)

{
/* Drive straight forward
*-----------------------------------------------
/
analogWrite(motorpinleft, 255); //100% speed
analogWrite(motorpinright, 255); //100% speed0
}
/
------------------------------------------------
/
else
/
Turn hard right
*---------------------------------------------
*/
{
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevright, 0); // stop right rev motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 255); //100% speed
analogWrite(motorpinleft, 255); //100% speed
delay(380); //380 milliseconds
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinleft, 0); // off

/*----------------------------------------------
*/
}

/* Backup and turn right when switch gets bumped and closes circuit to ground
*---------------------------------------------------
/
/

*/
digitalWrite(switchPin, HIGH); // Sets the pin to high
swval = digitalRead(switchPin); // Read input value and store it
if (swval == LOW) {
analogWrite(motorpinleft, 0); //stop left motor
analogWrite(motorpinright, 0); //stop right motor
analogWrite(motorpinrevleft, 0); // stop left rev motor
analogWrite(motorpinrevright, 0); // stop right rev motor
analogWrite(motorpinrevleft, 255); //100% speed
analogWrite(motorpinrevright, 255); //100% speed
delay(800); //800 milliseconds
analogWrite(motorpinrevleft, 0); // off
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinrevright, 255); //100% reverse speed
analogWrite(motorpinleft, 255); //100% forward speed
delay(700); //700 milliseconds
analogWrite(motorpinrevright, 0); // off
analogWrite(motorpinleft, 0); // off
delay(50); //50 milliseconds
}

/*------------------------------------------------
*/

}

Nice code. Do you have a question you would like to ask?

By the way, [code] tags are very useful.

@Mkoludrovic

I see you are using two motor, so you use two servo. Study that link http://arduino.cc/en/Reference/Servo, it use the servo library, read about the "commands", read the example program, so it will give you some ideas. You have to analyse the code you just posted ( please use # and use "copy & paste" techniques). And look into http://arduino.cc/en/Reference/PulseIn for your ultrasound sensor, and I saw a few pinMode(); INSIDE the void loop() ---> BAD IDEA !!! They have to be in void setup(), use a input to receive the ultrasound pulse using the PulseIn(); and use an output for transmitting the ultrasound pulse, using digitalWrite(pin, HIGH); delay(n); digitalWrite(pin, LOW);

I am sorry that you may re-design/re-program your robot.

By 'servos' do you mean RC model servomotors that have been modified for continuous rotation? The question doesn't make much sense any other way.

yes i would like to use servos that have been modified to 360 deg
instaed of DC motors

yes i would like to use servos that have been modified to 360 deg
instaed of DC motors

If you wan to use modified servos, then you should use the servo library to control them. Below is some simple servo code you can use with the serial monitor to test the servos.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}