Arduino UNO Robot Motion Loop (9 Steps)

In my previous articles I showed you how to select hardware for an Arduino UNO Robot and then how to assemble it. In this article I'll walk you through how to upload motion loop code to test it.

This is part of my free mini-course: How to Build an Arduino UNO Robot. Which I'm making available as a series of blog posts.

Step 1. Mount the robot for testing

While programming the robot, the wheels might spin. This will be a problem when the robot is tethered to your laptop.

Find a box that is narrower than the space between the two robots wheels and place the robot on top of it. Make sure the wheels aren't touching the table or the box.

Step 2. Remove the VIN (power) jumper from the motor shield

For testing, the motor shield should use external power.

  • Remove the VIN (power) jumper from the motor shield and put it in a safe place

This makes sure that the motor power supply is not feeding into the Arduino while your laptop is hooked up to it.

Step 3. Plug in the battery pack

  • Plug the male connector from the battery pack into the female connector on the robot
    • The adapter that you connected the switch and power header to in the previous article
  • If the board lights up, flick off the power switch

While the robot is in a stationary position you can just lay the battery pack on the table next to it.

Step 4. Plug the cable into the Arduino

  • Plug the cable into the Arduino and your laptop

Step 5. Launch the Web editor

For working with the Arduino Uno WiFi Rev2 I've found it easer to use the Web editor.

Open up the editor at:

If you've never worked with the Web IDE before you will need to signup. You will be prompted when you select an Arduino board to install a plugin. That is so the IDE can upload to and monitor the board.

Create a new sketch.

Step 6. Paste in the motion loop code

Paste the code below into the new sketch, overwritting the previous contents:

/*
 * Author: Mitch Allen
 * Web Site: https://desertbot.io
 *
 * Reference: https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/using-dc-motors
 */
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);

// Valid values: 0 - 255
// Robot may not move below 50
int speed = 50;

void robot_stop() {
  motor1->run(RELEASE);
  motor2->run(RELEASE);
}

void robot_forward() {
  motor1->run(FORWARD);
  motor2->run(FORWARD);
}

void robot_backward() {
  motor1->run(BACKWARD);
  motor2->run(BACKWARD);
}

void robot_spinRight() {
  motor1->run(BACKWARD);
  motor2->run(FORWARD);
}

void robot_spinLeft() {
  motor1->run(FORWARD);
  motor2->run(BACKWARD);
}

void setup() {
  AFMS.begin();

  motor1->setSpeed(speed);
  motor2->setSpeed(speed); 
}

void loop() {
  // Forward
  delay(3000);
  robot_stop();
  delay(3000);
  robot_forward();

  // Backward
  delay(3000);
  robot_stop();
  delay(3000);
  robot_backward();

  // Right
  delay(3000);
  robot_stop();
  delay(3000);
  robot_spinRight();

  // Left
  delay(3000);
  robot_stop();
  delay(3000);
  robot_spinLeft();
}

Code Description

The code above does the following:
* Imports the Arduino software libraries that work with the V2 Motor Shield
* Defines global pointers to reference each of the two motors
* Defines a global speed variable to control the motors
* Below 50 it may not even work
* The maximum value should not be more that 255 (full speed)
* At speeds above 75, secure the battery pack or it may fly out of the robot
* Defines a series of functions to stop, move and turn the robot
* Calls the standard Arduino setup function
* The function initializes the motor driver and sets the speed of each motor
* Calls the standard Arduino loop
* The function defines a loop where the robot moves and turns
* The delay function is called to pause between each action
* Defined in milliseconds (3000ms = 3s)

Step 7. Upload the code to the board

  • Above the edit window, search for Arduino Uno WiFi Rev2
  • Click the ... to save the file under a new name (like "arduino_uno_motion_loop")
  • Click the Upload and Save (arrow) button
  • You may see some errors fly by at the bottom of the screen
  • If the uploading ends with a green Success message the upload was successful

Step 8. Test the code while tethered

  • Double check and make sure that the robot wheels are not touching anything
  • Flick the power button on
  • The robot might pause for three seconds, then go through the motion loop
  • You should see the wheels spin forward, backward, rotate as if turning left, then right

Step 9. Test on battery power

  • Turn off the power switch on the robot
  • Disconnect the cable between the Arduino board and your laptop
  • Put the VIN (power) jumper back on the motor shield
  • Tuck the battery pack inside the robot
    • Make sure the weight is over the universal wheel
  • Put the robot in the middle of a room
  • Flick on the power switch
  • After a brief pause the robot should go through the motion loop
    • Repeatedly going forward, backward, turning left and turning right

Once you get it working, be sure to leave a comment and add a photo to one of the pins on the desertbot.io Arduino board on Pinterest.

Troubleshooting

The robot lights up but doesn't move

  • If the robot is no longer tethered, did you put the power jumper back on?

The robot whines and doesn't move

  • It's possible that the batteries are low or the speed is too low
  • Charge the batteries and / or increase the speed value in the code

The robot doesn't end up in the exact spot after the loop

This is not a precision instrument. It's a device made from plastic running on a carpet with no sensors to orient itself. After several iterations the robot may be close to where it started, but not exactly.

The robot tilts backwards

  • Push the battery pack as far over the universal wheel as it goes
  • If that still fails, try adding more weight or another universal wheel on the other side

Code verification fails

  • Try Upload and Save and see if it still works

The Arduino Web Editor doesn't see the board

If I plug in the robot first, then start the editor, it doesn't see the board.

  • Unplug the board from your computer, then plug it in again
  • If that doesn't work, try this:
    • option 1:
    • flip the power switch
    • option 2 (only if nothing else works):
    • turn the power switch off
    • unplug the robot from your computer
    • put the power jumper back on the motor shield
    • plug the robot back into your computer
    • if still nothing, turn the power switch back on (make sure the wheels are not touching anything or the ground)

Conclusion

In this article you learned how to:
* Attach an Arduino Uno WiFi Rev2 to the Web editor
* Upload motion loop code to a robot
* Test the robot while tethered
* Test the robot on its own battery power

Related Articles

References

  • Adafruit Motor Shield V2 - Using DC Motors - [1]