B.E.R.D - Arduino Programming Guide / Example Code

There’s a few different ways you could control the servos and in turn control the dog. The main two are creating a long list of individual servo commands or using functions to group certain servo movements/commands. 

We’ll be using functions in our examples as we’ve found it makes the programs easier to understand and adapt to other uses.

 

Lifting legs up and down:

Typical values used to control the servos are 0, 90, and 180. These will need to be adjusted slightly to ensure that the dog stands level and can lift its legs properly. The values will vary depending on the angle you connected the servo horn on at. 

 

The values for up and down should be the same for both the servos on the left-hand side and also the same value for the two on the right-hand side. The table below has the values that we used in our build but they might be slightly differnet in yours.

 

Estimated Servo values:

These are the generic servo values which are a great starting point. The servo position assignments are with holes on the central mount facing away from you, main body servos closest to you.

Servo Name/Position

Up Value

Down Value

Back right

180

90

Back left

0

90

Front right

180

90

Front Left

0

90

 

We now also need to do the same for the servos attached to the main body.

Servo Name/Position

Forward Value

Back Value

Left Middle

0 (90 middle)

180

Right Middle

90

0

 

Making the Dog walk:

There are six steps involved in making the dog move forward:

  1. Lifting an opposite pair of legs up
  2. Moving one half of the body
  3. Lowering the pair of legs
  4. Lifting the other pair of legs
  5. Shifting the other half of the body forward
  6. Lowering the legs

 

The complete example code is heavily commented which should explain how the code works but we’ll break down some of the main parts below. All servo position references are made with the dog facing away from you, the main body servos closest to you.

 

Assigning and defining the servos:

Before we can use and control the servos we need to define the servo and the pin it is connected to.

 

Servo FL;

This line is used to create and name an object that is a Servo type. Once defined we're able to use the name in future references.

 

FL.attach(19);

This line of code is used in the setup function at defined which pin each servo is connected to. In this example we're defining the front left (FL) servo as pin 19.

Move Function:

An Arduino program is make us of a series of functions ran in a specific order. To minimise the amount of code and complexity of controlling the dog we’ll be using a function. When called the function will move the desired servo to the desired position.

void move (int servo, int pos){ }

This creates a function called “move” and we pass it two numbers. The servo we want to control and the position we want to move the servo to. For example we could move servo 1 to the leg up position using a single line of code.

We use a serious of if and else if statements inside the function to check for which servo move and what the desired position is.

 Code Snippet Example:

  //Front Right
  if (servo==1) 
  {
    if (pos==0){
    FR.write(90); //FR straight
    }
  else if (pos==1){
    FR.write(180); //FR up
    }
  }



In this section of code we've definined the servo values for positions 0 and 1 when servo 1 is used. If we wanted to lift the leg up we'd need to use servo 1 and pos 1.

 

This same principle is used to create servo positions for all the valued mentioned in the table earlier. It's even the same for the middle servos which generate forward movement and rotational movement.

Calling the Function:

move(1,0);

 

This will call the function and pass in the servo value “1”, and the position value “0”.

Creating the walking motion: 

As we have a functions created which can be called for all the possible servo movements we’ll need to make the dog walk or turn we just need to call in the correct order.

Below is the sequesnce we've used to create one cycle of the walking forward motion.

//Lift opposite pair of legs up
move(1,1);
move(4,1);
delay(1000); //Delay to allow servos to finish moving

//Move one half of body
move(6,1);
delay(1000);

//Lower the pair of legs
move(1,0);
move(4,0);
delay(1000);

//Lift other legs
move(2,1);
move(3,1);
delay(1000);

//Move other half of body
move(6,0);
delay(1000);

//Lower pair of legs
move(2,0);
move(3,0);
delay(1000);  

Making the Dog turn:

Making the dog turn uses the same principe and steps as walking except it uses the other main body servo.

    We can use the same example code as we did for making the dog walk with a couple of changes. We need to change the parts using the servo which moved the body forward to use the servo which creates rotation. 

    //Rotate body

      move(6,1);

      delay(1000);

     

     //Move other half of body to meet rotated part

      move(6,0);

      delay(1000);

      

    Full Example Walking Code:

    #include <Servo.h>

    Servo FL; //Front Left Servo

    Servo BL; //Back Left Servo

    Servo FR; //Front Right Servo

    Servo BR; //Back Right Servo

    Servo ML; //Left Middle Servo

    Servo MR; //Right Middle Servo

     

    void setup() {

     

    //Defines which pin each servo is connected to (You'll need to change this depending on your controller)

      FL.attach(19);

      BL.attach(23);

      FR.attach(9);

      BR.attach(10);

      ML.attach(20);

      MR.attach(8);

     

    //Sets are servos to default positions (legs down) which are always stored as position 0

    move(1,0);

    move(2,0);

    move(3,0);

    move(4,0);

    move(5,0);

    move(6,0);

     

    }

     

    void loop() {

      /* Servo Assignments

       * 1 -> FR

       * 2 -> FL

       * 3 -> BR

       * 4 -> BL

       * 

       * pos 0 = down/straight leg

       * pos 1 = lift up

       * 

       * 5 -> Middle Forward

       * 6 -> Middle Rotation

       */

     

    //Lift opposite pair of legs up

      move(1,1);

      move(4,1);

      delay(1000); //Delay to allow servos to finish moving

     

    //Move one half of body

      move(5,2);

      delay(1000);

     

    //Lower the pair of legs

      move(1,0);

      move(4,0);

      delay(1000); 

     

    //Lift other legs

      move(2,1);

      move(3,1);

      delay(1000); 

     

     //Move other half of body

      move(5,3);

      delay(1000);

     

     //Lower pair of legs

      move(2,0);

      move(3,0);

      delay(1000); 

     

    }

     

    //function to move desired servo to pre-defined position

    void move (int servo, int pos){

     

    //Series of if statements to check which servo to move and to what position

     

      //Front Right

      if (servo==1)

      {

        if (pos==0){

        FR.write(90); //FR straight

        }

      else if (pos==1){

        FR.write(180); //FR up

        }

      }

     

    //Front Left

      else if (servo == 2)

      {

        if (pos==0){

        FL.write(90); //FL straight

        }

      else if (pos==1){

        FL.write(0); //FL up

        }

      }

     

    //Back Right

      else if (servo == 3)

      {

        if (pos==0){

        BR.write(90); //BR straight

        }

      else if (pos==1){

        BR.write(180); //BR up

        }

      }

     

    //Back Left

        else if (servo == 4)

      {

        if (pos==0){

        BL.write(90); //BL straight

        }

      else if (pos==1){

        BL.write(0); //BL up

        }

      }

     

    //Middle Left -> Move forwards

        else if (servo == 5)

      {

        if (pos==0){

        ML.write(90); //middle

        }

      else if (pos==2){

        ML.write(180); //forward

        }

        else if (pos==3){

        ML.write(0); //back

        }

      }

     

      //Middle Right - Rotation

          else if (servo == 6)

      {

        if (pos==0){

        MR.write(180); //straight

        }

      else if (pos==1){

        MR.write(90); //turn

        }

      }

      

    }

    Previous Next