Code Example

/*
 * Code by Brian Patton
 * 4/27/2016
 * Simple DC motor control
 */
byte In1pin = 3;
byte In2pin = 4;
byte In3pin = 5;
byte In4pin = 6;
//********************************************************
//    Setup
//********************************************************
void setup() {
  // put your setup code here, to run once:
  pinMode(In1pin, OUTPUT);
  pinMode(In2pin, OUTPUT);
  pinMode(In3pin, OUTPUT);
  pinMode(In4pin, OUTPUT);
  pinMode(13,OUTPUT);
}
//********************************************************
//    Go Forward for a time
//********************************************************
void Forward(int dlay) {
  digitalWrite(In1pin, LOW);  //Left Forward 1 of 2
  digitalWrite(In2pin, HIGH); //Left Forward 2 of 2
  delay(5);                   //Short head start if running on USB
  digitalWrite(In3pin, HIGH); //Right Forward 1 of 2
  digitalWrite(In4pin, LOW);  //Right Forward 2 of 2
  delay(dlay);
}
//********************************************************
//    Turn Right for a time
//********************************************************
void TRight(int dlay) {
  digitalWrite(In1pin, LOW);  //Left Forward 1 of 2
  digitalWrite(In2pin, HIGH); //Left Forward 2 of 2
  delay(5);                   //Short head start if running on USB
  digitalWrite(In3pin, LOW);  //Right Reverse 1 of 2
  digitalWrite(In4pin, HIGH); //Right Reverse 2 of 2
  delay(dlay);
}
//********************************************************
//    Turn Left for a time
//********************************************************
void TLeft(int dlay) {
  digitalWrite(In1pin, HIGH); //Left Reverse 1 of 2
  digitalWrite(In2pin, LOW);  //Left Reverse 2 of 2
  delay(5);                   //Short head start if running on USB
  digitalWrite(In3pin, HIGH); //Right Forward 1 of 2
  digitalWrite(In4pin, LOW);  //Right Forward 2 of 2
  delay(dlay);
}
//********************************************************
//    Reverse for a time
//********************************************************
void Reverse(int dlay) {
  digitalWrite(In1pin, HIGH); //Left Reverse 1 of 2
  digitalWrite(In2pin, LOW);  //Left Reverse 2 of 2
  delay(5);                   //Short head start if running on USB
  digitalWrite(In3pin, LOW);  //Right Reverse 1 of 2
  digitalWrite(In4pin, HIGH); //Right Reverse 2 of 2
  delay(dlay);
}
//********************************************************
//    Stop for a time
//********************************************************
void StopBot(int dlay) {
  //All pins low to stop
  digitalWrite(In1pin, LOW);
  digitalWrite(In2pin, LOW);
  digitalWrite(In3pin, LOW);
  digitalWrite(In4pin, LOW);
  delay(dlay);
}
//********************************************************
//    Stop with infinite loop
//********************************************************
void FinalStop() {
  //All pins low to stop
  digitalWrite(In1pin, LOW);
  digitalWrite(In2pin, LOW);
  digitalWrite(In3pin, LOW);
  digitalWrite(In4pin, LOW);
  while (true){
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }
}
//********************************************************
//    Main Loop
//********************************************************
void loop() {
  // put your main code here, to run repeatedly:
  Forward(500);
  StopBot(50);
  TRight(500);
  StopBot(50);
  TLeft(500);
  StopBot(50);
  Reverse(500);
  //  StopBot(5000);
  FinalStop();
}