Book Image

Arduino Robotic Projects

By : Richard Grimmett
Book Image

Arduino Robotic Projects

By: Richard Grimmett

Overview of this book

Table of Contents (21 chapters)
Arduino Robotic Projects
About the Author
About the Reviewers

The Arduino code for the DC motor shield

Now that the HW is in place, bring up the Arduino IDE; make sure that the proper port and device are selected and enter the following code:

The code is straightforward. It consists of the following three blocks:

  • The declaration of the six variables that connect to the proper Arduino pins are as follows:

    int pwmA = 3;
    int pwmB = 11;
    int brakeA = 9;
    int brakeB = 8;
    int directionA = 12;
    int directionB = 13;
  • The setup() function, which sets the directionA, directionB, brakeA, and brakeB digital output pins:

    pinMode(directionA, OUTPUT);
    pinMode(brakeA, OUTPUT);
    pinMode(directionB, OUTPUT);
    pinMode(brakeB, OUTPUT);
  • The loop() function. This is an example of how to make the wheeled robot go forward and then turn to the right. At each of these steps, you'll need to use the brake to stop the robot. The code is as follows:

    // Move forward
    digitalWrite(directionA, HIGH);
    digitalWrite(brakeA, LOW);
    analogWrite(pwmA, 255);
    digitalWrite(directionB, HIGH);