Now that we have understood the H-bridge circuit, we will write a program called Forward.cpp to move our robot forward. After that, we will write a program to move the robot backward, left, and right, and then stop. You can download the Forward.cpp program from Chapter03 of the GitHub repository.
The program for moving the robot forward is as follows:
#include <stdio.h>
#include <wiringPi.h>
int main(void)
{
wiringPiSetup();
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
for(int i=0; i<1;i++)
{
digitalWrite(0,HIGH); //PIN O & 2 will move the Left Motor
digitalWrite(2,LOW);
digitalWrite(3,HIGH); //PIN 3 & 4 will move the Right Motor
digitalWrite(4,LOW);
delay(3000);
}
return 0;
}
Let's see how this program works:
- First, we set the wiringPi pins (numbers 0, 1, 2, and 3) as output pins.
- Next, with the following two...