/*

Problem 2: H-Bridge

An H-Bridge is a rudamentary method for controlling DC motors. You can find out more
about them here: https://en.wikipedia.org/wiki/H-bridge

On the right you'll see 3 buttons. The green button is forward, the blue button is backward, 
and the red button is stop

*/


//Declare Variabls
#define FWD 2
#define BCK 3

#define FWD_BUTTON 8
#define BCK_BUTTON 10
#define STOP_BUTTON 9


//Direction stores the current direction of the H bridge
//Forward = 1
//Stopped = 0
//Backward = -1
int direction = 0;


void setup() {

  //LEDs
  pinMode(FWD, OUTPUT);
  pinMode(BCK, OUTPUT);


  //Buttons
  pinMode(FWD_BUTTON, INPUT_PULLUP);
  pinMode(BCK_BUTTON, INPUT_PULLUP);
  pinMode(STOP_BUTTON, INPUT_PULLUP);

}

void loop() {

  //First, read the buttons and determine what direction we should go in
  //If no button is pressed, then just stay in the current direction

  //You could also just put the direction code from below in here, but
  //Having a flag to indicate direction is very useful for complex projects
  if (digitalRead(STOP_BUTTON) == LOW) {
    direction = 0;
    digitalWrite(FWD, LOW);
    digitalWrite(BCK, LOW);
  }
  else if (digitalRead(FWD_BUTTON) == LOW) {
    direction = 1;
    digitalWrite(FWD, HIGH);
    digitalWrite(BCK, LOW);
  }
  else if (digitalRead(BCK_BUTTON) == LOW) {
    direction = -1;
    digitalWrite(FWD, LOW);
    digitalWrite(BCK, HIGH);
  }
  
}