Hi all,
I have been struggling with this project for a couple of weeks now, and one after another problems keep showing up. I have this car that I have built from scratch, and it can be controlled by a Bluetooth app made in MIT App Inventor. The 4 TT geared motors are controlled by 2 BTS7960 motor drivers. The problem is that when I try and control the car from the app, the motors only work when I give them full power from the Android app over BT. It has worked before, but now that I added some more features (a controllable servo) it has seemed to only work at full speed. Here are some images for an idea of what's going on:
I can provide more images if necessary.
Here is the code I have used for the Arduino. I apologize for the messy code, but it has been edited many, many times.
#include <Servo.h>
Servo camera;
int state = 0;
long distanceFront = 1188;
long distanceRight = 1188;
long distanceLeft = 1188;
int varSpd = 90;//Default motor speeds
//Motor Driver 1:
int motorPin1 = A0;
int motorPin2 = A1;
int EN1 = 9;
//Motor driver 2:
int motorPin3 = A2;
int motorPin4 = A3;
int EN2 = 10;
long duration, distance;
int trigPinFront = 2;
int echoPinFront = 3;
int trigPinRight = 4;
int echoPinRight = 5;
int trigPinLeft = 7;
int echoPinLeft = 8;
bool status = false;
int pos = 90;
String data;
void setup()
{
camera.attach(6);
camera.write(pos);
pinMode(trigPinFront, OUTPUT);
pinMode(echoPinFront, INPUT);
pinMode(trigPinRight, OUTPUT);
pinMode(echoPinRight, INPUT);
pinMode(trigPinLeft, OUTPUT);
pinMode(echoPinLeft, INPUT);
pinMode(A4, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
Serial.begin(9600);
delay(1000);
Serial.println("started.");
}
void loop() {
if (digitalRead(A4) == LOW) {
if (status == false) {
status = true;
} else {
status = false;
stopAll();
}
delay(300);
}
if (status == 1) {
if (Serial.available() > 0) { // Checks whether data is coming from the serial port
state = Serial.read(); // Reads the data from the serial port
if (state > 50) {
varSpd = state;
}
}
sonar();
if (distanceFront < 30) {
if (distanceRight > distanceLeft) {
while (distanceRight > distanceLeft) {
right();
sonar();
}
} else if (distanceLeft > distanceRight) {
while (distanceLeft > distanceRight) {
left();
sonar();
}
}
} else {
if (distanceRight < 25) {
while (distanceRight < 25) {
left();
sonar();
}
} else if (distanceLeft < 25) {
while (distanceLeft < 25) {
right();
sonar();
}
} else {
forward();
}
}
}
//Read the data if available in buffer
if (status == 0) {
if (Serial.available() > 0) { // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
// Serial.println(state);
if (state == 0) {
//Serial.println("stop");
digitalWrite(13, LOW); // Turn LED OFF
stopAll();
}
else if (state == 1) {
digitalWrite(13, HIGH);
forward();
}
else if (state == 2) {
digitalWrite(13, HIGH);
backward();
}
else if (state == 3) {
digitalWrite(13, HIGH);
left();
}
else if (state == 4) {
digitalWrite(13, HIGH);
right();
} else if (state > 10) {
//Serial.println("Setting speed!");
varSpd = state;
} else if (state == '5') {
//servo left
digitalWrite(13, HIGH);
while (state != '7') {
if (Serial.available() > 0) {
state = Serial.read();
}
if (pos > 179) {
pos = 179;
} else {
pos++;
camera.write(pos);
delay(35);
}
}
digitalWrite(13, LOW);
} else if (state == '6') {
//servo right;
digitalWrite(13, HIGH);
while (state != '7') {
if (Serial.available() > 0) {
state = Serial.read();
}
if (pos < 1) {
pos = 1;
} else {
pos--;
camera.write(pos);
delay(35);
}
}
digitalWrite(13, LOW);
} else {
stopAll();
digitalWrite(13, LOW);
}
}
}
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delay(.2);
digitalWrite(trigPin, HIGH);
delay(.10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
}
void backward() {
// Serial.print("backward. Speed: ");
//Serial.println(varSpd);
analogWrite(EN1, varSpd);
analogWrite(EN2, varSpd);
digitalWrite(motorPin1, 1);
digitalWrite(motorPin2, 0);
digitalWrite(motorPin3, 0);
digitalWrite(motorPin4, 1);
}
void forward() {
// Serial.print("forward. Speed: ");
//Serial.println(varSpd);
analogWrite(EN1, varSpd);
analogWrite(EN2, varSpd);
digitalWrite(motorPin1, 0);
digitalWrite(motorPin2, 1);
digitalWrite(motorPin3, 1);
digitalWrite(motorPin4, 0);
}
void left() {
// Serial.print("left. Speed: ");
//Serial.println(varSpd);
analogWrite(EN1, varSpd);
analogWrite(EN2, varSpd);
digitalWrite(motorPin1, 1);
digitalWrite(motorPin2, 0);
digitalWrite(motorPin3, 1);
digitalWrite(motorPin4, 0);
}
void right() {
// Serial.print("right. Speed: ");
//Serial.println(varSpd);
analogWrite(EN1, varSpd);
analogWrite(EN2, varSpd);
digitalWrite(motorPin1, 0);
digitalWrite(motorPin2, 1);
digitalWrite(motorPin3, 0);
digitalWrite(motorPin4, 1);
}
void stopAll() {
analogWrite(EN1, 0);
analogWrite(EN2, 0);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void sonar() {
SonarSensor(trigPinFront, echoPinFront);
distanceFront = distance;
SonarSensor(trigPinRight, echoPinRight);
distanceRight = distance;
SonarSensor(trigPinLeft, echoPinLeft);
distanceLeft = distance;
//Serial.print(distanceFront);Serial.print(" "); Serial.print(distanceRight);Serial.print(" "); Serial.print(distanceLeft); Serial.println(" ");
}
I'm not sure if you will need this, but I will provide an image of the code I am using in MIT App Inventor:
I know this is a strange and possibly pretty confusing question, but if you need any more detail or wiring diagrams, just let me know!
Thanks in advance,
transistor_man