HOW TO CODE WITH 2 MODES SELECTED?

GOOD DAY EVERYONE,

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 :slight_smile:

#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;
}

linar87:
CAN YOU LOOK AT MY CODE

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.

…R

thanks for your reply robin2.. its in loop()

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();
   
  }
}

im newbie

Did you meanif (gch == '5')?

linar87:
its in loop()

What is?

You need to give us as much detail as you can about what your program actually does and what you want it to do.

And please post the entire program using the code button.

...R

I ALREADY UPDATE MY POST....

linar87:
I ALREADY UPDATE MY POST....

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
}

...R

ohh sorry for that..

BTW thanks i will try this one....

I already try the code but it’s only execute once.
.
And repeat the loop. And It will never execute the selected mode…

linar87:
I already try the code but it's only execute once.

Post the code you tried - and in a new Reply, please,

...R

#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(gMode != '6')
  {
       return;
}

  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()
{

  if(gMode != '5')
  {
       return;
}

  //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;
}

This is the whole code… I want is to continue to execute the mode I selected.

Note: I’m not good in English :slight_smile:

Hint: serial is sloooooow

AWOL:
Hint: serial is sloooooow

What did you mean ? Serial is slow?

I mean that, in the time it takes an Arduino to receive a character at 9600 bits per second, it could well execute over 16000 instructions.

linar87:
This is the whole code… I want is to continue to execute the mode I selected.

You don’t seem to have taken account of what I said in Reply #6.

I want you to have all the Serial.read() in one place (as in my examples) and your code in loop() should then be something like

void loop() {
recvWithEndMarker();
parseData(); // function to save the received data in the correct variables
MODE1();
MODE2();
}

and there would be no calls to Serial.read() in MODE1() or MODE2()

…R

Thanks for the help..

I will read it first :slight_smile:

I just finished reading....

I code this way

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void loop()
 {
    recvData();
    MODE1();
    MODE2(); 
}

void recvData()
 {
   static byte ndx =0;

    if (Serial.available() > 0) 
   {
        receivedChar[ndx]= Serial.read();
        newData = true;

        if(receivedChars[5])
           MODE1();

        else if(receivedChars[6])
           MODE2();
    }
}

I'm not good in complicated coding..
I code that easy to understand :slight_smile:

Just copy the complete example from my link and then add your functions MODE1() and MODE2() to it. (Or vice versa).

Spend a bit longer studying my examples until you understand how they work and why I have structured then like I have.

...R

Can you give at least sample program

linar87:
Can you give at least sample program

I thought I already did - in Reply #6

If you want a bigger example have a look at Planning and Implementing a Program

...R