How To Make an Obstacle Avoiding Arduino Robot

 by generalgeek314
Featured
MAEPV2.png
Hello all! In this Instructable I'll be showing you how to make a robot similar to the "Mobile Arduino Experimental Platform" (MAEP) that I made. It is equipped with two motors that can steer the robot and the ability to see obstacles in front of it with a PING))) ultrasonic sensor. 

With the attached breadboard, you can do more electronics experiments, fool around with different sensors, etc. This project can teach you about electronics, programming, and robotics. It is also a fun toy to entertain younger siblings and pets (just be sure they don't break it!).

Here is a video of what MAEP will do when it is done:
 
Remove these adsRemove these ads by Signing Up

Step 1: Parts

20120211_175413.jpg
Here's everything you need to make MAEP. I tried to provide links to where you can buy these parts from YourDuino.com, which is the official shop of letsmakerobots.com. EDIT: YourDuino and LMR are now separated and YourDuino is no longer giving money to LMR, however they still have pretty good prices so feel free to shop there.

- Arduino compatible microcontroller. I used an Arduino Uno, so I recommend one if you wish to follow closely along with this tutorial. There are also some Arduino based controllers that are designed specifically for robotics that may be helpful, but you’ll have to find your own method of mounting those. You can buy a copy of the Uno from YourDuino which I believe is completely compatible: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=5

-  Breadboard. Make sure it’s not too big, or else it won’t fit. However, we want it to be as big as possible so we have more room for electronics. This is good: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=168

- A standard old 9 volt alkaline battery (which you can probably find at home), as well as a barrel jack connector to hook it up to your Uno easily (not required, but again, easier):  http://arduino-direct.com/sunshop/index.php?l=product_detail&p=119

- A power supply for the motors. I don’t believe the power supply I chose is the cheapest or best option, so I’m not going to recommend it to you. I used a 4.8 volt rechargeable NiCad battery, but it’s probably easier to just use a 4 AA battery holder (no need to buy a charger that way)

- A bunch of Boe-Bot hardware for the chassis. Unfortunately, you can’t get this from YourDuino, so look at Parallax, the manufacturer’s website. You can choose either to buy this: http://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/304/Default.aspx?txtSearch=boe+bot+chassis which comes with tons of extra components you don’t need (although may be useful someday), or you can buy all the components in the following list from Parallax and no, I’m not gonna provide a link for each one:



Stock # Quantity              Product Name

700-00002            8             Panhead screw, 4/40, 3/8

700-00003            8              4/40 x 3/8" Nut

700-00009            1             Tail Wheel Ball

700-00015            2              #4 Nylon Washer

700-00022            1              Boe-Bot Aluminum Chassis

700-00023            1              Cotter Pin-1/16" Diameter

700-00025            1              Rubber Grommet-13/32" Hole Diameter

700-00028            4              Panhead screw, 4/40, 1/4

700-00060            2              Standoff, threaded aluminum, round 4-40

721-00001            2              Wheel, Plastic, 2.58 Dia, .3 W

721-00002            2              Rubber Band Tire for 721-00001

900-00008            2              Continuous Rotation Servo



- Wire to hook things up will be necessary. Some male-to-male jumpers will do the trick: http://arduino-direct.com/sunshop/index.php?l=product_detail&p=94

- Finally, the PING))) sensor and mounting bracket, so that your robot doesn't kill itself: http://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/563/Default.aspx?txtSearch=ping+sensor+mount


As for tools, all you need is a phillips head screwdriver, a computer, and one of those standard USB printer cables. The Arduino Uno I provided a link for has a cable included. 

Now lets get building!
1-40 of 84Next »
paleontologist99 says: Apr 13, 2013. 5:02 PM
can you also put the center servo code
paleontologist99 says: Mar 21, 2013. 8:31 PM
hey can you put the code in the comments because I'm having a hard time opening the code attachment
generalgeek314 (author) in reply to paleontologist99Mar 25, 2013. 6:44 PM
Sorry it took me so long to reply

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#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 = 7;
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; 
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //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
  {
    leftMotor.write(LForward);
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral);
    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
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward); //turn left
    delay(500);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    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;
}
Beachley says: Mar 6, 2013. 5:01 PM
I just got my robot up and running, it works great! I did have to make one adjustment, the 6v "AA" battery holder wasn't supplying enough power to the motors so I used a 9v battery instead.

Thanks for the great instructable
generalgeek314 (author) in reply to BeachleyMar 20, 2013. 7:24 PM
That's strange - is it a standard alkaline 9 volt battery? For regular servo motors 9v should be too much, and standard 9v batteries don't have much current - you'll probably have to replace them a lot. Glad your robot's working though! Congrats!
Malak_iqra says: Feb 23, 2013. 12:37 PM
Hi,
So am using the link you sent me and making communication between 2 arduinos bt so far no luck :( so i thought of connecting cliff detection to the same Arduino with the Obstacle detection as a back up plan, but for this can u send me the code you used ??
thanks a lot for your help, I really appreciate
generalgeek314 (author) in reply to Malak_iqraFeb 23, 2013. 5:12 PM
I found the code I used, but this robot was taken apart a long time ago so I can't test it unfortunately. I think I may have been working on a different obstacle detection system that continuously scans with the servo motor in front, but you can try it out and replace any code that's not working. Also, be sure to test the cliff detection while holding the robot first, I don't want your shiny new robot driving off of a table!

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include //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 = 7;
const int irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
const int minCliff = 350;
const int maxCliff = 470; //thresholds for cliff detection
const int scanPosition[] = {45, 67.5, 90, 112.5, 135};
int cliffVal; //values from analog sensor
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}

void loop()
{
for(int i=0; i==4; i++)
{
panMotor.write(scanPosition[i]);
delay(1000);
int cliffVal = analogRead(irPin);
if (cliffValmaxCliff)
{
leftMotor.write(LBackward);
rightMotor.write(RBackward);
delay(500);
compareDistance();
}

int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward);
}
else //if path is blocked
{
compareDistance();
}
}
}

void compareDistance()
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
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);

if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
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;
}

Malak_iqra says: Feb 20, 2013. 4:32 PM
HI, So you think an Arduino can perform 2 tasks at the same time, the cliff detection and obstacle detection, cs i bought another Arduino and i though that i can make one responsible for motor movement and the other only sensors and then my sensors one can send commands to my main arduino one, cs i need it to work as efficient as possible. Can u send me any code that i can use?? And for what u saying, can u exactly tell me how it works? Does it work fast enough because next, the robot will go through a specific direction, if no obstacle or cliff on the way.
generalgeek314 (author) in reply to Malak_iqraFeb 20, 2013. 5:44 PM
Yes, it would work fast enough. You can have the Arduino first check for a cliff (because that's higher priority, then only move and continue with the code if there's not one). However, if you want multiple Arduinos to communicate you can use this tutorial: http://www.instructables.com/id/I2C-between-Arduinos/
kwstantinos says: Feb 8, 2013. 10:13 AM
Hi there! Today i got all the stuff i needed, but i realised that i 've done a mistake! My ping sensor has 4 pins... and also the servos was standard... I converted them to continuous and centered them and i think that its ok now... But the big problem is the sensor...What i have to change to the code, and where to plug the extra pins? I saw your older post about this sensor, but i cant understand anything... If you can give me a hand...
generalgeek314 (author) in reply to kwstantinosFeb 19, 2013. 7:59 PM
Hi! This shouldn't be too big of a problem - take a look at sangelek's code further up in the comments. That code should work. Plug the pin labeled Trig into port 7, and the pin labeled echo into port 3.
Malak_iqra says: Feb 19, 2013. 1:40 PM
Hi again :)
I want to thank you so much cs my obstacle detection now works perfectly, so now i will have to add RFID detection and cliff detection for the same robot for it so my project gets completed, however, i found out that i need to add Attiny chip and then use serial ports and send information to my main arduino uno which will be responsible only for the motors directions, but i was thinking if you can help me with this i tried to write some codes and add things together bt so far i have no luck, as i heard in the video u had an extra Infrared sensor for cliff detection, So can you please help me with this ? can u send me the link code or post info abt the obstacle and cliff detection on same robot ?? Or any website that can help me with the serial communication btw Attiny85 and arduino uno ?? Thanx a lot i really appreciate and hope u can help me with that :)
generalgeek314 (author) in reply to Malak_iqraFeb 19, 2013. 7:57 PM
For the IR sensor, you don't need to add an extra microcontroller. If you buy a Sharp IR sensor, you can wire it up like this: http://communityofrobots.com/sites/default/files/images/u1/sharp_ir_arduino_0.jpg. Then, use the function analogRead(0); in your Arduino code to get a reading from the IR sensor. You can use the Arduino serial port to print out the values you get, and then use those to find a nice "danger threshold" for cliffs.
дцветков says: Feb 19, 2013. 2:49 PM
Hi u have build a nice robot ; ) , I have one question about the avoiding of objects . Because i started to build something like robot i was wondering if it is possible when the sensor for avoiding obstacles see an obstacle to turn right or left on a random principe .Am i supposed to use the random function or there is another way . Thanks.
generalgeek314 (author) in reply to дцветковFeb 19, 2013. 7:54 PM
yes, you can use the random function - http://arduino.cc/en/Reference/random
sangelek says: Feb 19, 2013. 4:21 PM
Hi,
Could you help me with the problem I'm having with my robot PLEASE.
Basically I've used the code you have posted, but it didnt work so I made bit change but still it didnt work, the problem is my the servo with ultrasonic sensor on it keep on spinning left and right constantly even tho there is no obstacle in front.
And also the robot goes few steps forward and again spins the servo with the ultrasonic sensor.
By the way i'm using ultrasonic sensor with 4 pins (HC-SR04)
I would really appreciate, if u could correct it
Cheers




/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/
#include //include Servo library

const int RForward = 0;
const int RBackward = 180;
const int LForward = 0;
const int LBackward = 180;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int trigpin = 7;
const int echopin = 3;
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //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
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
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
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}

long ping()
{
// Send out PING))) signal pulse
pinMode(trigpin, OUTPUT);
pinMode(echopin,INPUT);
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);

//Get duration it takes to receive echo

duration = pulseIn(echopin, HIGH);

//Convert duration into distance
return duration / 29 / 2;
}
generalgeek314 (author) in reply to sangelekFeb 19, 2013. 7:53 PM
Hi, this seems like it should work. Google how to use the Arduino serial port if you don't know how, then print out the duration variable in a loop. Test different distances, and see if the sensor is returning the right distance from the obstacle.
greentree89 says: Mar 13, 2012. 8:22 PM
Hey there i am back, i finally got all the parts and i have put my robot together, all that is left is to wire it up! :)
Just got a quick question, I am not entirely sure about the 5v power rail you mentioned.
So i got the 3 servo motors and the ping sensor that all need 5v.
should i be connecting them all to the battery 5v rail or the arduino 5v rail that you said to connect to the breadboard?
Thanks again !
generalgeek314 (author) in reply to greentree89Mar 14, 2012. 1:04 PM
Hi, I'm glad that the robot build went successfully!
Wire up the 3 servo motors to power from the separate battery. Motors use up a lot of current, more than the regulator on the Arduino provides, and they can't be powered directly from the 9V battery because 9 volts is too much for them. That's why you have to hook them up to the other battery.
As for the sensor, hook that up to 5V power from the Arduino. It needs the same steady voltage the Arduino is getting in order to give accurate results.
Hope I helped. If things are still unclear, feel free to ask and I'll do my best to fix whatever comes up.
simmarkalsi in reply to generalgeek314Feb 1, 2013. 7:11 AM
hey i too much liked the project and i would like to try it once but THERE WAS A PROBLEM WITH SERVO MOTORS I DONT KNOW HOW TO MOD SERVO MOTORS FOR CONTINUOUS ROTATION could you please send me message telling me how to do it or a link ...................... please!!!
generalgeek314 (author) in reply to simmarkalsiFeb 6, 2013. 5:16 PM
Also, for step 1 of that tutorial keep in mind that the code provided in the calibrating servos step will work to put the servos in the center position.
generalgeek314 (author) in reply to simmarkalsiFeb 6, 2013. 5:12 PM
Depending on the type of servo you have, you can Google for the solution. Although the process is generally similar amongst all servos, it may vary so I can't give one link that will teach you how to do it. This is a pretty good tutorial though - http://www.societyofrobots.com/actuators_modifyservo.shtml
greentree89 in reply to generalgeek314Mar 14, 2012. 4:00 PM
Hey thanks for your reply, i have now wired it and uploaded the code to my robot.
What happens however is that it rotates in a circle with the right motor moving forward and the left moving backward.
when i put my hand in front of the sensor it moves backwards untill all is clear and then continues to move in rotation, also the sensor servo does not operate at all.

I made sure i got all the wires in the right place and also tried swapping the motor wires around but just got more odd movements

here is the code i am using:
/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include //include Servo library
#include

Ultrasonic ultrasonic ( 4, 5);
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 irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 4; //threshold for obstacles (in inches)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors

void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
leftMotor.write(90);
rightMotor.write(90);
}

void loop()
{
int distanceFwd = ultrasonic.Ranging(INC);
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ultrasonic.Ranging(INC); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ultrasonic.Ranging(INC); //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
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}

gasconmarvinlogico in reply to greentree89Jan 27, 2013. 2:52 AM
hello @greentree89 thanks for the code.. you help me a lot.. we have the same number of pins. if you don't mind, do you still have the schematic diagram or the wiring diagram? coz i'm a beginner if in wiring the arduino. i'm hoping that you can share me the wiring diagram.. thank very much.
generalgeek314 (author) in reply to greentree89Mar 14, 2012. 4:11 PM
For your servo direction problem, try changing this in the code:
const int LForward = RBackward;
const int LBackward = RForward;
To this:
const int LForward = 0;
const int LBackward = 180;

As for your sensor servo, I'm not sure what the problem is. Are you certain this servo works? Try using code from this tutorial to test it - http://arduino.cc/it/Tutorial/Sweep
greentree89 in reply to generalgeek314Mar 15, 2012. 7:46 PM
I fixed it! :D
I was reading up the reason for the odd behavior from all 3 servos and it turns out it was because the servos need to share the ground from both arduino and the battery, i must of missed that part on your tutorial, i just took a lead from from the arduino gnd and connected it to the breadbord gnd rail that you said to link from both sides.
now it responds perfectly! :)
I have now tested out the whole code as i was breaking it down to see how it responded.
everything seems fine except the panmotor still moves all the way to the right at the very start before it even detects an object.
i will do some more tests tomorrow to find out the problem.
generalgeek314 (author) in reply to greentree89Mar 16, 2012. 4:13 AM
Great! Glad to see you were able to find a solution for that problem on your own :) For the problem with the panning servo, try do step 2 again with just that servo (after taking the servo "horn" with the ping sensor on it off). After you've centered the servo, put the ping sensor back on so that it's facing forward.
greentree89 in reply to generalgeek314Mar 17, 2012. 2:40 PM
Hey i tried everything to get the panmotor servo working correctly but it will not respond other than go clockwise then it starts shaking, very odd i can only say it must be faulty.
i will order a new servo but in the mean time i have simplified the code for a fixed sensor so it simply turns left on detection of obstacles, i have also added a piezo and led to the code, i will make a video to show you my robot :)
it is very simple compared to your robot but for my first robot i am very proud :D
i also had to increase the speed of the right servo as it seems to turn left, i must of designed the balance of the chassis badly.
i also find that when it is heading towards obstacles at an angle it does not pick up on the obstacle and completely gets stuck, but i do have a spare sensor, perhaps i can use 2 on the robot to help this.
well theres plenty of ideas to upgrade my robot :)
I will make the video of my robot very soon, thankyou so much for all your help, you've been amazing and so very helpful :)
generalgeek314 (author) in reply to greentree89Mar 18, 2012. 9:12 AM
It's too bad your servo isn't working, but I'm glad you've got your robot working anyways! I look forward to seeing it, and I like that you've added your own custom additions with the LED and piezo. Maybe to give it a more interesting behavior you could have it turn randomly when it detects an obstacle? Try looking here: http://arduino.cc/en/Reference/random

I hope you continue with this hobby, it's very fun and I'm incredibly happy to have helped you enter the world of robotics. I recommend you check out letsmakerobots.com, they have a great community of robot builders that give great advice and feedback, and also there are lots of robots there to get inspiration from.
greentree89 in reply to generalgeek314Mar 18, 2012. 7:55 PM
Hey there! :)
Thankyou so much for all your encouragement and it is because of you i have built my first robot.
I built up a random code from the link you sent me, it works great!
And to add to that i combined the code with something else i found which solves a problem my robot had,

from my last post, simply when the robot came in at an obstacle from an angle it would not detect it and then try and turn too late and it must move forward to make a turn, but when it gets too close to an obstacle it moves backwards then turns.
I have tested the random turning which i really like but i noticed a problem i had not thought of before,

lets say it comes to a corner, and chooses to turn left, it will then immediately meet the other wall and by chance the random code makes it turn right it hit the previous wall again, i have watched it do this quite a few times, but its still quite fun to watch it like this as you said because you never know which turn it will take making things interesting, it always gets it's self out of areas sooner or later anyway hahah

I have bought a new servo for the sensor which should arrive shortly.
Video coming soon! :)
Thanks again!
here is my code if you are interested in seeing it at the moment:


#include //include Servo library
#include

Ultrasonic ultrasonic ( 4, 5);


const int numOfReadings = 1; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
int echoPin = 5; // SRF05 echo pin (digital 2)
int initPin = 4; // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm
int pinSpeaker = 13;
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
int leftDistance, rightDistance; //distances on either side
Servo leftMotor;
Servo rightMotor; //declare motors
int randNumberSpeed; // variable to store the random speed value
int randNumberDir; // variable to store the random direction value
int x = randNumberDir;
void setup()
{
Serial.begin(9600);
rightMotor.attach(11);
leftMotor.attach(8);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
pinMode(4, OUTPUT); // trig init
pinMode(5, INPUT); //echo
// create array loop to iterate over every item in the array

for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;

randomSeed(millis());
}

}

void loop() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array

// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}

averageDistance = total / numOfReadings; // calculate the average distance
delay(10);

// Check the average distance and move accordingly

if (averageDistance <= 10) {
// go backwards
leftMotor.writeMicroseconds(1000);
rightMotor.writeMicroseconds(2000);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
{
playTone(750, 500);
delay(350);
}
}


if (averageDistance <= 25 && averageDistance > 10 ) {

//turn left
randomSeed(millis());
randNumberDir = random(300); {
if (randNumberDir > 150){
rightMotor.writeMicroseconds(1000);
leftMotor.writeMicroseconds(1500);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
{
playTone(750, 300);
delay(750);
}
}
else if (randNumberDir < 150){
rightMotor.writeMicroseconds(1500);
leftMotor.writeMicroseconds(2000);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
{
playTone(750, 400);
delay(750);
}
}
}
}

if (averageDistance > 25) {
// go forward
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(1000);
leftMotor.writeMicroseconds(2000);
rightMotor.writeMicroseconds(1100);
}
}


// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
greentree89 in reply to generalgeek314Mar 14, 2012. 6:38 PM
Heheh got 3 problems all at once, i tried this but it still does the same thing.
It simply continues to move in rotation, it seems to be the left motor that is going the wrong way no matter where ever i set it to 0, 180 or 90

And of course it does not stop but rather instead moves backwards when there is an obstacle.

I also tried that code for the sensor servo, it moves it all the way to the right but then nothing else happens, same for the robot code, nothing moves it to the left again.

Its ok if you cant figure this one out, you've been an amazing help with all my questions, i will continue to try and work on it.
Thanks again.
simmarkalsi says: Feb 1, 2013. 7:11 AM
hey i too much liked the project and i would like to try it once but THERE WAS A PROBLEM WITH SERVO MOTORS I DONT KNOW HOW TO MOD SERVO MOTORS FOR CONTINUOUS ROTATION could you please send me message telling me how to do it or a link ...................... please!!!
gasconmarvinlogico says: Jan 28, 2013. 4:56 AM
hello im just wondering. my left and right servo won't spin 360degrees but it only spin to 180 degrees.. how to make it spin to 360...
gasconmarvinlogico says: Jan 27, 2013. 2:51 AM
hello @greentree89 thanks for the code.. you help me a lot.. we have the same number of pins. if you don't mind, do you still have the schematic diagram or the wiring diagram? coz i'm a beginner if in wiring the arduino. i'm hoping that you can share me the wiring diagram.. thank very much..
gasconmarvinlogico says: Jan 21, 2013. 3:54 AM
thank you very much for your reply.. i will message if i will start doing the project, because im still waiting the components... can i ask you a question on how to send a message using an arduino and receive it using arduino also.. your reply helps a lot..
generalgeek314 (author) in reply to gasconmarvinlogicoJan 26, 2013. 12:48 PM
There are many ways to make two arduinos communicate - if you want wireless, I might take a look at bluetooth or xbee. If you are okay with a wired connection, I would try serial. Here's an example of serial that you can start from - http://www.billporter.info/2011/05/30/easytransfer-arduino-library/. Google is your friend to get more info :)
gasconmarvinlogico says: Jan 24, 2013. 7:56 AM
hello good day.. im already starting to build the robot but i have a problem of my sensor because it has a 4 pins output the ( VCC , trig(T), echo(R), GND), whereas your sensor has only 3 pins,, how should i make it to work...?? please help me...hoping for your immediate reply..
generalgeek314 (author) in reply to gasconmarvinlogicoJan 26, 2013. 12:45 PM
If you go the next comment page, at the very bottom you can take a look at a discussion I had with user "greentree". He has the same type of sensor as you, and he managed to make it work.
Malak_iqra says: Jan 23, 2013. 2:00 PM
hi, thnx a lot for ur reply :)
am using S35/STD motors
do u think i should change them, Also, i guess sensors work when motors stop and when motors stop my sensor works, cs it takes a long time to detect an object, how can i change fix this issue?? Or do u think its just motors problem?? I also tried to program my robot to move forward and when it detects sth to stop and then move forward bt it just move forward without detecting anything?? do u have any idea why is this happening ?? Is there a way that u can help me with these inquiries ?? I really need help am working on a big project and these are the first steps of it :(
hope u can help me
generalgeek314 (author) in reply to Malak_iqraJan 26, 2013. 12:43 PM
Those motors should work. What sensor are you using, and can you send me the code you used to try and test the robot? Thanks!
1-40 of 84Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!