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 rover code. Click the button below to download.
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 phone (which we will use in the next step). 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.
Text editor powered by tinymce.