#include <SoftwareSerial.h>
#define BRAKEVCC 0
#define CW   1
#define CCW  2
#define BRAKEGND 3
#define CS_THRESHOLD 100
int inApin[2] = {7, 4};  
int inBpin[2] = {8, 9}; 
int pwmpin[2] = {5, 6}; 
int statpin = 10;
SoftwareSerial mySerial(2, 3); 
char a,b;
void setup()  
{
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }
 pinMode(statpin, OUTPUT);
  
  for (int i=0; i<2; i++)
  {
    pinMode(inApin[i], OUTPUT);
    pinMode(inBpin[i], OUTPUT);
    pinMode(pwmpin[i], OUTPUT);
  }
  
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}
void loop() 
{
    digitalWrite(statpin, HIGH);
    
  if (mySerial.available()){
        a=mySerial.read();
    if(a=='F'){
  motorGo(0, CW, 1023);
  motorGo(1, CW, 1023);
    }
    if(a=='B'){
  motorGo(0, CCW, 1023);
  motorGo(1, CCW, 1023);
    }
    if(a=='L'){
  motorGo(0, CW, 1023);
  motorGo(1, CCW, 1023);
    }
    if(a=='R'){
  motorGo(0, CCW, 1023);
  motorGo(1, CW, 1023);
    }
     if(a=='I'){
  motorGo(0, CW, 500);
  motorGo(1, CW, 1023);
    }
    if(a=='G'){
  motorGo(0, CW, 1023);
  motorGo(1, CW, 500);
    }
    if(a=='H'){
  motorGo(0, CCW, 1023);
  motorGo(1, CCW, 500);
    }
    if(a=='J'){
  motorGo(0, CCW, 500);
  motorGo(1, CCW, 1023);
    }
       if(a=='S'){
  motorOff(1);
  motorOff(2);
    }
    Serial.write(a);
    }else{
      
    }
  
}
void motorOff(int motor)
{
  
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  analogWrite(pwmpin[motor], 0);
}
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
  if (motor <= 1)
  {
    if (direct <=4)
    {
      
      if (direct <=1)
        digitalWrite(inApin[motor], HIGH);
      else
        digitalWrite(inApin[motor], LOW);
      
      if ((direct==0)||(direct==2))
        digitalWrite(inBpin[motor], HIGH);
      else
        digitalWrite(inBpin[motor], LOW);
      analogWrite(pwmpin[motor], pwm);
    }
  }
}