Purpose:

This is a section by section discussion/explanation of what the Motor Control code is doing.

Comments:

An important feature of all science is the need to communicate clearly what we are doing. What we hear all the time is that we will change jobs multiple times which means that someone else will very likely be trying to make sense of anything we create for our employer. Computer code is just another product but the same could be said of an Excel spreadsheet that you use and modify as part of your work. In all cases writing clear comments in your document that explain what is going on are very helpful to the next person looking at the document.

In sketches that we create for the robot there are two ways to indicate that you are making a comment that should be ignored by the robot. Both are evident near the begining of the Motor Control code.

/* */ : Any text which is in between these two symbols is considered a comment and is ignored by the robot. I, and most who work with the IDE use these symbols to create a robust description of what the sketch is intended to do as well as any references or core concepts that may be of interest to some future user. This method of indicating a comment is more commonly used for large blocks of writing.

// : For single lines or shorter comments the '//' notation is typically used. Any text that follows a '//' on a single line is ignored by the robot. Be careful -- as soon as a new line is started the robot is paying attention again. See if you can find examples of the commenting process in the code below.

/*
Motor Control I

This sketch shows ......

........ Only certain pins
are capable of 'dimming' but 5 and 6 which run the motors are capable of this.

6/22/20
Bruce Emerson

*/

// initialize variables that will used repetitively during execution
int rightMotor = 5; // the PWM pin for the right motor
int leftMotor = 6; // the PWM pin for the left motor

 

Definitions: After the comments there is section of the sketch before the void setup(); section that can be used to set up some useful variables. The Arduino world has a gentle convention that variable names use camelCase (never heard of it before myself) which does help our eye see the two words without using other forms of spacing. The polite thing to do with your variable names is to make them clearly descriptive. This makes your code more readable to others. Note the names I have used which I hope are effective descriptions of their use.

Each of the lines below has this form

int [variable name] = [value];

The 'int' tells the IDE that the value you are assigning to that variable is an integer. If you set the variable equal to some decimal value the IDE will cut off the decimals (mathematicians call this truncating the value) and just save the whole number. Another choice which we will meet later is called 'float' for floating point (decimal numbers)

NOTE: Every line (statement) terminates with a ';'. This is an important detail that tells the IDE when you are done with that statement. Try leaving this off and see what the IDE spits up at you for an error. This is one of the more irritating mistakes we all make while writing sketches for the Arduino.

// initialize variables that will used repetitively during execution
int rightMotor = 5; // the PWM pin for the right motor
int leftMotor = 6; // the PWM pin for the left motor
int rightForward = 7; // forward-reverse right motor
int leftForward = 8; // forward-reverse left motor
int maxspeed = 250; // set max motor speed for safety, 255 is absolute max

// adjustments to balance the motors
int forwardOffset = -5; // correction to motor going forward
int reverseOffset = -7; // correction to motor going forward

// settings for turning
int turnSpeed = 50; // set speed for making turns
int turn90Time = 1820; // turn time for 90 degrees

// motor speed for this test
int motorSpeed = 100; //set speed for the test

 

Getting things started: As we develop our experience with the Arduino/Qbot we will find that there are some particular steps that need to be taken only once when we start the program/sketch running. These commands are best placed in the setup() loop.

void setup() {

}

Notice a couple of things. There is no ';' at the end of the first line. That's because the setup() function tells the IDE to do everything between the { } before it is done with this command. If we put an ';' after the first '{' it tells the IDE that thats the end of the actions it should take. For readability the left bracket '{' is usually right after the command and the right bracket '}' is on it's own line after the last statement within the brackets.

pinMode();: pinMode is a function (a special command) that tells the IDE whether a pin is going to be used as an OUTPUT pin to control some external object or as an INPUT that brings information in from the outside world. For our motors we want to control their behavior through their pins which include both the motor control pins (5 and 6) and the direction control pins (7 and 8). This is the general format (note the ; at the end).

pinMode([which pin], [OUTPUT or INPUT]);

delay();: delay is a function which tells the Arduino to wait for a certain number of milliseconds before moving on to the next operation. It is NOT a very efficient way to do timing in more complex settings but is conceptually clear and simple for us. I chose to delay for 5 s (5000 ms) after I turn it on to give me time to set it down on the floor before the motors start turning.

// the setup routine runs once when you press reset or power up the Qbot:
void setup() {
// declare motor pins to be outputs:
pinMode(rightMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(leftForward, OUTPUT);
delay(5000); // this allows you 5 s after you start the Qbot to set it down

}

The Main Feature (the loop): The loop(){ } is the part of the sketch that runs over and over until you turn the power off. The structure of the loop() function is essentially the same as the setup() function.

void loop() {

}

Again, there is not ';' at the end of the line because the IDE needs to consider everything from the '{' to the '}' as part of what the loop is asking it to do.

digitalWrite(); and analogWrite();: In this part of the sketch there are really only two functions we need to talk about. digitalWrite and analogWrite. digitalWrite is a little simpler so we'll start with it.

digitalWrite([which pin], [HIGH or LOW]);

We use digitalWrite() when we want the pin to switch on and off (no dimmer!). [which pin] tells the IDE which pin I'm switching and [HIGH or LOW] tells the IDE I want it On (HIGH) or Off (LOW). When you look below in the code I'm using off/on switches to tell the robot whether to go forward (LOW) or backwards (HIGH) and those are the only choices I need.

analogWrite([which pin], [value from 0 - 255]);

analogWrite() is the dimmer switch. Again I need to tell it which pin (switch) I want to dim and then I need to tell it the level I want it set to. The 'dimmer' can be set from 0 (off) to 255 (full on). In the next lab we will explore how this is actually accomplished through a process called Pulse Width Modulation (PWM) but for now the dimmer switch model is perfectly acceptable. Not all pins can be used as dimmers but the ones for the motor speeds are dimmable.

// the loop routine runs over and over again forever:
void loop() {

// set the direction of motor: forward
digitalWrite(rightForward, LOW);
digitalWrite(leftForward, LOW);

// set the speed of motor:
analogWrite(rightMotor, motorSpeed+forwardOffset);
analogWrite(leftMotor, motorSpeed);

// forward for 5 s
delay(5000);

//stop the motors
analogWrite(rightMotor, 0);
analogWrite(leftMotor, 0);
// wait 3 s
delay(3000);


// insert code for turning here

// end turning code here

// set the direction of motor:reverse
digitalWrite(rightForward, HIGH);
digitalWrite(leftForward, HIGH);

// set the speed of motor:
analogWrite(rightMotor, motorSpeed + reverseOffset);
analogWrite(leftMotor, motorSpeed);

// reverse for 5 s
delay(5000);

//stop the motors
analogWrite(rightMotor, 0);
analogWrite(leftMotor, 0);

// wait 3 s
delay(3000);


// insert same code for turning here

// end turning code here


}

 

For a later part of the lab you will be asked to figure out how to turn the Qbot 90 degrees. The code that you create -- presumably turning different motors on and off for specific lengths of time -- is inserted in the two locations indicated. You only need the tools (functions) that we have discussed here.