I have a dual dc motor car chassis that I'm trying to control using my iPhone through the ArduinoBlue app. When connected to a physical joystick the car drives (mostly) fine, but when controlled through the Bluetooth on my phone it will only go in circles.
Here's my code
#include <SoftwareSerial.h>
#include <ArduinoBlue.h>
//Motor variables
const int M1_1A = 11; //Motor 1 Control Input A through pin 11
const int M1_1B = 10; //Motor 1 Control Input B through pin 10
const int M2_1A = 6; //Motor 2 Control Input A through pin 6
const int M2_1B = 5; //Motor 2 Control Input B through pin 5
// HM 10 bluetooth module connections
const int BLUETOOTH_TX = 8;
const int BLUETOOTH_RX = 7;
int throttle = 49;
int steering = 49;
int PWM_1;
int PWM_2;
SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX);
ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor.
void setup()
{
pinMode(M1_1A, OUTPUT);
pinMode(M1_1B, OUTPUT);
pinMode(M2_1A, OUTPUT);
pinMode(M2_1B, OUTPUT);
stop(M1_1A,M1_1B);
stop(M2_1A,M2_1B);
Serial.begin(9600);
// Begin serial communication with HM 10 at 9600 bps
bluetooth.begin(9600);
// delay in case the HM 10 needs time to respond
delay(100);
}
void loop()
{
throttle = phone.getThrottle();
steering = phone.getSteering();
//Forward
if(throttle>50)
{
PWM_1=map(throttle,50,99,0,255);
PWM_2=map(throttle,50,99,0,255);
forward(M1_1A,M1_1B,PWM_1);
forward(M2_1B,M2_1A,PWM_2);
}
//Reverse
else if(throttle<48)
{
PWM_1=map(throttle,48,0,0,255);
PWM_2=map(throttle,48,0,0,255);
reverse(M1_1A,M1_1B,PWM_1);
reverse(M2_1B,M2_1A,PWM_2);
}
//Sharp Left
else if(steering<10)
{
forward(M2_1A,M2_1B,255);
stop(M1_1A,M1_1B);
}
//Sharp Right
else if(steering>90)
{
stop(M1_1A,M1_1B);
reverse(M2_1A,M2_1B,255);
}
else
{
stop(M1_1A,M1_1B);
stop(M2_1A,M2_1B);
}
Serial.print("Throttle: ");Serial.println(throttle);
Serial.print("Steering: ");Serial.println(steering);
Serial.println("\n\n");
delay(50);
}
//function that controls forward motion
void forward (int m1A, int m1B, int ispeed)
// ispeed between 0 and 255. m1A and m1B indicate motor
{
if ((ispeed >= 0) and (ispeed <= 255))
{
stop (m1A,m1B); // ensure control pins at 0,0 before changing
analogWrite (m1A, ispeed);
digitalWrite(m1B , LOW);
}
}
//function that controls reverse motion
void reverse (int m1A, int m1B, int ispeed)
//ispeed between 0 and 255. m1A and m1B indicate motor
{
if ((ispeed >= 0) and (ispeed <= 255))
{
stop (m1A,m1B); // ensure control pins at 0,0 before changing
digitalWrite(m1A, LOW);
analogWrite (m1B, ispeed);
}
}
//stops both motors
void stop(int m1A, int m1B) // m1A and m1B indicate motor
{
digitalWrite(m1A, LOW);
digitalWrite(m1B, LOW);
}
One thing I noticed is that if i swap the one of the motors in the functions for forward and reverse the car will now only go in a straight line but will not turn
From
forward(M1_1A,M1_1B,PWM_1);
forward(M2_1B,M2_1A,PWM_2);
To
forward(M1_1A,M1_1B,PWM_1);
forward(M2_1A,M2_1B,PWM_2);
Switching the motors in the functions to turn has no effect.
I'm using an UNO board with a sbx h bridge and a DSDTECH HM 10 Bluetooth module.
Any ideas?
Thanks.