I did come to realize spinning 2 wheels in opposite directions wasn't going to work. If it did, then a car's wheels would be able to do it. haha! I was thinking to attach a servo in the front changing the angle 45degrees in each direction. My vehicle performs badly without the battery. While moving forward or backward why would I want to spin 1 wheel faster than the other?
Corrected Code:
/*
Evan Johnson
Description: A bluetooth remote controlled car. The car has FA-130 motors,
a Tamiya universal plate and axel hinges for front end wheels, and a SM-S4303R servo.
Most likely using a LiPo 3.7v 1000mA battery to power the car.
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
char serInStr[30]; // array that will hold the serial input string
int val = 45; // change angle of servo 45 degrees left from center
int val2 = 90; // change angle of servo to 90 degree center
int val3 = 135; // change angle of servo 45 degrees right from center
void setup()
{
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(3, OUTPUT); //Initiates Speed Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
pinMode(11, OUTPUT); //Initiates Speed Channel B pin
myservo.attach(10); // attaches the servo on pin 10 to the servo object
//Start the UART TX1/RX0 (D1/D0) communication at defined rate
Serial.begin(115200);
delay(1000);
//Call menu function
menu();
delay(1000);
}
void loop()
{
//read the serial port and create a string out of what you read
if(readSerialString())
{
Serial.println(serInStr);
char cmd = serInStr[0];
int num = atoi(serInStr+1);
switch(cmd)
{
case '8':
{
Serial.println("8");
Serial.println("MADE IT IN 8 FOR FORWARD");
//Motor A and Motor B engage breaks
digitalWrite(9, HIGH); // ENGAGE BREAK ON CHANNEL A MOTOR
digitalWrite(8, HIGH); // ENGAGE BREAK ON CHANNEL B MOTOR
//Motor A background @ full speed
digitalWrite(12, LOW); //Establish forward direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at full speed
//Motor B background @ full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
case '2':
{
Serial.println("2");
Serial.println("MADE IT IN 2 FOR BACKWARDS");
//Motor A and Motor B engage breaks
digitalWrite(9, HIGH); // ENGAGE BREAK ON CHANNEL A MOTOR
digitalWrite(8, HIGH); // ENGAGE BREAK ON CHANNEL B MOTOR
//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establish forward direction of channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3, 255); //Spins the motor on channel A at half speed
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
case '4':
{
Serial.println("4");
Serial.println("MADE IT IN 4 FOR LEFT SERVO");
myservo.write(val); // sets the servo position 45 degrees for right position
delay(15); // waits for the servo to get there
}
case '0':
{
Serial.println("0");
Serial.println("MADE IT IN 0 FOR CENTER SERVO");
myservo.write(val2); // sets the servo position 90 degrees for center position
delay(15); // waits for the servo to get there
}
case '6':
{
Serial.println("6");
Serial.println("MADE IT IN 6 FOR RIGHT SERVO");
myservo.write(val3); // sets the servo position 45 degrees for left position
delay(15); // waits for the servo to get there
}
case '5':
{
Serial.println("5");
Serial.println("MADE IT IN 5 FOR BREAK MOTORS");
//Motor A and Motor B engage breaks
digitalWrite(9, HIGH); // ENGAGE BREAK ON CHANNEL A MOTOR
digitalWrite(8, HIGH); // ENGAGE BREAK ON CHANNEL B MOTOR
}
default:
{
Serial.println("Invalid Selection. Please try again!");
return;
}
}
}
delay(2);
}
//****************************************************
//1) Menu void/void function
void menu()
{
Serial.print("\r\nEvan's Double Gearbox Control!\n"
"'8' for forward motors\n"
"'2' for reverse motors\n"
"'4' for left servo\n"
"'0' for center servo\n"
"'6' for right servo\n"
"'5' for break motors\n"
"Please enter your command: ");
}
//2) Read a string from the serial and store it in an array
//you must supply the array variable
uint8_t readSerialString()
{
if(!Serial.available())
{
return 0;
}
delay(10); // wait a little for serial data
int i = 0;
while (Serial.available())
{
serInStr[i] = Serial.read(); // FIXME: doesn't check buffer overrun
i++;
}
serInStr[i] = 0; // indicate end of read string
return i; // return number of chars read
}