MY PROJECT HAVE 2 MODES AND IT CAN CONTROLLED VIA BLUETOOTH,
MODE 1 - TO SELF AVOIDANCE ROBOT
MODE 2 - TO REMOTE VIA BLUETOOTH
IF I PRESS THE BUTTON IN MY APP IT WILL SEND "5" OR "6" COMMAND TO MY ARDUINO UNO BOARD(THAT'S THE CODE BELOW), BUT WHEN I TRY MY CODE IT WILL NOT WORK, ITS ONLY
CONNECT TO THE BLUETOOTH MODULE BUT NO COMMAND RECEIVE/EXECUTED.
BUT WHEN I TRY IT TO CONTROLLED VIA BLUETOOTH (DISABLED THE 2 MODES), IT WILL WORK.
SORRY FOR MY BAD AND WRONG GRAMMAR
#include <Servo.h>
#include <AFMotor.h>
Servo servo1;
AF_DCMotor motor1(1);
AF_DCMotor motor2(3);
#define SERVO1_PWM 10
char data=0;
int gMode=0;
const int trigPin = 9;
const int echoPin = 8;
long duration, inches, cm;
const int dangerThresh = 10;
int leftDistance, rightDistance, centerDistance;
int distanceFwd;
void setup()
{
Serial.begin(9600);
Serial.println("ROBOTANK V 2.0");
servo1.attach(SERVO1_PWM);
servo1.write(90);
motor1.setSpeed(200);
motor2.setSpeed(200);
}
void loop()
{
if(Serial.available() > 0)
{
gMode = Serial.read();
if(gMode=='5')
{
MODE1();
}
else if(gMode=='6')
{
MODE2();
}
}
}
void MODE2()
{
if(Serial.available() > 0)
{
data = Serial.read();
if(data == '1')
goForward();
else if(data == '2')
goBackward();
else if(data == '3')
goLeft();
else if(data == '4')
goRight();
else if(data == '0')
stopA();
}
}
//for mode 1
void MODE1()
{
//goForward();
LookAH();
distanceFwd = inches;
if(distanceFwd>dangerThresh)
{
goForward();
}
else
{
stopA();
LookA();
if(leftDistance>rightDistance && leftDistance>centerDistance)
{
goLeft();
}
else if(rightDistance>leftDistance && rightDistance>centerDistance)
{
goRight();
}
else
{
goBackward();
}
}
}
//motor Direction
void goForward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void goBackward()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
void goLeft()
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
void goRight()
{
motor1.run(BACKWARD);
motor2.run(FORWARD);
}
void stopA()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
}
//SENSOR
void LookAH()
{
servo1.write(90);
ping();
delay(500);
}
void LookA()
{
servo1.write(1);
ping();
rightDistance = inches;
Serial.print("Right: ");
Serial.println(rightDistance);
delay(500);
servo1.write(179);
ping();
leftDistance = inches;
Serial.print("Left: ");
Serial.println(leftDistance);
delay(500);
servo1.write(90);
ping();
centerDistance = inches;
Serial.print("Center: ");
Serial.println(centerDistance);
delay(500);
}
void ping()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
You have not told us whether the code does what you want, or what it actually does and what it should do.
Also, please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.
void loop()
{
if(Serial.available() > 0)
{
gch = Serial.read();
if(data == '5') //when i select the mode 1 it should be self avoidance robot
MODE1(); //and select mode 2 it should be controlled via bluetooh using my phone
else if(data == '6') //this code is not working at all or any another method to used?
MODE2();
}
}
For the future, please do not make big changes to older posts as that makes a nonsense of other comments and makes it very difficult to follow the discussion. Add new stuff in a new Reply.
And please don't SHOUT. It is actually very difficult to read text that is all capitals.
Your code for receiving serial data is not at all robust. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
You need to separate the receiving from the action. So, if a '5' or a '6' arrives it is stored in a modeSelected variable and that is used to determine which mode gets used. And if any of the other characters arrived it should also be stored in a suitable variable - perhaps called moveSelect
Then you could have code in the function MODE2() like this
void MODE2() {
if (modeSelected != '6') {
return; // don't use this mode
}
if (moveSelected == '1') {
goForward();
}
// etc
}