How Your Race Car Works: The Basics

Before we dig into the more complicated code, let's take a minute to break down the simple motor controller code, and how it works to control your robot's motors.

Before going any further, make sure you have a basic understanding of how to program and use an Arduino-compatible Feather microcontroller. Thankfully, we have a lot of great tutorials on how this whole thing works. Click here to get started with Arduino, and then come back to this guide to continue.

Before going any further, make sure you have a basic understanding of how to program and use an Arduino

Board Support Package and Drivers

For the Feather 32u4, check out the guide for how to install Arduino IDE & add Adafruit Board support, then install Feather Board support

Libraries

For your robot to work correctly, you will need to install a couple libraries. To install the libraries, we will use Arduino's handy library manager. Navigate to the library manager like shown in the screenshot below.

Then, all you need to do is search for the library you want to install. For this mini race car, start by searching for 'Adafruit Motor Shield', you should see two options like this:

Under the Adafruit Motor Shield V2 Library, select the latest version from the dropdown, then click the install button.

While we won't need it quite yet, you might as well go ahead and search for 'Adafruit BluefruitLE nRF51', and install the latest version of that. We will use this library later on.

DC Motor Test

Now that you have the libraries installed, let's open up the example sketch to try out the DC motors on your car. You can find the example sketch in the Arduino Examples menu:

Take a look through this example sketch. Before you upload the code and run it on your robot, you need to make a change or two. First off, you will need to change what port the motors are connected to. Let's start by just trying one of the motors. If you remember when we assembled the robot, we connected the motors to the back of the motor shield, in ports M3, and M4. So, in the top part of the code, go ahead and change out the port from 1, to 3 like this:

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(3);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
When you see two forward slashes // in front of text, that is called commenting. Anything written after the // will be ignored by the Arduino. It is a way to communicate with whomever is reading your code.

Place your car on top of a cup or mug so the wheels are not touching the ground. Select Adafruit Feather 32u4 as the board under the Arduino Tools menu, and upload this code to your Feather through the USB connector.

Notice how just the one wheel is going forward, then backward. If you update the code to this:

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(4);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

With the motor port changed to 4, the other motor should now run. That is really the basics of how we will go about controlling the race car.

Let's Get Moving

Now that you know how to make either motor go forward and backward with the example sketch, let's learn the rest of the motor controller basics and write a sketch to get this robot moving. At the top of your sketch, you just need to call out both motors. To do this, you just need to uncomment the myOtherMotor line of code by removing the two forward slashes before the code. Then, set the second motor to 3 like so:

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(4);
// You can also make another motor on port M3
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(3);

In this case, we have myMotor set to M4, which is our right side motor, and myOtherMotor is set to M3, our left side motor.

Using the names myMotor, and myOtherMotor makes things really hard for us to remember which motor is which. So, we can simply change the variable name to whatever we want. Let's simply call them leftMotor, and rightMotor like this:

// Set up the left motor on port M4
Adafruit_DCMotor *leftMotor = AFMS.getMotor(4);
// Set up the right motor on port M3
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

Of course, now that we have changed the variable name, we need to replace any instance of myMotor with leftMotor, and myOtherMotor with rightMotor.

Before we start driving forward, backward, or turning; we need to tell the motor controller how fast we want the motors to go. This is done using the setSpeed function. If we want to make both motors go full speed ahead, we would set them both to 255 like this:

// Set the speed to start, from 0 (off) to 255 (max speed)
  leftMotor->setSpeed(255);
  rightMotor->setSpeed(255);

To make your robot go forward, all we need to do is tell each motor to go forward like this:

leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD!
rightMotor->run(FORWARD); //RIGHT MOTOR FULL STEAM AHEAD!

All right, let's put this into a full sketch and give it a shot. For this sketch, Go ahead and upload this sketch to your robot. I have slowed down the speed for safety, but you can update to whatever you want. Even though it is slowed down, don't forget to set your race car on a mug or a cup to keep the wheels off the ground.

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Set up the left motor on port M4
Adafruit_DCMotor *leftMotor = AFMS.getMotor(4);
// Set up the right motor on port M3
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - Robot Test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
}

void loop() {
  // Set the speed to start, from 0 (off) to 255 (max speed)
  leftMotor->setSpeed(100);
  rightMotor->setSpeed(100);
  
  leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD!
  rightMotor->run(FORWARD); //RIGHT MOTOR FULL STEAM AHEAD!
}
Is your race car still not going forward? Be sure to flip the switch on your battery box to 'on'

Ok, so we have the robot going forward. To put the robot in reverse, you just need to change FORWARD to BACKWARD. Turning is just as easy. To turn right, you just need to turn off the right motor, and go FORWARD with the left motor. This brings us to the next bit of motor controller code you need to know, RELEASE. Instead of FORWARD, or BACKWARD, you can also use RELEASE. RELEASE is like pulling the plug on the motor. It will ignore speeds, and direction, and just stop what it is doing. The code for RELEASE looks like this:

leftMotor->run(RELEASE);
rightMotor->run(RELEASE);

If we take that to our sketch, we can now make the robot turn in circles by releasing the right motor, and going forward with the left motor:

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Set up the left motor on port M4
Adafruit_DCMotor *leftMotor = AFMS.getMotor(4);
// Set up the right motor on port M3
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - Robot Test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
}

void loop() {
  // Set the speed to start, from 0 (off) to 255 (max speed)
  leftMotor->setSpeed(100);
  rightMotor->setSpeed(100);
  
  leftMotor->run(FORWARD); //LEFT MOTOR FULL STEAM AHEAD!
  rightMotor->run(RELEASE); //RIGHT MOTOR FULL STOP!
}

That just about covers the basics, now let's take this to the next level...

Last updated on 2017-07-12 at 04.14.02 PM Published on 2017-06-01 at 12.22.38 PM