2013-02-01T07:32:05Z

Building an Arduino Robot, Part IV: A (Not So) Basic Robot Firmware

Arduino Robot

Welcome to the fourth article in the tutorial series in which I'm building a remote controlled Arduino based vehicle robot.

Here is the list of articles I have published:

In the previous article I completed the assembly of my robot vehicle. Today, I'm going to write an Arduino sketch for it that makes the robot go forward.

"Just to move the robot forward?", you may ask. What's so interesting about simply making the robot run forward?

Well, the idea is to not only make the robot move forward, but to write the sketch in such a way that allows me to later add more complex functionality without having to start over from scratch. I'm basically laying out a solid foundation on which I will stack the more complex pieces later.

Once again I want to say that it is not my intention to do a full and detailed coverage of the C++ programming language in these articles. I will share my experience and provide a few pointers, but I recommend you read up on C++ on your own if you have no experience with the language.

Goals

I mentioned what functionality I want my robot to have in past articles, but I did not discuss the goals I have for my project from a software engineering point of view.

Scalable Structure

For simple sketches the default sketch structure based on the setup() and loop() functions is sufficient. The sketches I wrote a couple of articles ago to test all the components of my robot fit in this category, I needed to test and learn to program these components and was able to accomplish both goals quickly and with very low effort.

The problem is that this structure does not scale well. For my robot project I will need to control two motors, the distance sensor and react to remote commands coming over bluetooth all at the same time. In the future I may want to extend it with even more controlled devices. Maybe additional distance sensors on the sides and back, or one looking down to detect when the vehicle reaches the end of a table. I may want to mount a wireless camera, or add sound capabilities. The possibilities are endless, and each new device added will increase the complexity of the sketch.

The loop() function would need to get pretty long and complex to do everything, and then making changes to it would become a nightmare, with any changes I make I could risk inadvertently breaking existing functionality. The sketch would end up being spaghetti code.

For this project I will use object oriented techniques to organize the code of my sketch and make it easier to write, test, maintain and modify.

Device Abstractions

I mentioned before that I will be releasing my sketch as open source. It is safe to assume that other people will build their own robot vehicles using different parts than those I put in mine. For example, someone may use a different motor driver board, or may want to use wi-fi for the remote control instead of bluetooth. If I build my sketch with the assumption that the components of the robot are just those that I'm using on mine then only a small few would be able to benefit right away and most will have to hack on it to make it work for them.

So instead, I will think about the functionality I want to implement in general terms, and then will write device drivers for the modules that I need.

This will give me or others the freedom to replace parts in the robot without having to modify the main sketch. Switching from bluetooth to wi-fi will then just require writing a new remote communications driver. The main sketch will talk to either the bluetooth or wi-fi drivers and will not know the difference!

Configuration

I spoke about configuration before. The idea here is that it should be easy to configure the robot software for the actual hardware. The best example is the use of pins in the Arduino board. I will not hard code any pin numbers. Instead, I will add #define constants at the top of the sketch with all the pins, so that changing the pin assignments becomes a trivial task.

But configuration is not limited to pin numbers. There are also some operational defaults (like the distance to a wall or obstacle when the robot stops and turns) that can also be considered part of the configuration and will be represented by constants at the top of the sketch for users to easily modify.

Smarter Timing

In simple sketches one tends to overuse the delay() function as an easy way to make actions last a certain amount of time. I'm guilty of this myself, as I've used delay() in all my test sketches.

But for a real project that needs to do many things at once this is impractical. For example, what happens if the robot approaches a wall while in the middle of a delay() call? I can tell you what happens, nothing happens! The robot will crash into the wall without having a chance to "see" the wall and act accordingly.

Unfortunately the Arduino board does not support multithreading, which is the ideal way to make a program do many things at once. In a multithreaded environment a thread calling delay() would not prevent other threads from running. But on the Arduino platform there is a single thread of execution, so calling delay() makes the entire program go to sleep, and that is not good for my needs.

So I will not be using delay() for anything except throw away tests. But how can I keep track of time without delay()?

Part of the secret is in making the loop() function do only a small amount of work and return. The loop() function will then get called at a really fast rate (remember that it gets called repeatedly until the board is powered off). Each time loop() is called I can find out what's the up time. Knowing the current time will enable me to implement my own non-blocking delay() equivalent, as you will see soon.

Enough talk... let's write a device driver!

I said earlier that today I'm making the robot move forward, so I'm going to start by writing a device driver that knows how to control motors.

A device driver is simply an abstraction for the intended device. I'm now writing one for a motor, so the basic operation that this driver should know how to do is to set the speed of the motor.

I will store my device drivers in header files, which are files that have a .h extension. For small modules using a header file is a good choice. For larger modules the norm is to store the code in a source file with extension .cpp and then write a small header file that only contains the declaration of the public functions from the source file. Header files work for me at this time, if in the future a module grows to a decent size I can always move the code to a .cpp file.

The intention is that the motor device driver will allow the main sketch to work with different motor controllers using the same function calls, so the first task when defining a driver is to decide what the common functions of the driver will be.

Here is the code for my motor device driver, which I will store in a file called motor_driver.h:

/**
 * @file motor_driver.h
 * @brief Motor device driver definition for the Michelino robot.
 * @author Miguel Grinberg
 */

namespace Michelino
{
    class MotorDriver
    {
    public:
        /**
         * @brief Change the speed of the motor.
         * @param speed The new speed of the motor.
         *  Valid values are between -255 and 255. 
         *  Use positive values to run the motor forward, 
         *  negative values to run it backward and zero to stop the motor.
         */
        virtual void setSpeed(int speed) = 0;

        /**
         * @brief Return the current speed of the motor.
         * @return The current speed of the motor with range -255 to 255.
         */
        virtual int getSpeed() const = 0;            
    };
};

If you are unfamiliar with C++ then there are a number of things here that need an explanation.

Any text that appears between /* and */ is considered a comment, so it will be ignored by the C++ compiler. There is an alternative format for comments that you may have seen, those use a // prefix and go through the end of the line. Both formats are valid, but in this case since I'm writing multi-line comments I'm using the /* ... */ format.

The comments that I'm writing follow a standard comment format that can be used to produce automatic documentation with a tool called doxygen. It is always good practice to write class documentation in this format for any classes that others may need to use. This class defines the format for motor drivers, so those who want to implement other motor drivers will find the documentation useful.

I have inserted a comment block at the top to describe the contents of the file, and then one before each function to describe the function. Each comment line is prefixed with a keyword, for example @file for the name of the file, @brief for a brief description, @param for documenting a function parameter and @return for documenting the return value of a function.

The namespace keyword groups all the definitions inside the { and } under a user defined name. This is helpful to avoid name collisions with other modules, so it is always a good idea to enclose your own code in a namespace. I will put all my code under the Michelino namespace.

The class keyword defines a structure that groups data and functions together into an entity called object. My motor driver class is called MotorDriver and just contains two functions, which in the context of a class are called methods. The setSpeed and getSpeed methods will control a motor, but note that unlike the AF_Motor library that I used before here I'm not defining what kind of motor controller is used for the task. The main sketch will just call these methods without knowing how they are implemented, so to switch from one controller to another one just needs to re-implement these two functions for the selected controller.

The public: keyword that appears before the methods indicates that the declarations that follow are accessible to anyone. You will see later that some classes have contents that are hidden from the outside world.

The virtual keyword that starts each method declaration indicates that these methods may be overwritten by a subclass, and the = 0 at the end of the declarations indicates that the methods do not have an implementation. These two attributes combined are used to make sure that the only way to provide an implementation for these methods is in a subclass. I will show what this means in a moment.

Finally, the const keyword that appears in the getSpeed method indicates that this method is constant, meaning that this method does not change the internal state of the object.

Setting the technical details of writing a C++ class aside, the important point is that device drivers that adopt the above interface should be able to control a DC motor with two operations, setSpeed() and getSpeed(). The speed values are given in the -255 to 255 range, with positive speeds moving forward, negative speeds moving backward and zero making the motor stop.

The Adafruit motor shield device driver

Now I have a very clear idea of what motor drivers for my robot need to do, so I'm ready to write a specific implementation for my Adafruit motor shield.

I will store the Adafruit motor shield driver in a file called adafruit_motor_driver.h. Here is the code:

/**
 * @file adafruit_motor_driver.h
 * @brief Motor device driver for the Adafruit motor shield.
 * @author Miguel Grinberg
 */

#include "motor_driver.h"

namespace Michelino
{
    class Motor : public MotorDriver
    {
    public:
        /*
         * @brief Class constructor.
         * @param number the DC motor number to control, from 1 to 4.
         */
        Motor(int number)
            : MotorDriver(), motor(number), currentSpeed(0)
        {
        }

        void setSpeed(int speed)
        {
            currentSpeed = speed;
            if (speed >= 0) {
                motor.setSpeed(speed);
                motor.run(FORWARD);
            }
            else {
                motor.setSpeed(-speed);
                motor.run(BACKWARD);
            }
        }

        int getSpeed() const
        {
            return currentSpeed;
        }

    private:
        AF_DCMotor motor;
        int currentSpeed;
    };
};

So what's new here? I start by including the motor driver header file. The Adafruit driver derives from the generic driver definition I wrote above so that definition needs to be accessible.

The class definition in this file looks a bit different:

class Motor : public MotorDriver

This says that I'm creating a class called Motor that derives from class MotorDriver. This means that class Motor will follow the protocol established by class MotorDriver.

The class has a public section that starts with a constructor:

Motor(int number)

This is a method with the same name as the class (Motor in this case). Constructor methods are called when an object is created and are intended to set the initial state of the object. This constructor takes the motor number as an argument.

The Motor constructor has an initializer list:

: MotorDriver(), motor(number), currentSpeed(0)

This is a list of items that will be initialized when an object of this class is created. The first item just invokes the constructor of the parent class, the generic driver MotorDriver. I did not include a constructor in this class, but the compiler will generate an empty one for me. It is good practice to always initialize the parent class even when there isn't a constructor there, because in the future a constructor might be added.

The two remaining items in the initializer list are member variables of this class. If you look at the bottom of the class there is a private block that defines two variables named motor and currentSpeed. The motor variable is of type AF_DCMotor, the class defined by the Adafruit motor shield library. To initialize this variable I have to invoke its constructor, which if you recall from a previous article takes the motor number as argument. I received the motor number as an argument to my own constructor, so I just pass it on here. The currentSpeed variable is of type int, so I just initialize it to 0 so that it has a known starting value.

The body of the Motor constructor is empty, since the only thing I need is to initialize the member variables.

What follows is the implementation of the setSpeed and getSpeed methods for this specific motor controller, using the open source AF_Motor library from Adafruit, which must be installed separately (see my previous article for instructions). This library requires two functions to be called to configure a motor, one to set the speed and another one to set the mode. I have defined my motor driver to use a simpler way to configure the motor, with just a speed that can be positive, negative or zero. The implementation of setSpeed translates my format to the one required by the Adafruit motor shield library. I also store the current speed in a member variable, so that I can then return it in getSpeed.

At the bottom there are the two private member variables that this class uses. These variables are declared as private because I do not want them to be accessible from outside the object that owns them. I want any changes the motor or its speed to be accomplished via the public functions of the driver (following the encapsulation principle), so a good way to prevent me from accidentally using the member variables directly is to make them private. A private member variable can only be modified from methods that belong to the object, but it is not accessible from any other place in the code. A good reason to hide these member variables is that other motor drivers will need different ones, so as long as the code outside of this class only calls setSpeed() and getSpeed() swapping out this driver for another one should not be a problem.

If you are using a different motor controller than mine on your robot, then all you need to do is copy the adafruit_motor_driver.h file to <your_controller>_motor_driver.h and implement the class as appropriate for your controller. The only requirement is that the new class is also called Motor and has the same two public functions. The two classes will then be conceptually equivalent, so the main sketch can use either one of them.

Using the device driver

Now I have a completed device driver for my Adafruit motor shield. But how do I use it on a sketch?

This is actually simpler than you may think. Here is an example sketch that starts a motor at full speed using the Adafruit driver that I just built:

#include <AFMotor.h>
#include "adafruit_motor_driver.h"
#define MOTOR_INIT 1

Michelino::Motor motor(MOTOR_INIT);

void setup()
{
    motor.setSpeed(255);
}

void loop()
{
}

The #includes at the top bring in the Adafruit motor library and my driver for it.

Note how the motor object is created:

Michelino::Motor motor(MOTOR_INIT);

The notation Michelino::Motor tells the compiler to locate the Motor class inside the Michelino namespace.

Once the motor object is created it can be controlled using the driver functions setSpeed and getSpeed.

Now let's assume that later you want to make the above sketch work with another motor shield, say one that is called "x", for which you have written a device driver. Then you can just add a few lines of code to the sketch, as follows:

// enable one of the motor shields below
//#define ENABLE_ADAFRUIT_MOTOR_DRIVER
#define ENABLE_X_MOTOR_DRIVER

#ifdef ENABLE_ADAFRUIT_MOTOR_DRIVER
#include <AFMotor.h>
#include "adafruit_motor_driver.h"
#define MOTOR_INIT 1
#endif

#ifdef ENABLE_X_MOTOR_DRIVER
#include <XMotor.h>
#include "x_motor_driver.h"
#define MOTOR_INIT 2
#endif

Michelino::Motor motor(MOTOR_INIT);

void setup()
{
    motor.setSpeed(255);
}

void loop()
{
}

At the top I'm defining two #define constants, but one of the is commented out. This is a pretty easy way to provide configuration, you just uncomment the one constant that corresponds to the part that you have.

An #ifdef ... #endif block will only be seen by the compiler if the constant that follows the #ifdef is defined. Since only one of the motor shield constants will be defined I'm effectively loading only one of the device drivers into the sketch.

In the example above the sketch can be switched between the Adafruit shield using motor #1 and the X motor shield using motor #2 just by changing which of the #define statements at the top is used!

The main sketch

Now that I have a really good solution for controlling my hardware parts I need to think about how the main sketch will be structured.

As I mentioned before, having a large piece of code in the loop() function is not ideal because large and complex functions difficult to maintain. If I created a class to represent a motor, then why not also create a class to represent the whole robot?

Here is a sketch that defines a starting point class for the robot. This file will be named michelino.ino (Arduino sketches use the .ino extension):

/**
 * @file michelino.ino
 * @brief Arduino robot vehicle firmware.
 * @author Miguel Grinberg
 */

#define ENABLE_ADAFRUIT_MOTOR_DRIVER

#ifdef ENABLE_ADAFRUIT_MOTOR_DRIVER
#include <AFMotor.h>
#include "adafruit_motor_driver.h"
#define LEFT_MOTOR_INIT 1
#define RIGHT_MOTOR_INIT 3
#endif

namespace Michelino
{
    class Robot
    {
    public:
        /*
         * @brief Class constructor.
         */
        Robot()
            : leftMotor(LEFT_MOTOR_INIT), rightMotor(RIGHT_MOTOR_INIT)
        {
            initialize();
        }

        /*
         * @brief Initialize the robot state.
         */
        void initialize()
        {
            leftMotor.setSpeed(255);
            rightMotor.setSpeed(255);
        }

        /*
         * @brief Update the state of the robot based on input from sensor and remote control.
         *  Must be called repeatedly while the robot is in operation.
         */
        void run()
        {
        }

    private:
        Motor leftMotor;
        Motor rightMotor;
    };
};

Michelino::Robot robot;

void setup()
{
    robot.initialize();
}

void loop()
{
    robot.run();
}

The sketch should not present any surprises. I begin by loading my motor shield driver, using the #ifdef technique I showed before. When and if I write new motor drivers I will add new constants and #ifdef blocks to load them.

My robot class will be appropriately named Robot and will be inside the Michelino namespace. It will initially have two private data members that represent the two motors named leftMotor and rightMotor. These will be initialized in the Robot class constructor's initializer list. The class has two public methods, initialize() and run() that will be the class counterparts to setup() and loop(). In this early version of the class the initialize() method starts the two motors at full speed forward, and run() does nothing.

After the class is defined an instance is created in the global scope, and the setup() and loop() methods simply call the initialize() and run() methods of this object.

What do you think will happen when you run this sketch? The robot will move forward! I took a few turns and twists, but I finally delivered on my promise.

How to implement delays

Before I end this article I'm going to show you how delays can be implemented without actually using the delay() function, so that the robot can do other things while it waits, like receiving and executing remote commands.

To show you how this is done, I'm going to modify the above sketch to make the robot start by waiting for five seconds, then moving forward for eight seconds, then repeating the cycle.

First, I'm going to add the concept of state to the robot. For this example I will have two possible states, stopped and running. To define these states in the sketch I'm going to use a C++ enumeration:

private:
    enum state_t { stateStopped, stateRunning };
    state_t state;

The enum statement creates a user defined enumerated type named state_t. The second statements defines a new member variable named state of that type. The state member variable wil have one of two possible values, stateStopped or stateRunning. I could have easily used an int variable and call state 0 the stopped state and state 1 the running state, but after you see me using this variable I'm sure you will agree with me that using enumerated types makes the code a lot more readable and easier to understand.

I'm going to add yet another member variable to the Robot class that will help me count time:

private:
    unsigned long startTime;

The type of the startTime variable is unsigned long. This is the type in which time units are returned by the function millis() provided by the Arduino software libraries.

Next, in the initialize() method instead of starting the motors I'm now going to initialize the robot state and record the current time:

    void initialize()
    {
        state = stateStopped;
        startTime = millis();
    }

And finally, in the run() method is where all the magic happens:

    void run()
    {
        unsigned long currentTime = millis();
        unsigned long elapsedTime = currentTime - startTime;
        switch (state) {
        case stateStopped:
            if (elapsedTime >= 5000) {
                leftMotor.setSpeed(255);
                rightMotor.setSpeed(255);
                state = stateRunning;
                startTime = currentTime;
            }
            break;
        case stateRunning:
            if (elapsedTime >= 8000) {
                leftMotor.setSpeed(0);
                rightMotor.setSpeed(0);
                state = stateStopped;
                startTime = currentTime;
            }
            break;
        }
    }

The run() method is repeatedly called through the loop() function, so here I can easily check how much time has passed by substracting the startTime that I initialized in initialize() from the current time provided by the millis() function. I'm storing both the current and elapsed time in two local variables that I named currentTime and elapsedTime.

What's left is to check if I'm done waiting, and if I am, then change the state of the robot. Since the wait period is different depending on which state the robot is in, I've decided to use a switch statement, which looks at the value of an expression and then executes the block of code that begins at the matching case statement and ends with the break statement.

So, when the robot is in the stopped state the following code will execute:

            if (elapsedTime >= 5000) {
                leftMotor.setSpeed(255);
                rightMotor.setSpeed(255);
                state = stateRunning;
                startTime = currentTime;
            }

This code is hopefully self-explanatory. If the elapsed time did not reach five seconds then nothing will happen, the run() function will return without making any changes, and a little bit later will be called again. Eventually, the elapsedTime will become 5000 milliseconds or more, so the code inside the if statement will execute. And those statements start the motors, change the state to stateRunning and reset the startTime to the currentTime, so that elapsed time is reset to zero.

The next time run() executes the state will be stateRunning, so the other block in the switch statement will execute:

            if (elapsedTime >= 8000) {
                leftMotor.setSpeed(0);
                rightMotor.setSpeed(0);
                state = stateStopped;
                startTime = currentTime;
            }

And this works exactly like above but waits eight seconds instead of five.

Doing things in this certainly more complicated than using delay():

        void run()
        {
            leftMotor.setSpeed(0);
            rightMotor.setSpeed(0);
            delay(5000);
            leftMotor.setSpeed(255);
            rightMotor.setSpeed(255);
            delay(8000);
         }

But the delay() solution pictured above is limited in that nothing happens during those five and eight second waits. With the elapsed time solution it will be easy for me to make the robot do other things while waiting, like stopping when it gets too close to a wall or responding to remote commands sends over bluetooth.

Final words

To close this article, here is a video that shows my robot running the above michelino sketch:

In the next article I will incorporate the distance sensor into the firmware to prevent the robot from running into walls or other obstacles.

As always, you are welcome to send me your questions in the comments below. If you wish to download or review my sketch I have uploaded it to github as version 0.1 (download or browse).

I hope to see you in the next article!

Miguel

82 comments

  • #51 Miguel Grinberg said 2015-01-28T15:21:50Z

    @sankar: you need to include a motor driver. Look in the Github project, there are a couple of implementations for the Motor class. Find the one that is appropriate to your hardware and include it above this code.

  • #52 roberto said 2015-02-01T09:25:24Z

    Dear Miguel, very amazing your project, I am interesting to remake your project, could you please help me sending the complete code? I am not strongly skillen on C+ language. Also I am interester to develop a Traker robot that follow (not avoid) a mouvment obstacle, could you help me? tks in advance Roberto Rome - Italy

  • #53 Miguel Grinberg said 2015-02-01T18:05:32Z

    @roberto: the project is on github: https://github.com/miguelgrinberg/michelino

  • #54 Jacob Betz said 2015-02-22T14:39:44Z

    Dear Miguel,

    Awesome Project, its really cool! I have a question I am using a SeeedStudio V2 motor shield and I am new to all of this so i was wondering what would i need to change to use the code you have as the code for the Seeed Studio Shield. I am using that,a Arduino Uno, and a ping sensor.

    Thanks Jacob Betz

  • #55 Miguel Grinberg said 2015-02-22T23:57:22Z

    @Jacob: looks like there is a library for your motor driver. What you need to do is to create a driver for your motor shield. Look in my github project: https://github.com/miguelgrinberg/michelino, file adafruit_motor_driver.h is a good model for what you need to do. Create another file called seeedstudio_motor_driver.h and implement the same methods using the seedstudio library. If you do it, then please share the driver so that I can add it to the project for others to use.

  • #56 Magnus said 2015-02-23T12:33:58Z

    Can someone explain some of theese errors for me?

    Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Uno" Deek_Robot_driver.ino:1:24: error: Motor.Driver: No such file or directory Deek_Robot_driver:6: error: expected class-name before '{' token Deek_Robot_driver:32: error: 'AF_DCMotor' does not name a type Deek_Robot_driver.ino: In constructor 'Gjerde::Motor::Motor(int)': Deek_Robot_driver:11: error: class 'Gjerde::Motor' does not have any field named 'MotorDriver' Deek_Robot_driver:11: error: class 'Gjerde::Motor' does not have any field named 'motor' Deek_Robot_driver.ino: In member function 'void Gjerde::Motor::setSpeed(int)': Deek_Robot_driver:19: error: 'motor' was not declared in this scope Deek_Robot_driver:20: error: 'FORWARD' was not declared in this scope Deek_Robot_driver:23: error: 'motor' was not declared in this scope Deek_Robot_driver:24: error: 'BACKWARD' was not declared in this scope

    Thank you :)

    Here is the code

    include "Motor.Driver"

    namespace Gjerde { class Motor : public MotorDriver { public:

    Motor(int number) : MotorDriver(), motor(number), currentSpeed(0) { } void setSpeed(int speed) { currentSpeed = speed; if(speed >= 0) { motor.setSpeed(speed); motor.run(FORWARD); }

    else { motor.setspeed(-speed); motor.run(BACKWARD); } } int getSpeed() const { return currentSpeed; } private: AF_DCMotor motor; int currentSpeed; }; };

  • #57 Jacob Betz said 2015-02-25T02:55:02Z

    Hello Miguel

    Ok so I was a little bit confused on what you meant above. (Sorry for the New guy questions) So when I make this new driver off of the old one would i just need to change the parts in the code where it says AF_DCMotor motor or more.

    Thanks Jacob

  • #58 Miguel Grinberg said 2015-02-25T06:39:37Z

    @Magnus: where is the Motor.Driver file that you included? Looks like it is missing.

  • #59 Miguel Grinberg said 2015-02-25T06:43:49Z

    @Jacob: you will need to implement the functions that make the motor run using the facilities provided by this library that works with your board.

  • #60 Magnus said 2015-03-02T07:44:18Z

    @Miguel Grinberg

    it's in the arduino folder, and I've done anything to make sure the Arduino get's the file from that folder where Motor.Driver is.

    I have a Deek Robot shield R3, would it make a difference?

  • #61 Miguel Grinberg said 2015-03-03T18:03:26Z

    @Magnus: not familiar with your motor shield. Does it come with code that you need to install as a library? Is that Motor.Driver file yours or is it part of the motor shield library?

  • #62 Magnus said 2015-03-09T07:54:10Z

    @Miguel Grinberg It does not come with any library, i looked up their website and the only item that they sold was the robot shield. searched around google for an hour or without finding anything. So i popped up my wallet and bought Adafruit's motor shield, so instead of saving money - I'm saving time :)

  • #63 Magnus Gjerde said 2015-03-11T12:16:26Z

    Hello Miguel I'm back with another problem, and I am google-ing myself to death on this error.

    This report would have more information with "Show verbose output during compilation" enabled in File > Preferences. Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Uno" core.a(main.cpp.o): In function main': C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:40: undefined reference tosetup' C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:43: undefined reference to `loop' java.lang.ArrayIndexOutOfBoundsException: 1

    at processing.app.Sketch.load(Sketch.java:189) at processing.app.Sketch.<init>(Sketch.java:137) at processing.app.Editor.handleOpenInternal(Editor.java:2193) at processing.app.Editor.handleOpenUnchecked(Editor.java:2089) at processing.app.Sketch.saveAs(Sketch.java:936) at processing.app.Editor.handleSaveAs(Editor.java:2299) at processing.app.Editor$7.actionPerformed(Editor.java:535) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.AbstractButton.doClick(AbstractButton.java:357) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266) at java.awt.Component.processMouseEvent(Component.java:6263) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6028) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    It's happening whenever I try to ''save as'' motor_driver.h , it's like the file wont accept being called .h at the end. The code is just as your's. I even copy/pasted it. it happens with all sketches.

  • #64 Miguel Grinberg said 2015-03-12T05:37:57Z

    @Magnus: the error suggests you don't have setup() and loop() functions in your application.

  • #65 Ted Snowberger said 2015-03-18T22:54:53Z

    I've been going off of your tutorial for a project that I need to do for one of my advanced classes. I'm basically clueless in the programming area and I could use some basic pointers. (In all seriousness) How do you add the motor_driver.h file? I could really use the help.

  • #66 Miguel Grinberg said 2015-03-21T21:39:25Z

    @Ted: what do you mean by "add"? You have a .ino file with your main code, and you can have additional code in external files. The .h files are included using #include, as shown above.

  • #67 Raif Deari said 2015-03-28T22:26:59Z

    Does anyone have the RocketBotr controller app? it dissapeared from everywhere

  • #68 Hayam said 2015-05-11T05:36:03Z

    Thanks for this tutorial. I just wanna ask about why you programmed it with Arduino then with C++?

  • #69 Miguel Grinberg said 2015-05-11T06:52:01Z

    @Hayam: Arduino is not a programming language, all Arduino sketches are written in C++.

  • #70 umayden said 2015-07-14T06:26:52Z

    hi there..awesome tutorial. the most detailed and well explained one i have seen so far . just one question though - my motors dont seem to be getting enough power for some reason. when i remove the ping wires and attach the motors , they run fine . but once i put em back . they sort of just shiver ; dont seem to be getting enough juice. alos the 9v lithium battery isnt working for some reason , the motors only run when the usb cable is attached. what could i be doing wrong. i have followed all of your instructions to the T

  • #71 Miguel Grinberg said 2015-07-16T06:07:38Z

    @umayden: hard to say, maybe you should try a separate power source for your motors, so that the rest of the components don't interfere.

  • #72 Javier said 2015-08-08T22:27:20Z

    First of all congratulate you and thank you for your tutorial. Excellent. I could assemble everything and it works with USB connection, but when I connect the battery box (I have 6 batteries), the LED lights power, but not working. What can be? Thank you.

  • #73 Robert kearbey said 2015-12-26T05:00:53Z

    Hi Miguel Robot runs forward and backward. Distance sensor works great. Issue: have copied the two files: adafruit_motor_driver.h and motor_driver.h but cannot transfer them into my Arduino library. When I click on "include library", neither shows up. If I add them to a sketch from computer files they appear at the top of the sketch. If I click on them the code for each are there. If I the "include or "motor_driver.h" compile says no file exists. Sorry but I am so new to this and need a little help to get past this. Thank you in advance for the time it may take to respond. Having so much fun to this point. Bob

  • #74 Miguel Grinberg said 2016-01-03T22:42:49Z

    @Robert: the .h files that are included with the michelino sketch are not an Arduino library. They are just regular C++ header files. They should be in the same directory as the michelino.ino file. The C++ compiler should be able to find them if they are there.

  • #75 Arun said 2016-02-17T14:06:02Z

    Hey Miguel I'm getting this error adafruit_motor_driver.h:7:26: fatal error: motor_driver.h: No such file or directory #include pls help

Leave a Comment