CODING: MOTOR CONTROL
Let’s start by getting TURL-X to move forward for 3 seconds and stop for 3 seconds (then repeat).
Copy and upload the code to TURL-X.
Unplug the USB to your computer and put TURL-X on the ground.
Turn Turl-X’s power on
With a bit of luck you will see it drive forward for 3 seconds and stop, then repeat.
Don’t just copy and paste but try and see if you can make sense of the code with what you have learnt so far.
➕➕
filename.cpp
// DRV8833 - TURL-X'S Two Motor Control
// Motor 1 (TURL-X'S right): pins 9 and 10
// Motor 2 (TURL-X'S left): pins 5 and 6
int MOTOR1_IN1 9
int MOTOR1_IN2 10
int MOTOR2_IN1 5
int MOTOR2_IN2 6
void setup() {
// Tell Arduino these pins send signals out
pinMode(MOTOR1_IN1, OUTPUT);
pinMode(MOTOR1_IN2, OUTPUT);
pinMode(MOTOR2_IN1, OUTPUT);
pinMode(MOTOR2_IN2, OUTPUT);
// Make sure motors are stopped at start
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN1, LOW);
digitalWrite(MOTOR2_IN2, LOW);
delay(1000); // wait 1 second before starting
}
void loop() {
// ────────────────
// Go forward 3 seconds
// ────────────────
// Right motor forward
digitalWrite(MOTOR1_IN1, HIGH);
digitalWrite(MOTOR1_IN2, LOW);
// Left motor forward
digitalWrite(MOTOR2_IN1, HIGH);
digitalWrite(MOTOR2_IN2, LOW);
delay(3000); // stay forward for 3 seconds
// ────────────────
// Stop for a tiny moment
// ────────────────
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN1, LOW);
digitalWrite(MOTOR2_IN2, LOW);
delay(3000); // 3 second pause
}
Let’s try something more exciting now!
Drive forward for 3 seconds, stop, turn 180 degrees, repeat.
Experiment! Change the drive time or even the turn time and see what happens. Hint: delay(?)
➕➕
filename.cpp
// DRV8833 - TURL-X Two Motor Control
// Motor 1 (TURL-X's right): pins 9 and 10
// Motor 2 (TIRL-X's left): pins 5 and 6
int MOTOR1_IN1 = 9; // we create a variable, like a box (it will hold an integer type - int), we give it a name = MOTOR_IN1, and put a value of 9 to it (the pin 9)
int MOTOR1_IN2 = 10;
int MOTOR2_IN1 = 5;
int MOTOR2_IN2 = 6;
void setup() {
// Tell Arduino these pins send signals out
pinMode(MOTOR1_IN1, OUTPUT);
pinMode(MOTOR1_IN2, OUTPUT);
pinMode(MOTOR2_IN1, OUTPUT);
pinMode(MOTOR2_IN2, OUTPUT);
// Make sure motors are stopped at start
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN1, LOW);
digitalWrite(MOTOR2_IN2, LOW);
delay(1000); // wait 1 second before starting
}
void loop() {
// ────────────────
// Go forward 3 seconds
// ────────────────
// Right motor forward
digitalWrite(MOTOR1_IN1, HIGH);
digitalWrite(MOTOR1_IN2, LOW);
// Left motor forward
digitalWrite(MOTOR2_IN1, HIGH);
digitalWrite(MOTOR2_IN2, LOW);
delay(3000); // stay forward for 3 seconds
// ────────────────
// Stop for a tiny moment
// ────────────────
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN1, LOW);
digitalWrite(MOTOR2_IN2, LOW);
delay(400); // short pause
// ────────────────
// Turn around (spin right) ≈ 180°
// ────────────────
// Right motor backward
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, HIGH);
// Left motor forward
digitalWrite(MOTOR2_IN1, HIGH);
digitalWrite(MOTOR2_IN2, LOW);
delay(800); // how long to spin/turn — change this number if needed
// ────────────────
// Stop again
// ────────────────
digitalWrite(MOTOR1_IN1, LOW);
digitalWrite(MOTOR1_IN2, LOW);
digitalWrite(MOTOR2_IN1, LOW);
digitalWrite(MOTOR2_IN2, LOW);
delay(400); // short pause before next forward
}