Motor Control, to control the left and right motors for our robotics project we need to create five functions, the code for each functions is described further down:
- forward()
- backward()
- left()
- right()
- stop()
At the beginning of our code we initialised six variables to pin number on our Arduino, these pins are wired to an h-bridge motor shield for controlling our motors:
- pinRB = 3; // Wired to IN3 on the Motor shield
- pinRF = 2; // Wired to IN4 on the Motor shield
- pinLB = 7; // Wired to IN1 on the Motor shield
- pinRB = 4; // Wired to IN2 on the Motor shield
- ENA = 5; // Wired to ENA on the Motor shield
- ENB = 6; // Wired to ENB on the Motor shield
This code controls the movement of our Roverbot moving forwards:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Function for moving forward void forward(){ digitalWrite(pinRB,LOW); // we set pinRB to LOW indicating an off for the right motor not to go backward digitalWrite(pinRF,HIGH); // we set pinRF to HIGH indicating an on for the right motor to move forward digitalWrite(pinLB,LOW); // we set pinLB to LOW indicating an off for the left motor not to go backward digitalWrite(pinLF,HIGH); // we set pinLF to HIGH indicating an on for the left motor to move forward digitalWrite(ENA,100); // motor A speed digitalWrite(ENB,100); // motor B speed } |
This code controls the movement of our Roverbot moving backwards:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Function to move backward void backward(){ digitalWrite(pinRB,HIGH); // we set pinRB to HIGH indicating an on for the right motor to move backward digitalWrite(pinRF,LOW); // we set pinRF to LOW indicating an off for the right motor not to go forward digitalWrite(pinLB,HIGH); // we set pinLB to HIGH indicating an on for the left motor to move backward digitalWrite(pinLF,LOW); // we set pinLF to LOW indicating an off for the left motor not to go forward digitalWrite(ENA,100); // motor A speed digitalWrite(ENB,100); // motor B speed } |
This code controls the movement of our Roverbot moving left:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Function to move left void left(){ digitalWrite(pinRB,HIGH); // we set pinRB to HIGH indicating an on for the right motor to move backward digitalWrite(pinRF,LOW); // we set pinRF to LOW indicating an off for the right motor not to go forward digitalWrite(pinLB,LOW); // we set pinLB to LOW indicating an off for the left motor not to go backward digitalWrite(pinLF,HIGH); // we set pinLF to HIGH indicating an on for the left motor to move forward digitalWrite(ENA,100); // motor A speed 100 digitalWrite(ENB,100); // motor B speed 100 } |
This code controls the movement of our Roverbot moving right:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Function to move right void right(){ digitalWrite(pinRB,LOW); // we set pinRB to LOW indicating an off for the right motor not to go backward digitalWrite(pinRF,HIGH); // we set pinRF to HIGH indicating an on for the right motor to move forward digitalWrite(pinLB,HIGH); // we set pinLB to HIGH indicating an on for the left motor to move backward digitalWrite(pinLF,LOW); // we set pinLF to LOW indicating an off for the left motor not to go forward digitalWrite(ENA,100); // motor A speed digitalWrite(ENB,100); // motor B speed } |
This code controls the movement of our Roverbot by stopping it:
|
1 2 3 4 5 6 7 8 |
//Function to stop movement void stop(){ digitalWrite(ENA,0); //motor A speed set to 0 digitalWrite(ENB,0); //motor B speed set to 0 } |


