Contents:

Idea
The next step in the journey of CarBot was to make it programmable using Arduino software. With this feature it will be possible to write code to move the robot around in every way, thus providing more possibilities for further projects.
Layout
Car-Bot’s Arduino UNO board is powered by a 9 volt battery, whilst the motors and other functional circuits are powered by two AA batteries.
Execution
In order to put everything together I connected two ports per motor to the motor driver, connected the positive and ground contacts from the battery to the motor driver, and created a switch for the Arduino UNO and the second battery.
Materials used:
- Motor Driver
- Wires
- AA Batteries
- Switch
- Arduino UNO
- CarBot (base)
Programming
The first program for this new version of CarBot will showcase its ability to move backwards without having to turn around. As always, the code starts with a short description to describe what its purpose is.
/*
FORWARDS - BACKWARDS
STEPS:
1. Move forward for 3 seconds
2. Pause for 3 seconds
3. Move backwards for 3 seconds
4. Repeat
*/
The setup loop is next. It describes all of the outputs that will be used in the code using the previously defined variables.
int m1a = 4; // motor 1 pin a
int m1b = 5; // motor 1 pin b
int m2a = 6; // motor 2 pin a
int m2b = 7; // motor 2 pin b
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
}
Below is the main loop of the program. It is fairly short because it only contains commands to wait for three seconds and a couple lines for calling the major functions.
// the loop function runs over and over again forever
void loop() {
forward();
delay(3000);
cease();
delay(3000);
backwards();
delay(3000);
cease();
delay(3000);
}
Finally, we define the major functions which were used in the main loop. They contain commands for the motor driver to move forwards, backwards, or stop.
void forward(){
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
void cease(){
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
void backwards(){
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}