Let's Make Robots!

Object avoidance with the Ultrasonic sensor

I am making a robot using arduino and i need an easy object avoidance code

Here is what i found:

 

 

#include "URMSerial.h"

 

#include <Servo.h>

Servo lservo;

Servo rservo;

// The measurement we're taking

#define DISTANCE 1

#define TEMPERATURE 2

#define ERROR 3

#define NOTREADY 4

 

int ledPin = 13; 

URMSerial urm;

 

void setup() {

  Serial.begin(9600); // Sets the baud rate to 9600

  urm.begin(2,3,9600); // RX Pin, TX Pin, Baud Rate

 pinMode(ledPin, OUTPUT);

  lservo.attach(9);  // attaches the servo on pin 9 to the servo object

  rservo.attach(10);  // attaches the servo on pin 10 to the servo object

}

 

void loop()

{

  // Request a distance reading from the URM37

  urm.requestMeasurement(DISTANCE); // You may call this as many times as you like. It will only send once

 

  // Avoid fetching the distance until we're sure the reading is ready

  if(urm.hasReading())

  {

    int value; // This value will be populated

    switch(urm.getMeasurement(value)) // Find out the type of request

    {

    case DISTANCE: // Double check the reading we recieve is of DISTANCE type

      Serial.println(value); // Fetch the distance in centimeters from the URM37

 

      if (value < 25) // Less than 25cms turn, else carry on.

      {

      digitalWrite(ledPin, HIGH);

      lservo.write(110);

      rservo.write(110);

      }

   else

      {

      digitalWrite(ledPin, LOW);

      lservo.write(110);

      rservo.write(50);

      }

      delay(200);

      break;

    }

  }

}

 

 

But it wont work! can anybody modify the code? please

There is a pic of the wireing i am using

 

 

 

 

AttachmentSize
circuit.jpg213.38 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Well, you could maybe start with reading some of the tips and robot pages on this very website.

Thanks chris I will start learning any tips on books or websights?

It appears that this URMserial thing is to work with a serial-enabled sonar sensor. I have no idea what sensor this would be. It would help us a lot if you would let us know what parts you are using, and provide a CLEAR, IN FOCUS picture of your set-up.

I am a bit curious as to where you found this code...

I have never seen "URMSerial.h" before --not to say it would not work, but it is very strange and there is no reason to use it over the regular built-in Serial. 

 

This will not work, nor can anyone modify this for you. Rarely, if ever, are you going to find code that you can just "drop in to" your robot and have it work. You will have to adjust thresholds for your "too close" numbers, and thus, are going to have to do some stand-alone tests of just your sensor to be sure it is spitting out what you think it is. This is the same for your motors and everything else.

It appears you are using CR servos and you will have to write some test code to find their "center" numbers as well as what numbers (forward and reverse) are needed to get the two CR servos to turn at the same speed.

etc. etc. etc. etc.

There is no way around this, you are going to have to learn code. I would suggest, as we all did, start by simply getting a LED to blink. --There happens to be one handy on pin13 of your Arduino. From there, you will need to write (and learn) test code for just your servos, just your sensor, etc. Once you have figured out each component part of your bot, and how to code and work with each part, it will be a piece of cake to combine them.