KevsRobots Learning Platform
By , Minute
In this lesson you will learn how to move your robot backwards, turning left and right, and finally creating some code to control the SMARS over the serial port.
You can download the completed code from GitHub - www.github.com/kevinmcaleer/lesson_02_turning
Before you begin, you’ll need to make sure you have a couple of things before you start this lesson:
New Sketch buttonWe are going to create 3 more functions, almost identical to the function we made in lesson 01, but with slightly different parameters for the direction of the motors.
// move backward
void backward() {
// set the direction to backward
digitalWrite(motor_A, HIGH);
digitalWrite(motor_B, LOW);
// set to full speed
analogWrite(motor_A_speed, 255);
analogWrite(motor_B_speed, 255);
// wait
delay(wait_in_milliseconds);
// stop
analogWrite(motor_A_speed, 0);
analogWrite(motor_B_speed, 0);
}
// turn left
void turnLeft() {
// set both motors the same direction to spin on the spot
digitalWrite(motor_A, LOW);
digitalWrite(motor_B, LOW);
// set to full speed
analogWrite(motor_A_speed, 255);
analogWrite(motor_B_speed, 255);
// wait
delay(wait_in_milliseconds);
// stop
analogWrite(motor_A_speed, 0);
analogWrite(motor_B_speed, 0);
}
// turn right
void turnRight() {
// set both motors the same direction to spin on the spot
digitalWrite(motor_A, HIGH);
digitalWrite(motor_B, HIGH);
// set to full speed
analogWrite(motor_A_speed, 255);
analogWrite(motor_B_speed, 255);
// wait
delay(wait_in_milliseconds);
// stop
analogWrite(motor_A_speed, 0);
analogWrite(motor_B_speed, 0);
}
The code in each of these functions is virtually identical, apart from these lines:
digitalWrite(motor_A, HIGH);
digitalWrite(motor_B, HIGH);
These lines tell the motor shield which way to spin motor A and motor B. Because the motors on the SMARS face opposite directions, setting them to different values (LOW/HIGH) makes the robot drive forwards or backwards, and setting them to the same value makes the robot spin left or right on the spot.

To make the SMARS controllable from the keyboard, we can use the Arduino Serial Monitor to issue commands to our SMARS. We will do this by creating a new piece of code that will look for the following 4 keys from the keyboard:
We will do this by reading in characters from the Serial interface, and then running either the forward(), backward(), turnLeft(), or turnRight() functions.
We will change the code in the void loop() section to look like this. Make sure your setup() function also starts the serial port with Serial.begin(9600);, otherwise nothing will arrive from the keyboard.
while (Serial.available()) {
char c = Serial.read();
switch(c) {
// press the 'w' key to move SMARS Forward
case 'w':
Serial.println("Moving Forward");
forward();
break;
// press the 's' key to move SMARS backward
case 's':
Serial.println("Moving Backward");
backward();
break;
// press the 'a' key to move SMARS left
case 'a':
Serial.println("Turning Left");
turnLeft();
break;
// press the 'd' key to move SMARS right
case 'd':
Serial.println("Turning Right");
turnRight();
break;
}
}
// wait 2 seconds
delay(2000);
We can now open the Serial monitor from the Tools menu, and begin typing any of the four direction commands ‘w’,’s’,’a’,’d’ and return to move the SMARS in any of the four directions.


char c = Serial.read(); - this means create a new variable, of type Character (which can hold a single letter or number) and call it c and make that read in from the Serial interface. This is how we read in keys from the serial monitor.
switch(c) { - the switch function will look at the value within the variable c that we have passed it, and then depending on that it will execute different parts of code, depending on each case.
There then follows four similar blocks of code:
// press the 'w' key to move SMARS Forward
case 'w':
Serial.println("Moving Forward");
forward();
break;
The first line is a comment (as denoted by the ‘//’ characters).
The second line says, in the case of the value of c being w then execute the lines below.
The third line says, write the string Moving Forward to the Serial monitor.
The fourth line executes the forward() function.
The last line break; means stop running through the case statements.
wait_in_milliseconds to 250 so each key press moves the robot a shorter distance.x, that stops the robot.q and e keys that turn the robot by a smaller amount.Quick quiz 1 of 2
How do you make SMARS spin on the spot?
Quick quiz 2 of 2
In a switch statement, what does break; do?
break; the code would carry on into the next case as well.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments