Code for Your Race Car

To take this robot to the next level, we are going to modify James De Vito's awesome code for the similar Red Robot Rover. In his code, he has multiple control methods where you can either use the Bluefruit App controller or use your phone's accelerometer to drive your robot. For this example, I am going to simplify things and we will just use the control pad. This will free up the four auxiliary buttons for some customization which we will cover in a couple steps.

Libraries

For this code to work, we will need a couple libraries. See the previous step on a really easy way to install these libraries using the Arduino Library manager.

First up is the Adafruit BLE library. Learn more about this library and download it here.

Next, we will need the Adafruit MotorShield library. Learn more about this library and download it here.

The Code

Once you have the libraries installed, you will need to download the updated code. Click the button below to download. Then, double-click the archive to unzip it, and then move the MiniRaceCar folder to your Arduino sketches directory.

If you take a good look through this code, you will see it isn't so much more complicated than what we learned in the previous step. There is some complicated code that deals with the Bluetooth connection to your mobile device (which we will use in the next step). This version of the sketch also includes code for reading and streaming the temperature and humidity sensor data, which we'll look at a bit more later.

One neat feature we added is a way to slowly speed up the motors to full speed so it doesn't pop a wheelie when you take off going forward with this bit of code:

// speed up the motors
      for (int speed=0; speed < maxspeed; speed+=5) {
        L_MOTOR->setSpeed(speed);
        R_MOTOR->setSpeed(speed);
        delay(5); // 250ms total to speed up
      }

We just use a simple for loop to slowly ramp up the speed until it hits max speed. Learn more about for loops here.

Last updated on 2017-07-11 at 04.26.28 PM Published on 2017-06-01 at 12.22.38 PM