I recently built a SMARS (robotic vehicle) run by Arduino Uno R3 and MH Motor Shield, which I believe is similar to the Addafruit V1.
The basic sketch proved okay and and drove fwd, back, left & right this said all hardware good.
The next stage fit a Bluetooth board, HC06, for manual control, this set up okay and my Android tablet connects (Bluetooth LED static lit) but no control from the Android software controller. I double checked the HC06 using a breadboard and test sketch which proved I had I/O control of an LED. Looking online I have seen that using tx/rx 0&1 on the Arduino is not a good idea, this was from the build sheet I followed not wanting to use mismatched information, I uploaded the sketch without the motor board fitted.
Now my question, looking at the sketch where do I find the port settings to change from 0/1 to other ports and what ports do the motors use, I'm using a baud of 9600 which I confirmed works on my LED test.
#include <AFMotor.h>
AF_DCMotor MotorL(1); //Motor for drive Left on M1
AF_DCMotor MotorR(2); //Motor for drive Right on M2
const int buzPin = 2; //set digital pin 2 as buzzer pin (use active buzzer)
String readString, action, Lspeed, Rspeed, actionDelay, stopDelay; //declaring multiple strings
void setup(){
Serial.begin(9600); //set up Serial library at 115200 bps
Serial.println("*SMARS Remote Control Mod*");
pinMode(buzPin, OUTPUT); //sets the buzzer pin as an Output
// Set the speed to start, from 0 (off) to 255 (max speed)
// sometimes the motors don't have the same speed, so use these values to make the SMARS move straight
MotorL.setSpeed(255);
MotorR.setSpeed(255);
// turn off motors
MotorL.run(RELEASE);
MotorR.run(RELEASE);
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c;
if (c == '/n') {
Serial.println("---------------");
Serial.print(readString); //prints string to serial port out
int n1;
int n2;
// separate the string that receive from serial buffer into several substring
action = readString.substring(0, 1);
Lspeed = readString.substring(1, 4);
Rspeed = readString.substring(4, 7);
Serial.println(action);
Serial.println(Lspeed);
Serial.println(Rspeed);
char carray1[7]; //declaring character array
Lspeed.toCharArray(carray1, sizeof(carray1)); //passing the value of the string to the character array
n1 = atoi(carray1); //convert the char/string to a integer value
char carray2[7];
Rspeed.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
Serial.println(n1); //prints integer value n1 to serial port out
Serial.println(n2); //prints integer value n2 to serial port out
readString = "";
//move forward
if(action.equals("F")){
MotorL.setSpeed(n1);
MotorR.setSpeed(n2);
MotorL.run(FORWARD);
MotorR.run(FORWARD);
//move backward
} else if(action.equals("B")){
MotorL.setSpeed(n1);
MotorR.setSpeed(n2);
MotorL.run(BACKWARD);
MotorR.run(BACKWARD);
//turn left
} else if(action.equals("L")){
MotorL.setSpeed(n1);
MotorR.setSpeed(n2);
MotorL.run(BACKWARD);
MotorR.run(BACKWARD);
//turn right
} else if(action.equals("R")){
MotorL.setSpeed(n1);
MotorR.setSpeed(n2);
MotorL.run(BACKWARD);
MotorR.run(BACKWARD);
//stop
} else if(action.equals("S")){
MotorL.run(RELEASE);
MotorR.run(RELEASE);
//turn on horn
}else if(action.equals("H")){
digitalWrite(2, HIGH);
delay(150);
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(250);
digitalWrite(2, LOW);
}
}
}
}
