Arduino Forum

Topics => Robotics => Topic started by: manju123 on Sep 13, 2013, 03:53 pm

Title: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 13, 2013, 03:53 pm
Hi genius  :)
   I'm going to build (assemble) ultrasonic obstacle avoiding robot and I have Arduino Uno - Atmel Atmega 328-PU board and Arduino Compatible H-Bridge Motor Driver board (photo's attached) and Ultrasonic Module HC-SR04 Arduino. I'm looking for a way to attach cable from motor,ultrasonic module and within uno and motor control board and also suitable  program/code for it... :)
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: PaulS on Sep 14, 2013, 01:22 am
Quote
and also suitable  program/code for it...

Gee, I don't think anyone has ever done that. You just night have to develop your own code.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: drewdavis on Sep 14, 2013, 01:30 am
Quote
Gee, I don't think anyone has ever done that.

LOL.


Quote
Hi genius  
  I'm going to build (assemble) ultrasonic obstacle avoiding robot and I have Arduino Uno - Atmel Atmega 328-PU board and Arduino Compatible H-Bridge Motor Driver board (photo's attached) and Ultrasonic Module HC-SR04 Arduino. I'm looking for a way to attach cable from motor,ultrasonic module and within uno and motor control board and also suitable  program/code for it...


1st off pictures are of little value. Give us links to data sheet or the products page… I don't plan on hunting them down. You bought the stuff!

2. No body is going to wright your code for you… you would never learn. However, I will give you some examples.

Analog read example… chances are your sensor is analog… now way to tell with out data sheet.
Code: [Select]
/*
 AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor.
 Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
 // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 // print out the value you read:
 Serial.println(sensorValue);
 delay(700);        // delay in between reads for stability
}




Thats it for now  until you post the data sheets or product page. No point wasting my time giving you code that might/might not apply.

Best of luck!
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: PaulS on Sep 14, 2013, 02:22 am
Quote
2. No body is going to right your code for you…

Not true. We'll right the code, if it's wrong. But, we won't write the code.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 14, 2013, 04:01 am
here are those links unfortunately there's no any data sheets.some data sheets are with sensor  :(

Uno board- http://hobbyking.com/hobbyking/store/__23767__Arduino_Uno_Atmel_Atmega_328_PU.html
motor drive - http://www.hobbyking.com/hobbyking/store/__37421__Arduino_Compatible_H_Bridge_Motor_Driver.html
sensor - http://www.hobbyking.com/hobbyking/store/__31136__Ultrasonic_Module_HC_SR04_Arduino.html
the project* -http://www.hobbyking.com/hobbyking/store/__37892__Arduino_2WD_Ultrasonic_Smart_Car_Kit.html

I'm bought bought items separately,not the whole project.so I haven't any data sheet or code :( that's why I'm trying to find the way to connect components and code



Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: drewdavis on Sep 14, 2013, 05:21 am
Start with one thing at a time. Lets get the wheels rolling before we add in the obstacle avoidance.


I found this http://www.geekonfire.com/wiki/index.php?title=Dual_H-Bridge_Motor_Driver on the link you gave me. It pretty much explains how to get your motor driver up and running. It even gives you the code!


Once you have your car zipping around come back to this thread you started and we can help you with the object avoidance.

Best of luck!



Quote
Not true. We'll right the code, if it's wrong. But, we won't write the code.


Thanks. I fixed my bad grammar :)
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 14, 2013, 06:04 am
http://www.hobbyking.com/hobbyking/store/__37892__Arduino_2WD_Ultrasonic_Smart_Car_Kit.html

That's actually a pretty good kit for the price, includes all the "components" you need to build a basic robot. What's missing is some of the "glue" components, namely the wires to connect things together (see below). All the pieces are common, standard items, so you can easily find software and libraries for everything. It's very typical with CN companies that they will sell you things very cheaply, but provide little or no support whatsoever beyond that.

Like Drew said, you do one thing at a time, get it working in isolation from everything else, and then gradually bring the different pieces together. After a couple of iterations on this, you'll have learned a lot.

1. first, learn how to program the UNO, if you don't already know how.
2. pick one of the subsystems, and learn to program that.
3. go on to the next subsystem, etc.

If you do a google search on "arduino" and the part, you'll find links to follow up, eg the library for the HC-SR04,
https://www.google.com/webhp?source=search_app#psj=1&q=arduino+HC-SR04
http://forum.arduino.cc/index.php/topic,37712.0.html

Also, to connect things to the Arduino board is a bigger problem than the software. You can get jumpers like this, with male on one end and female on the other, to stick into the Arduino female headers, but the little round pins are really crappy and marginally reliable,
http://www.adafruit.com/products/826

What I would do instead is insert male headers into the Arduino female headers, and use the following type of female-female jumper. MUCH more reliable, and won't fall out as easily.
http://www.adafruit.com/products/400
http://www.adafruit.com/products/266

Good luck, :-).
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 14, 2013, 10:56 am
I have all the connectors :)
I'm check ultrasonic sensor by this code-
http://www.tautvidas.com/blog/2012/08/distance-sensing-with-ultrasonic-sensor-and-arduino/
and motors with
int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7
void setup ()
{
  pinMode(ENA,OUTPUT);//output
  pinMode(ENB,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);//stop driving
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,HIGH);//setting motorA's direction
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);//setting motorB's direction
}
void loop ()
{
  analogWrite (ENA,255);//start driving motorA
  analogWrite (ENB,255);//start driving motorB
}

both are working well and I'm very happy  :D

now I'm looking for how to combine those 2 codes....I think now you can directly help me :)
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: PaulS on Sep 14, 2013, 03:51 pm
Quote
I think now you can directly help me

Code A does something that you like. Code B does something that you like. The combined program, Code C, has some requirements. They are?

If you UNDERSTOOD the code, you'd know that combining them is both easy and hard. Getting all the code into one sketch is easy. Making the parts do something useful is hard.

Knowing what they should do is more than half the battle.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 14, 2013, 07:44 pm
manju, three things:

1. wrap your code using the "#" icon on the edit screen,
Code: [Select]
code goes here

2. go research the concepts of "modular programming", so your program isn't one long jumble of code.

3. at your point in this project, the best thing you can probably do is go do a search on similar projects, and adapt code from them for your own use. This is how everybody does it at the beginning, :-).

https://www.google.com/#psj=1&q=arduino+obstacle+avoidance
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: drewdavis on Sep 14, 2013, 07:48 pm
That code should make your robot turn when something gets closer then 10 inches. Try it and see if it works. Also please do what oric_dan said.

Quote
1. wrap your code using the "#" icon on the edit screen,



Code: [Select]




int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 13;
const int echoPin = 12;

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
}

void loop()
{

analogWrite (ENA,200);//start driving motorA
 analogWrite (ENB,200);//start driving motorB

 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 // Read the signal from the sensor: 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.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

 // convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);
 
 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 
if ( inches < 10) {
 
 
 analogWrite (ENA,255);//start driving motorA
 analogWrite (ENB,1);//start driving motorB
 
 delay (500);
 
}



}

long microsecondsToInches(long microseconds)
{
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 14, 2013, 08:13 pm
Yeah, item #4 was supposed to be the following, but I forgot to include it.

4. "if...else" is the single most important construct for this sort of thing.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 15, 2013, 10:13 am
dear drew davis, :)

as you say I have connect motor control's pins then where's to connect sensor?  :(

in your code
int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 2;
const int echoPin = 4;

as I highlight in red is it ok when I connect ENA & ENB to digital port 5

in sensors connection ( highlight in blue ) where's to connect them motor control board's pins are almost there in 2 and 4 or do I want to connect sensor's trig & echo wires to analog's 2 & 4

please explain me where's to connect wires

I'm add pic how I connected motor control's wires to arduino
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: drewdavis on Sep 15, 2013, 06:38 pm
Sorry I did not notice. I put the code together a little to fast. :) I will update my post soon.

I'm a little confused. That is how your code was. I just copied it in. Does it work like that...

Code: [Select]

int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 15, 2013, 08:35 pm
Where it says port 5 the 2nd time is just a copy typo, pin 6 is actually being assigned.

Also, you cannot use pins 2,4 for the sonar, as they are already assigned as IN1,IN3. You can use 12,13 as shown earlier.

Also, since you're trying to do both sensors and motors in one initial sketch, put the robot up on a book,etc, so the wheels can spin freely during program development.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 16, 2013, 06:15 pm
thanks on advice :) waiting for modified code :)
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 17, 2013, 12:35 pm
I'm try the code in robot. sensor is working (sending distance to serial monitor) well but motors are on same position without turning :/
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 17, 2013, 05:17 pm
As I originally suggested, the best thing to do is learn how to operate the motors separately from the sensors, and the sensors separately from the motors, and after you do that, only then combine the two together.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 20, 2013, 02:10 pm
HELP ME PLEASE  !                                                                          HELP ME PLEASE !                                                                       HELP ME PLEASE !
[/color][/color]
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 20, 2013, 06:47 pm
What is the problem now?
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 21, 2013, 06:34 am
sensors are working  :| and send details to serial monitor.but motors are not working I tried it by even removing sensor but it wasn't work  :(
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: PaulS on Sep 22, 2013, 02:10 am
Quote
but motors are not working

All we know so far is where one end of each of 4 wires go. Where is the rest of the schematic? Where is the code?

You need to do some stuff yourself.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: manju123 on Sep 22, 2013, 07:07 am
this is the code I used

int ENA=5;//connected to arduino's port 5 (output pwm)
int IN1=2;//connected to arduino's port 2
int IN2=3;//connected to arduino's port 3
int ENB=6;//connected to arduino's port 5 (output pwm)
int IN3=4;//connected to arduino's port 4
int IN4=7;//connected to arduino's port 7

const int trigPin = 13;
const int echoPin = 12;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{

analogWrite (ENA,200);//start driving motorA
  analogWrite (ENB,200);//start driving motorB

  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: 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.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
if ( inches < 10) {
 
 
  analogWrite (ENA,255);//start driving motorA
  analogWrite (ENB,1);//start driving motorB
 
  delay (500);
 
}



}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: PaulS on Sep 22, 2013, 06:05 pm
Quote
this is the code I used

Wonderful. So, now what we know is that you STILL can't post code correctly. Still NOTHING about where the other end of the wires go.

All I can say at this point is good luck.
Title: Re: How to make wall/obstacle avoiding robot with arduino uno atmega 328
Post by: oric_dan on Sep 23, 2013, 08:12 pm

he he don't say so  =( =( I'm new to arduino and I like to go throug but I don't know how to do it and no time to learn on this project because it want to be finish before end of this month

Yes, we all have this trouble, don't we. No time to learn how to do our own projects, so better to find someone else to do them for us.

You're off to a VERY BAD start here.

You didn't listen to anything I said previously, so I'll just point you back to read those earlier posts, reply #6 and #9. I'm done here, got other things to do, so good luck.