Arduino Avoidance Robot

hey there i am new to programing and if there is anyone out there who would help me program this robot to Avoid things here is the robot http://jpegbay.com/gallery/002855865-.html#1 it is using a adafruit motor shield and twin drive motor.

thanks 

Small steps

You are going to have to start the same way we all did…   Blink a LED. You can move on to getting the servo to sweep then maybe take a reading from your sensor. Etc. Etc.

Start hitting google , searching each item, and find a tutorial you like. There is plenty out there.

i have made a LED blink

i have made a LED blink  and  i have made servo to sweep 

i have made a LED blink

i have made a LED blink  and  i have made servo to sweep 

**code **

i wrote this soo far 

#include <Servo.h>  

#include <AFMotor.h>

 

int motor = 4;

int servo = 6;

#define trigpin 3

#define echopin 2 

int dist = 0;

int object = 20;

Servo myservo;

int leftdist = 0;

int rightdist = 0;

 

void setup (){

  Serial.begin (9600);

  pinMode(trigpin, OUTPUT);

  pinMode(echopin, INPUT);

  pinMode(motor, OUTPUT);

  myservo.attach(servo);

  myservo.write(90);

  delay(500);

}

 

void loop(){

  int duration, distance;

  digitalWrite(trigpin, HIGH);

  delayMicroseconds(500);

  digitalWrite(trigpin, LOW);

  duration = pulseIn(echopin, HIGH);

  distance = (duration/2) /29.1;

  Serial.print(distance);

  Serial.println(" cm");

  delay(500);

 

  dist = analogRead(trigpin);

  dist = analogRead(echopin);

 

    if(dist < object) {                         //if distance is less than 550

   forward();                                  //then move forward

  }

  if(dist >= object) {               //if distance is greater than or equal to 550

    findroute();

  }

 

}

 

void forward() {                            // use combination which works for you

digitalWrite(motor, HIGH);

 return;

}

 

void findroute() {

  halt();                                             // stop

  backward();                                       //go backwards

  lookleft();                                      //go to subroutine lookleft

  lookright();                                   //go to subroutine lookright

 

  if ( leftdist < rightdist )

  {

    turnleft();

  }

 else

 {

   turnright ();

 }

}

 

void backward() {

  digitalWrite(motor,LOW);

  delay(500);

  halt();

  return;

}

 

void halt () {

  digitalWrite(motor,LOW);

 delay(500);                          //wait after stopping

  return;

}

 

void lookleft() {

  myservo.write(150);

  delay(500);                                //wait for the servo to get there

 dist = analogRead(trigpin);

  dist = analogRead(echopin);

  myservo.write(90);

  delay(500);                                 //wait for the servo to get there

  return;

}

 

void lookright () {

  myservo.write(30);

  delay(500);                           //wait for the servo to get there

  dist = analogRead(trigpin);

  dist = analogRead(echopin);

  myservo.write(90);                                  

  delay(500);                        //wait for the servo to get there

  return;

}

 

void turnleft () {

  digitalWrite(motor,HIGH);       //use the combination which works for you

  delay(1000);                     // wait for the robot to make the turn

  halt();

  return;

}

 

void turnright () {

  digitalWrite(motor,LOW);       //use the combination which works for you

  delay(1000);                              // wait for the robot to make the turn

  halt();

  return;

}

I would recommend breaking

I would recommend breaking this thing down into bite-sized pieces. I’m not sure of your experience, so I’ll assume you’re starting from scratch. I would first download the Adafruit Motor Library here:

https://github.com/adafruit/Adafruit-Motor-Shield-library

After this is done. Learn how to control your motors with it by going through the following tutorials and writting some code to get your wheels turning.

Control DC motor drive wheels with Adafruit Motor Shield:

http://learn.adafruit.com/adafruit-motor-shield/using-dc-motors

Control Servo steering with Arduino Servo Library (This library is already installed with Arduino IDE):

http://arduino.cc/en/Tutorial/Sweep

After you know how to control your motors and move your servo, It is time to figure out the ultrasonic sensor, so that you can use it’s readings to influence what the motors should be doing. You can find the New_Ping library and an example sketch of it working here:

http://code.google.com/p/arduino-new-ping/

Hope this helps. Your bot looks pretty good so far. Keep it up!

thanks

thanks for the help  JerZ 

i will try that and i will let you know as soon as i get back home i will contact you on 7 august 

A couple pointers

This is your distance finding code:

  digitalWrite(trigpin, HIGH);

  delayMicroseconds(500);

  digitalWrite(trigpin, LOW);

  duration = pulseIn(echopin, HIGH);

  distance = (duration/2) /29.1;

 

These code bits are useless:

  dist = analogRead(trigpin);

  dist = analogRead(echopin);

 

You can’t just do an analog read on the echo pin to get distance, you must use the code listed above. And a read on an input like trigpin will never work! :slight_smile:

In findroute() you call lookleft() and lookright(). Both need to have the garbage code replaced as described above. You also need to add variables leftdist and rightdist and use them in place of the distance variable in the appropriate subroutines or:

  if ( leftdist < rightdist )

will be comparing zero to zero every time. Be sure to make them global variables as they are used in more than one subroutine. I might even change things to

  if ( leftdist <= rightdist )

just to make sure equal values from both directions gets a reaction from the robot.

Check your motor states in the turning subroutines (HIGH vs LOW). You will need to turn the servo before and after the motor command or you’ll avoid nothing, however.

Good start, these things should get you further.

 

**i wrote the code **

i have wrote the code

 i stayed up till 3.00am working on the code 

#include <AFMotor.h>

#include <Servo.h>

 

AF_DCMotor motor(1);

 

int steerPin = 10;  

Servo steerServo;

 

int pingPin = A0; 

int inPin = A1; 

unsigned long duration, inches;

int indec, cmdec;

int inchconv = 147; 

int cmconv = 59; 

String s1, s2, s3;

int steerCentre = 95;  

 

void setup()

{

  //Serial.begin(115200);

  pinMode(pingPin, OUTPUT);

  pinMode(inPin, INPUT);

  steerServo.attach(steerPin, 1000, 2000);

  steer(0);

}

 

void loop()

{

  int cm, lcm, rcm;

  forward(250);

  delay(200);

  cm = getDistance();

  if(cm < 30)

  {

    halt();

    steer(-60);

    lcm = getDistance();

    steer(60);

    rcm = getDistance();

    steer(0);

    if (lcm < rcm)

      steer(-45);

    else

      steer(45);

    reverse(250);

    delay(700);

    steer(0);

  }

}

 

void steer(int angle)

{

  steerServo.write(steerCentre + angle);

  delay(800); 

}

 

int getDistance()

{

  int rval;

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(pingPin, LOW);

  duration = pulseIn(inPin, HIGH, 38000L); 

  if (duration == 0)

    duration = 1000;

  rval = microsecondsToCentimeters(duration);

//  Serial.println(rval);

  return rval;

}

 

void forward (int spd)

{

  motor.run(FORWARD);

  motor.setSpeed(spd);  

}

 

void reverse(int spd)

{

  motor.run(BACKWARD);

  motor.setSpeed(spd);  

 }

 

void spinLeft(int spd)

{

 }

 

void spinRight(int spd)

{

}

 

void halt()

{

  motor.run(RELEASE);

  delay(10);

}

 

long microsecondsToCentimeters(long microseconds)

{

  return microseconds / cmconv;

}