Hi!
I am trying to make a a robot using the Arduino UNO, and it is programmed to communicate with a keypad app on my Android smartphone via bluetooth. Now when I press "a" on the keypad, it begins using a sonar sensor and runs autonomous. I want to be able to return to bluetooth communication after entering autonomous mode, but currently it stops communicating with the bluetooth after entering autonomous. Any ideas on how to repair this code?
Thanks!
#include <Servo.h>
#include <AFMotor.h>
#include <SoftwareSerial.h>
#define trigPin 17
#define echoPin 16
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
Servo servoMain; // Define our Servo
boolean sweep = true;
int pos = 0;
SoftwareSerial mySerial(17, 14); // TX, RX
String command = ""; // Stores response of bluetooth device
char val;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.println("Type AT commands!");
// SoftwareSerial "com port" data rate. JY-MCU v1.03 defaults to 9600.
mySerial.begin(9600);
servoMain.attach(10); // servo on digital pin 10
}
void loop()
{
// Read device output if available.
if (mySerial.available()) {
val = mySerial.read();
Serial.println(val); // read it and store it in 'val'
if( val == 'C')
{
motor1.setSpeed(0);
motor2.setSpeed(0);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
if( val == 'L' )
{
motor1.setSpeed(225);
motor2.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(50);
}
if( val == 'D' )
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(100);
}
if( val == 'R' )
{
motor1.setSpeed(225);
motor2.setSpeed(200);
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(50);
}
if( val == 'U' )
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(100);
}
if( val == 'a' )
{
val = mySerial.read();
Serial.println(val); // read it and store it in 'val'
int duration, distance;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
if (sweep == true)
{
pos = 0;
servoMain.write(pos); // Turn Servo Left to 45 degrees
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
if (sweep == false)
{
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(BACKWARD);
delay(300);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(FORWARD);
}
delay(350); // Wait 1 second
pos = 45;
servoMain.write(pos); // Turn Servo Left to 45 degrees
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
if (199 >= distance >= 1)
{
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(BACKWARD);
delay(300);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(FORWARD);
}
delay(350); // Wait 1 second
pos = 135;
servoMain.write(pos); // Turn Servo Left to 0 degrees
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor2.run(BACKWARD);
delay(300);
motor2.setSpeed(100);
motor1.setSpeed(100);
motor2.run(FORWARD);
}
delay(350); // Wait 1 second
pos = 180;
servoMain.write(pos); // Turn Servo back to center position (90 degrees)
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor2.run(BACKWARD);
delay(300);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor2.run(FORWARD);
}
delay(350); // Wait 1 second
pos = 135;
servoMain.write(pos); // Turn Servo Right to 135 degrees
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor2.run(BACKWARD);
delay(300);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor2.run(FORWARD);
}
delay(350); // Wait 1 second
pos = 45;
servoMain.write(pos); // Turn Servo Right to 180 degrees
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
if ( 10 >= distance >= 1)
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(BACKWARD);
delay(300);
motor1.setSpeed(100);
motor2.setSpeed(100);
motor1.run(FORWARD);
}
delay(350); // Wait 1 second
}
}
if( val == 'b' )
{
}
if( val == 'c' )
{
}
if( val == 'd' )
{
}
if( val == 'e' )
{
}
if( val == 'f' )
{
}
if( val == 'g' )
{
}
if( val == 'h' )
{
}
}
}