Instead of hard coding a specific speed for your robot, here we will try out a couple different ways to adjust your robot speed without having to constantly upload new code.
The first way we are going to adjust the speed is with a simple breadboard trim potentiometer. They are super cheap! Pick one up on the Adafruit Shop:

Potentiometers, or Pots for short, are variable resistors that allow us to send different voltages to the analog pin. Wiring it up is super simple. Just connect one of the outside pins to the 3.3V pin, and the other outside pin to ground. Then, connect the middle pin to the A0 pin on the Arduino 101.
The code to get this all working is incredibly simple. Just copy and paste in the code below to the top of your main loop in the Ada_CurieBot_RC code.
//Set your motor speed int reading = analogRead(A0); L_MOTOR->setSpeed(map(reading, 0, 1023, 0, 255)); R_MOTOR->setSpeed(map(reading, 0, 1023, 0, 255));
So, it should look something like this now:
void loop(void) { //Set your motor speed int reading = analogRead(A0); L_MOTOR->setSpeed(map(reading, 0, 1023, 0, 255)); R_MOTOR->setSpeed(map(reading, 0, 1023, 0, 255)); // read new packet data uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT); if (len == 0) return; // Read from Accelerometer input if( accelMode() ) { lastAccelPacket = millis(); modeToggle = true; return; } }
What we are doing here is reading that analog pin, and the Arduino 101 is going to pull a number from 0 to 1023, depending on which direction the arrow is facing on your pot. Because the motor controller needs a value from 0 to 255, we are using the map() function.
Open the Ada_CurieBot_RC_potSpeed sketch in the Arduino IDE and upload it to your robot.
Then, turn the pot, then press forward on the controller in the Bluefruit LE app to see how it works. Try turning knob of the potentiometer different amounts in both directions to adjust the speed.
Using the Controller to Control Speed
If you would rather just control your robot's speed using the extra 4 buttons on the controller, it is also pretty easy to set up. First off, we need to set up a global speed variable. Anywhere above the sketch setup, add in something like this:
int robotSpeed = 100;
In the main loop, we can then use that variable to set the motor speed using:
L_MOTOR->setSpeed(robotSpeed); R_MOTOR->setSpeed(robotSpeed);
Then, all we need to do is increment the speed up every time we press the 1 button, and down every time we press the 3 button.
if(buttnum == 1){ if(robotSpeed <= 245){ robotSpeed = robotSpeed + 10; } } if(buttnum == 2){ } if(buttnum == 3){ if(robotSpeed >=10){ robotSpeed = robotSpeed - 10; } } if(buttnum == 4){ }
Open the Ada_CurieBot_RC_buttonSpeed sketch in the Arduino IDE and upload it to your robot.
Page last edited January 31, 2017
Text editor powered by tinymce.