Object avoidance HELP

​Can someone please help me with coding Im new to coding and robotics and I built a biped robot with 4 servos and a HC-SRO4 servo for object avoidance. Im using Arduino uno and I only know how to make the servos move with write.microseconds().


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you also tell us what the servos are that you are using, note that the arduino cannot supply enough current to power one let alone 5 servos.
A picture of your robot would also help.

Tom… :slight_smile:

When your project is finished the working program will be a collection of different pieces working together. For example there will be a piece of code that reads the obstacle detector, another piece that works out where to go, another part that controls the servos etc.

At this stage you need to work on each of those pieces separately. And only bring them together when you know how to get each working on its own.

Have a look at Planning and Implementing a Program


Read This

TED the BiPed Core Code

// ** TED (Totally Easy Design) the BiPed - BASE Code
// ** v1.00 - 12/2012 - Stephen W Nolen
// LetsMakeRobots.com TED Page - http://letsmakerobots.com/node/35632

// ** The basic concept is a "delay free" walking / avoiding solution using timers and incremental changes for walking options
// ** The main loop determines what walking mode we should be in and then calls the sub-routine(s) over and over to complete the walking action
// ** Walking "modes" can change on the fly and the sub routines will start executing that walking request

// ** This assums you are using an UltraSonic sensor and the NewPing library
// ** the Serial library isn't actually used here but can be to feed data to the console on a true Arduino or an LCD display for debugging
#include <Servo.h>
#include <Serial.h>
#include <NewPing.h>

// ** Setup Leg servos - only 2 DOF so Hips and Ankles only

Servo LeftHip;
Servo LeftAnkle;

Servo RightHip;
Servo RightAnkle;

// ** YOU WILL NEED TO CHANGE THE Center/In/Left/Right values in the NormalWalk() 

byte LeftHipPin  = 8; // Define the servo pins and the In / Out / Center limits
  byte LeftHipCenterLimit; // This way you can actually change the distance / settings on the fly via code for "Shortwalk", "LongWalk", etc
  byte LeftHipInLimit;// See the NormalWalk() and ShortWalk() subs at the bottom of the code
  byte LeftHipOutLimit; // This just makes it much easier to change things although it makes it a bit more complicated
  byte LeftHipCurrent;// This tracks the CURRENT servo position so we know what we need to do to move through sub-steps - could use the .read() function but this was easier at the time
  float LeftHipInc;// This variable is used by the sub-step routine that moves the servos from current to requested position as it is called over and over

byte RightHipPin = A4; // This is on an analog to keep wiring shorter based on the Baby Orangutan pin layouts
  byte RightHipCenterLimit;
  byte RightHipInLimit;
  byte RightHipOutLimit;
  byte RightHipCurrent;
  float RightHipInc;

byte LeftAnklePin        = 9;
  byte LeftAnkleCenterLimit;
  byte LeftAnkleLeftLimit;
  byte LeftAnkleRightLimit;
  byte LeftAnkleCurrent;
  float LeftAnkleInc;

byte RightAnklePin        = A3;
  byte RightAnkleCenterLimit;
  byte RightAnkleLeftLimit;
  byte RightAnkleRightLimit;
  byte RightAnkleCurrent;
  float RightAnkleInc;

byte WalkMode         = 0; // Set by the walking subs for the CURRENT walking status - this is set by the sub routines
byte WalkRequest      = 0; // used in the main loop, this is set by sensor, etc and then processed at the end of the main loop

byte WalkModeStop     = 0; // These are the walking status values so we do not have to remember numbers
  byte WalkModeForward  = 1;
  byte WalkModeBackwards= 2;
  byte WalkModeLeft     = 3;
  byte WalkModeRight    = 4;
  byte OldWalkMode; // where we save the last walk mode to try to recover it if we want to use it

byte RobotMode; // Used to determine what the robot should be doing 
  byte StopMode    = 0; // just sit there for now watching the sensors
  byte RoamMode    = 1; // roam mode - just walk around avoiding things
  //byte LightSeekMode    = 2; // find the light using (future) LDRs
  //byte LightAvoidMode   = 3; // avoid the light using (future) LDRs
  byte WaitMode  = 4; // just wait for (future) PIR trigger

long TurnDirection; // from random, determines which was to turn when we see something
long WalkTimer; // walk timer is used by walking routines to test which step we should be taking
int  WalkDelay = 200; // walk delay is used by walktimer to wait to take the next "step" in the stepping process

long SubStepTimer; // used to track what substep we are in within the main step/frame
int  SubSteps; // this is how many increments we are going to move a servo for each walk step or "frame"

long AvoidTimer        = 0; // timer needed to determine how long we should be in avoid mode, start it up at zero
long AvoidTimerWalk    = 0; // timer to use for waking stats during avoid

int LoopDelay          = 20;   // this determines how much we delay the main loop - 50ms is about right

// ** NewPing variables settings#define TRIGGER_PIN  0  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define TRIGGER_PIN  0  //
#define ECHO_PIN     1  // Arduino pin tied to echo pin on the ultrasonic sensor - change as needed - these are just what I have setup on a Baby Orangutan
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int uS;  // sonar raw read
int uScm; // sonar CM value - may be difference for Orangutan?
int SonarHitRange = 10;  // ** Make this smaller if you want to get closer to something before avoiding

// ** Setup stuff
void setup()
  randomSeed(analogRead(0)); // we do not have a hard sensor here (open button) so randomize off it before making input

  SubSteps = (WalkDelay / LoopDelay); // this is how many chances we get to move a servo between each step or frame

  SubSteps = 5;   // You can OVERRIDE the substeps here and just pick a number, the higher the number the SLOWER the walk
  NormalWalk(); // HAVE to set the leg limits before initializing them

  LeftHip.attach(LeftHipPin); // config the left/right/center leg servos

  RightHip.attach(RightHipPin);// config the right leg

  NormalWalk();  // HAVE to set the leg limits before initializing them
  LegsInit();  // initialize legs so we know where we are at

  // Start in standard position
  RobotMode = RoamMode;
  delay(2000);   // Give us some time to put it on the ground

  uS = sonar.ping();// Send ping, get ping time in microseconds (uS).
  uScm = uS / US_ROUNDTRIP_CM; // convert to CM for using - I *THINK* this is off due to 20mhz clock on Orangutan?

  AvoidTimer = 0;// zero out/ initialize these timers
  AvoidTimerWalk = 0;  
  WalkTimer = millis();

void loop() 

  // ** Read the sonar sensor(s) up top so we have a value to work with
  uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  uScm = uS / US_ROUNDTRIP_CM;  // convert to CM for using - I *THINK* this is off due to 20mhz clock on Orangutan?
  if (uScm == 0)
    uScm = 9999;
  } // if no distance we assume out of range and nothing there
  if (RobotMode == StopMode)
  { // pure stop mode - just doing nothing here - need a way out of it
    WalkRequest = WalkModeStop;

  if (RobotMode == WaitMode) // wait mode just waits for a close up sonar hit and then jumps into roam mode
    WalkRequest = WalkModeStop;
    if (uScm < 5)
     RobotMode = RoamMode;                      


  if (RobotMode == RoamMode)
  { // normal roam mode - we generally are forcing a forward walk unless something is in the way
        WalkRequest = WalkModeForward;

        if (uScm < 5) // we can get here by a long walk timer or other robot mode change
         RobotMode = WaitMode;// so if something bothers us trigger avoid and then back to roam mode

  if (uScm < SonarHitRange) 
  {           // Something too close for comfort so we want to backup and then turn
    if (AvoidTimer == 0)
    { // if avoid timer not already running then start it
      AvoidTimer = millis();// avoid timer runs until we reset below for object avoidance
      AvoidTimerWalk = millis();// this is used to track what phase we are in during an avoidance and is reset at end of phase
      TurnDirection = random(1,10); 

  if ((getET(AvoidTimer) > 5000))
   AvoidTimer = 0; 
   AvoidTimerWalk = 0; 

  if (AvoidTimer > 0)

 switch (WalkRequest)
    case 0: 
    case 1:
    case 2: 
    case 3: 
    case 4: 
  }  // End Current Step

  delay(LoopDelay); // loop delay to keep things civilized

TEDBiPed_Base.pde (29.1 KB)

Hi I have built a biped robot with
4 Hitec HS-422 servos
1 HC-SRO4 sensor
Project box to hold Arduino and breadboard
1 9v battery

I am trying to make it avoid obstacles.

Is there a way I could connect all 4 servos to the Arduino if not what could I buy?

Could you provide a sketch example of what an obstacle avoidance code would look like w/ servo and sensor?

Would I put all the coding for the servos and sensor on the same page?

Sorry for all the questions i’m new to coding and robotics.

A good place to start.


I am trying to make it avoid obstacles.

So you have some code already? Is it actually walking and in a straight line?

Tom.... :) PS, how physically do you want the legs to move to make a turn?

No I don't have any code yet, I want to make it walk in a straight line and I want the legs to move like human legs.

Gbelly117: No I don't know how to write code with the servo and sensor to make it avoid objects

Read Reply #6 again and then answer the three questions.

When we have the answer to those questions it will be much easier to make sensible suggestions.



Hi, what is your electronics, programming, arduino, hardware experience?

Do you know how to write and load a simple program into your UNO.

Look in the IDE for EXAMPLES, select SERVO and it will have examples of how to use servos.



Google gargle google arduino uno servo

Tom… :slight_smile:

Hi Tom and I haven't had much experience, but I know how to upload basic code for the Arduino. I am currently trying to make it walk in a straight line with sweep. I don't know how to make it atonomous.

I am currently trying to make it walk in a straight line with sweep.

You need to get that working well before you consider anything more complex.

Post your code. And please use the code button </> so it looks like this


Hi, Do you have an idea of how you want your servos to move to get it to make a step?

Tom... :)

How does this look?

#include <Servo.h>
const int pingPin = 2;
int pos = 0;
Servo rightHip;
Servo rightLeg;
Servo leftHip;
Servo leftLeg;

void setup() {
Serial.begin (9600);

void loop() {
  for (pos = 0; pos >= 180; pos += 1) 

Do you have an idea of how you want your servos to move to get it to make a step?

Tom… :slight_smile:

I want to make the leftHip to go from 0 to 130 degrees while the leftLeg goes from 90 to 120. Also I want the rightHip to go from 0 to 130 when the leftHip returns to 0 and the rightLeg to go from 90 to 120 when the rightHip goes out.

Gbelly117: How does this look?

I presume you have tested it - how does it WORK ?