BennyBot First Movement Tutorial (Arduino)

From Edubot Wiki
Jump to: navigation, search

So you have built your first robot and you have setup your Arduino IDE. Lets write a small program that will make your robot move.

Move your Robot forward

The following program will make both motors move forward:


// pinout http://www.bennybot.co.za/index.php/Bennybot_Controller_Pinout
#define MOTOR1DIRECTION 9
#define MOTOR2DIRECTION 8
#define MOTOR1PWM 11
#define MOTOR2PWM 10

void setup() {
  // set direction pins as output
  pinMode( MOTOR1DIRECTION, OUTPUT );
  pinMode( MOTOR2DIRECTION, OUTPUT );

  // set direction pins high (forward)
  digitalWrite( MOTOR1DIRECTION, HIGH);
  digitalWrite( MOTOR2DIRECTION, HIGH);

  // set the speed of our motors
  analogWrite( MOTOR1PWM, 128 );
  analogWrite( MOTOR2PWM, 128 );
}

void loop() {

}

Type or Copy/Paste this program into your Arduino IDE. Upload it to your BennyBot Controller (Remember to put it in programming mode first).

Note. This program will make your robot move forward without stopping. Make sure it won't drive off a high surface.

Once it has upload the program successfully, press the reset button to start your program.

If all went well and your connections are good then your robot should have moved forward.

If your robot went into circles then one of your motors are wired the wrong way around, to fix it just swop the motor connection around.

If your robot went straight backwards then both motors are connected incorrectly, just swop the wires for each motor.

So how does the program work ? Lets go through it bit by bit.

// pinout http://www.bennybot.co.za/index.php/Bennybot_Controller_Pinout
#define MOTOR1DIRECTION 9
#define MOTOR2DIRECTION 8
#define MOTOR1PWM 11
#define MOTOR2PWM 10

The first part is easy, we made some defines to the pins for our motor control. This means that everywhere in the program we reference to these defines the compiler will replace it with the correct number.

For example:

digitalWrite( MOTOR1DIRECTION, HIGH);

makes more sense than:

digitalWrite( 9, HIGH);

This makes reading our code easier and you will be happy you did this 6 months later when you are reading the code and trying to figure out what it does.

For the same reason, any lines that start with // are comments to our program. These are ignored by the program and is used for adding our own notes to our program. Get in to the habit to add comments to your code to explain what a certain line does.

Next up in our code we have 2 functions. They are called setup() and loop().

void setup() {
	// code here runs once at startup
}

void loop() {
	// code here loops continually
}

The code you add in the setup() function is only run once when your program starts. We use this for doing initial setup. The loop function is used for our main program. The code you put in here will loop continually.

All our code for this test program is in the setup() function because we only need them to run once.

  // set direction pins as output
  pinMode( MOTOR1DIRECTION, OUTPUT );
  pinMode( MOTOR2DIRECTION, OUTPUT );

Digital Pins (GPIO) can be either used as inputs or outputs. We use INPUT if we want to receive digital signals from something to our robot, for example if we connect a button to our robot.

We use OUTPUT if we want to send a signal to something connected to our robot, for example, we would use OUTPUT to light up a LED connected to that pin.

In our piece of code we set both the MOTOR1DIRECTION and MOTOR2DIRECTION pins to output. These outputs tell our motor driver whether we want the motor to go forward or backwards. A HIGH signal makes the motor move forward and a LOW signal makes the motor move backwards.

To set these two pins to either HIGH or LOW we use the digitalWrite function. The following piece of code tells the motor driver that we want the motors to move forward:

  digitalWrite( MOTOR1DIRECTION, HIGH);
  digitalWrite( MOTOR2DIRECTION, HIGH);

The last piece of code is where we set the speed of the motors.

  analogWrite( MOTOR1PWM, 128 );
  analogWrite( MOTOR2PWM, 128 );

The motors are driven by a PWM signal. This means we can set the speed of a motor or the brightness of an LED. For this we use the analogWrite function. Our speed can be any value from 0 (Stopped) to 255 (FullSpeed). In the program we have set it to 128 which should be about halfspeed.

Note: We only need to call the analogWrite function once to set the speed of the motor. It will now always run at that speed until we set it again.

Info: You will notice that we didn't set the MOTOR1PWM and MOTOR2PWM as outputs. The analogWrite function will only work on outputs so it will set them accordingly automatically.