Week 7 Use H-Bridge to control a motor

Linda and I decided to create a very basic car using the H-Bridge controlled geared motor. The car is pretty basic right now since it only has one wheel attached to a sled. The vehicle will be able to move forwards or backwards and can also be called to an intermediary position. The “track” will have three sensors. One on each end to keep the car from trying to drive off of the track as well as a sensor in the middle to ensure that the vehicle has arrived at its destination. The only electronics on the vehicle will be the motor itself since the arduino will be off to the side next to a bread board which has buttons to control the vehicle. This drastically limits the amount of wires needed to connect to the vehicle and also allows for easy troubleshooting.

vehicle vehicle in track vehicle and track

const int callPin = 3;
const int IRProxPin = 7;
const int frontPin = A0;
const int forwardPin = 2;
const int backPin = A1;
const int backwardPin = 4;
 
void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
 
}
 
void loop() {
  int forward = digitalRead(forwardPin);
  int backward = digitalRead(backwardPin);
  int front = digitalRead(frontPin);
  int back = digitalRead(backPin);
  int irProx = digitalRead(IRProxPin);
  int call = digitalRead(callPin);
 
  Serial.print("forward = ");
  Serial.print(forward);
  Serial.print("backward = ");
  Serial.print(backward);
  Serial.print("front = ");
  Serial.print(front);
  Serial.print(" back = ");
  Serial.print(back);
  Serial.print(" IR proximity sensor = ");
  Serial.print(irProx);
  Serial.print(" call = ");
  Serial.print(call);
 
  if ((front == 1) || ( back == 1 ) || (irProx == 1)) {
    stop();
  } else if ( forward == 1 ) {
    goForward();
  } else if ( backward == 1 ) {
    goBackward();
  }
  Serial.println();
  delay(5);
}
 
void stop () {
  digitalWrite(8, 0);
  digitalWrite(12, 0);
}
void goForward () {
  Serial.print(" going forward ");
  digitalWrite(8, 1);
  digitalWrite(12, 0);
}
void goBackward () {
  Serial.print(" going backward ");
  digitalWrite(8, 0);
  digitalWrite(12, 1);
}

Leave a comment