autonomous robot

I have bean working on a autonomous robot I have all the hardware done but dont know how to program the robot. I was hopeing Someone will give me someone help for the program. Heres some info for the hardware and programing.

motors heres what i use to run the moters

(1) digitalWrite(12,0); //start motor-B (2) analogWrite(11,0);

(3) digitalWrite(13,0); //stop motor-A (4) analogWrite(3,0);

1 and 3 sets the motors a and b direction the range is 0 to 1 2 and 4 sets the motors power the range is 0 to 255

i have a 180 servo and the signal pin is pluged in to digital pin 7

and i have a ultrasonic sensor and the signal is pluged in to digital pin 6

the ultrasonic sensor is mounted on the servo head.

You've shown us an incomplete list of ingredients; where is your recipe?

the servo moves the ultrasonic sensor back and forth to get distonce data and when the robot gets 1 foot from a wall it uses that data and picks the logest part and heads that direction

Still pretty incomplete.

than what do you need?

I imagine AWOL is looking for your code. We won't write it from scratch.

I was also looking for some sensor details. Vague hand-waving doesn't work on me.

by sensor details what do you mean?

dxw00d: I imagine AWOL is looking for your code. We won't write it from scratch.

and i dont have a code thats why in posting here.

A name, a link to a page describing the device, a spec. The usual stuff.

AWOL: A name, a link to a page describing the device, a spec. The usual stuff.

http://www.parallax.com/Store/Sensors/ObjectDetection/tabid/176/CategoryID/51/List/0/SortField/0/Level/a/ProductID/92/Default.aspx

Ok, so do you have code to drive the sonar?
Do you have code to drive the servo?
Do you have code to drive the motors?

AWOL: Ok, so do you have code to drive the sonar? Do you have code to drive the servo? Do you have code to drive the motors?

nope nope and yes i used this to run the motors with an input from a computer

int button1State = 0;
const int button1 = 2;
int command = 0;

void setup() {     
const int button1 = 2;


Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

pinMode(0,INPUT);
pinMode(13,OUTPUT);
pinMode(3,OUTPUT); 
pinMode(12,OUTPUT);
pinMode(11,OUTPUT); 
pinMode(button1, INPUT);


}

void loop() {
    
  command = Serial.read();

  button1State = digitalRead(button1);
  
    if (command == 'a') {     
    // turn LED on:    
digitalWrite(12,0); //start motor-B
analogWrite(11,255);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);

  }
  
      if (command == 'b') {     
        digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,255);
      }

      if (command == 'c') {     
digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);


if (command == 'd') {    
  
digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);
  }   
  
}

}]int button1State = 0;
const int button1 = 2;
int command = 0;

void setup() {     
const int button1 = 2;


Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

pinMode(0,INPUT);
pinMode(13,OUTPUT);
pinMode(3,OUTPUT); 
pinMode(12,OUTPUT);
pinMode(11,OUTPUT); 
pinMode(button1, INPUT);


}

void loop() {
    
  command = Serial.read();

  button1State = digitalRead(button1);
  
    if (command == 'a') {     
    // turn LED on:    
digitalWrite(12,0); //start motor-B
analogWrite(11,255);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);

  }
  
      if (command == 'b') {     
        digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,255);
      }

      if (command == 'c') {     
digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);


if (command == 'd') {    
  
digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0);
  }   
  
}

}

Moderator edit: URL tags replaced with code tags.

I did a similar robot for one of my first projects.

Here is some links to get you going:

How to get the distance measurement from the PING))) sensor:

How to turn the servo:

Here is how my robot navigated on the floor:

start moving forward;
get distance measurement from the PING))) sensor;
if distance < 1 feet {
  stop;
  turn servo to the left;
  get distance measurement from the PING))) sensor;
  turn servo to the right;
  get distance measurement from the PING))) sensor;
  if leftDistance > rightDistance {
     turn left;
  } else {
     turn right;
  }
}

I actually did a couple of measurements from both right and left and compared the average results. It’s not a performance issue since the measuring only takes a few milliseconds.

virtuoosi:
I did a similar robot for one of my first projects.

Here is some links to get you going:

How to get the distance measurement from the PING))) sensor:
http://arduino.cc/en/Tutorial/Ping

How to turn the servo:
http://arduino.cc/it/Tutorial/Sweep

Here is how my robot navigated on the floor:

start moving forward;

get distance measurement from the PING))) sensor;
if distance < 1 feet {
  stop;
  turn servo to the left;
  get distance measurement from the PING))) sensor;
  turn servo to the right;
  get distance measurement from the PING))) sensor;
  if leftDistance > rightDistance {
     turn left;
  } else {
     turn right;
  }
}




I actually did a couple of measurements from both right and left and compared the average results. It's not a performance issue since the measuring only takes a few milliseconds.

Cool!
if you dont mind can i see your code?

legoaceking:
Cool!
if you dont mind can i see your code?

If you want to learn how to build robots you should do some research and write your own code.

I found one version of the code. It has no distance measurement averaging, and uses custom PWM signals to run the servos. The servos I had were kinda “off” so I couldn’t use a identical PWM signal to run both.

Also the code looks kinda lame and could be optimized - a lot. But I have moved on.

// A simple wall avoiding robot by virtuoosi

// Import the servo library
#include <Servo.h>

Servo lookServo; // Define the lookServo

// Constant variables for the pins
const int servoPin1 = 10; // Servo #1 connected to PWM pin 10
const int servoPin2 = 9; // Servo #2 connected to PWM pin 9
const int pingPin = 5; // Parallax Ping)) sensor connected to pin 5 

// Variables 
long tempTime = 0; // Helps to calculate the time that will be used for turning
long measuredDistance = 0; // Store the distance measured by the Ping)) sensor
long measuredDistanceLeft = 0; // Store the distance measured by the Ping)) sensor
long measuredDistanceRight = 0; // Store the distance measured by the Ping)) sensor

// Setup, run only once
void setup() {
  pinMode(servoPin1, OUTPUT); // Set the Servo #1 pin as output
  pinMode(servoPin2, OUTPUT); // Set the Servo #2 pin as output

  lookServo.attach(3); // Attach the lookServo to PWM pin 3
  lookServo.write(90);

  // A buffer timer, delays the start for 5 seconds
  while(millis() < 5000) {
    // Do nothing
  }
}

// Loop, run infinitely
void loop() {
  measuredDistance = measureDistance(); // Measure & store the distance using the Ping)) sensor

  // If the measured distance is less than 37cm
  if(measuredDistance < 37 && measuredDistance != 0) { // Check the distance and filter out false readings (these occur sometimes)

    // Turn the lookServo around and take measurements from each sides
    lookServo.write(130);
    delay(1000); // Wait 1000ms 
    measuredDistanceLeft = measureDistance(); // Measure the distance to the closest object on left
    lookServo.write(50);
    delay(1000); // Wait 1000ms 
    measuredDistanceRight = measureDistance(); // Measure the distance to the closest object on right
    lookServo.write(90);

    // Compare the measurements from left & right and turn to the direction with most space
    if(measuredDistanceRight > measuredDistanceLeft) {
      // Add 500ms to the current time and store it to the tempTime variable
      tempTime = millis() + 500;

      // Turn the servos to opposite directions for 500ms, turning the bot around
      while(millis() < tempTime) {
        moveForward2();
        moveBackward1();
      }
    }
    else {
      // Add 500ms to the current time and store it to the tempTime variable
      tempTime = millis() + 500;

      // Turn the servos to opposite directions for 500ms, turning the bot around
      while(millis() < tempTime) {
        moveForward1();
        moveBackward2();
      }
    }
  } 
  else { // If the measured distance is more than 37cm, keep moving forward

    moveForward1();
    moveForward2();
  }
}

// Function used to measure the distance to closest obstacle using the Ping)) sensor
// This function returns the distance in cm
long measureDistance() {
  // Variables for the pulse duration and measured distance in cm
  long duration, cm;

  // Set the Ping)) pin as output, and send a short LOW pulse to ensure a clean HIGH pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);

  // Send a HIGH pulse with pulsewidth of 5 microseconds, triggering the Ping)) signal
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.

  // Set the Ping)) pin as input, and read the signal from the sensor
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // Calculate the distance in cm using the duration
  cm = duration / 29 / 2;

  // Return the distance in cm
  return cm;
}

// Function to turn the Servo #1 forward
// Sends a pulse with pulsewidth of 0,2ms
void moveForward1() {
  digitalWrite(servoPin1, HIGH);
  delayMicroseconds(200);
  digitalWrite(servoPin1, LOW);
  delay(20);
}

// Function to turn the Servo #1 backwards
// Sends a pulse with pulsewidth of 1,8ms
void moveBackward1() {
  digitalWrite(servoPin1, HIGH);
  delayMicroseconds(1800);
  digitalWrite(servoPin1, LOW);
  delay(20);
}

// Function to turn the Servo #2 forward
// Sends a pulse with pulsewidth of 1,0ms
void moveForward2() {
  digitalWrite(servoPin2, HIGH);
  delayMicroseconds(1000);
  digitalWrite(servoPin2, LOW);
  delay(20);
}

// Function to turn the Servo #2 backwards
// Sends a pulse with pulsewidth of 0,3ms
void moveBackward2() {
  digitalWrite(servoPin2, HIGH);
  delayMicroseconds(300);
  digitalWrite(servoPin2, LOW);
  delay(20);
}

thanks for the help guys!
heres what i programed

#include <Servo.h> //include Servo library

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int pingPin = 4;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
long duration; //time it takes to recieve PING))) signal

void setup()
{


  panMotor.attach(7); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
digitalWrite(12,0); //start motor-B
analogWrite(11,255);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,255);
  }
  else //if path is blocked
  {
digitalWrite(12,0); //start motor-B
analogWrite(11,0);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,0); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
digitalWrite(12,1); //start motor-B
analogWrite(11,255);

digitalWrite(13,0);  //stop motor-A
analogWrite(3,255);
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
digitalWrite(12,0); //start motor-B
analogWrite(11,255);

digitalWrite(13,1);  //stop motor-A
analogWrite(3,255); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
digitalWrite(12,0); //start motor-B
analogWrite(11,255);

digitalWrite(13,1);  //stop motor-A
analogWrite(3,255);
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  //Convert duration into distance
  return duration / 29 / 2;
}