Problem with robot after disconnect Bluetooth.

Hello guys.

I have made a project with Arduino Uno - Motor Shield 3rev - 3x Ultrasonic sensors - Bluetooth module HC06 and 2 DC motors.

Let's say this is a robot, which may move - turn - avoid walls (because ultrasonic sensor). This program runs perfectly.

I added Bluetooth module so i can control my robot on my own with my Android phone. This program runs perfectly also.

So now I would like unite those two programs, and I did.

When i power my robot its start moving - turning - avoiding walls everything fine, after i connect my phone via the Bluetooth i can control perfect my robot, but when i disconnect my phone is not going back to main program moving - tuning - avoid walls. Does nothing at all. if i try to connect again via Bluetooth its connect perfect (i have full control on my robot again).

Here is main loop: void loop() { if (Serial.available() > 0){ state = Serial.read(); switch(state){ case 'F': Forward (); break; ..... rest cases else { ReadSensor (); AvoidWalls (); MoveRobot (); } Why is not going again back to "else" so it can start moving again alone. Any idea guys what i have to change in main loop so it can go back to "else"??

(deleted)

good question, probably do not know it. :stuck_out_tongue:
any idea how I can do this? i think this is the problem but i dont know how to fix it

What happens if your phone remains connected but you don't send any character for a long time?

It seems to me the code you posted would then behave the same as if you disconnect the phone.

What do you mean by "Does nothing at all"? Do you mean that the robot stays in one place?

Perhaps, after a phone command completes, the system remains in a state in which no action is expected.

Post your complete code so we can see all the SWITCH/CASE statements and see what happens within the functions.

...R

When my phone is connect with my robot i can see in Motor Shield both LEDs in every side A and B motor open (4leds with light), so lets say i press F in my code that mean Forward = 12, 13 pin write HIGH and 9, 8 pin write LOW and my robot start moving forward. if i dont press something in my phone i can see in shield again both LEDs in every side A and B open. if i disconnect bluetooth the LEDs are keeping open. So i think that's mean code is still going in swith/case mode not on "else". I am ask for help, if any know, how i can check if is a connect via Bleutooth.

Wormwood:
So i think that’s mean code is still …

But you did not think to post the code so we can see it ?
And please use the code button </> so your code looks like this and is easy to copy to a text editor

If it is too long (hopefully not) add your .ino file as an attachment.

…R

Code is attached.
I remind that, with this code work fine to avoid everthing ( “else” ) and also work fine when control my robot from phone. Problem is when disconnect bluetooth.

Code.txt (4.5 KB)

Code from Reply #6 so others don’t have to download it.

#include <Ultrasonic.h>

Ultrasonic ultraleft(6,5);     // (Trig PIN,Echo PIN)
Ultrasonic ultraright(2,4);    // (Trig PIN,Echo PIN)
Ultrasonic ultracenter(7,10);  // (Trig PIN,Echo PIN)

int SL; //speed value Left motor
int SR; // speed value Right motor

int UC; // Center sensor
int UL; // Left sensor
int UR; // Right sensor

int SpeedArray[17] = {90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250};  // Speed Array

char state = 'S';  //Bluetooth (HC-06) 

  
void setup() {

  Serial.begin(9600); 
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel B pin

  ResetSystem ();
}

void loop() {
  if(Serial.available() > 0){
     SL=255;
     SR=255;    
      state = Serial.read();
    switch(state){
    case 'F': 
      Forward ();
      break;
    case 'B':  
      Backward();
      break;
    case 'L':  
      Left();
      break;
    case 'R':
      Right();  
      break;
    case 'S':  
      Stop();
      break; 
    case 'I':  //FR  
      FrontRight ();
      break; 
    case 'J':  //BR  
      BackRight ();
      break;        
    case 'G':  //FL  
      FrontLeft ();
      break; 
    case 'H':  //BL
      BackLeft ();
      break;
    case 'W':  //Font ON 
      
      break;
    case 'w':  //Font OFF
      
      break;
    case 'U':  //Back ON 

      break;
    case 'u':  //Back OFF 
      
      break; 
    case 'D':  //Everything OFF 
	Stop();
      break;       
        } 
   }else {
     ReadSensor ();
     AvoidWalls ();
     MoveRobot ();
    }
}


void ResetSystem (){
  digitalWrite(9, HIGH);
  analogWrite(3, 0);
  digitalWrite(8, HIGH);
  analogWrite(11, 0);
  delay(2000);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
}

void ReadSensor (){ 
  UL=ultraleft.Ranging(CM);
  delay(50);
  UC=ultracenter.Ranging(CM);
  delay(50);
  UR=ultraright.Ranging(CM); 
  delay(50);
 
}

void AvoidWalls (){
  if (UC<15 and UR<17 and UL<17){
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    SL=255;
    SR=255;
  } 
  else if (UC<15){
    if (UR>UL) {
      digitalWrite(13, HIGH);
      digitalWrite(12, LOW); 
      SL=255;
      SR=255;
    }else{ 
      digitalWrite(13, LOW);
      digitalWrite(12, HIGH); 
      SL=255;
      SR=255;
    }
    }
  else if (UC>14) {
    digitalWrite(13, HIGH);
    digitalWrite(12, HIGH);
    if (UL<16 or UR<16){
      if (UL<16 and UR>16){
        SL=255;
        SR=SpeedArray[UL];
        
      } else if (UR<16 and UL>16){
        SL=SpeedArray[UR];
        SR=255;
      }else{
        SL=SpeedArray[UR];
        SR=SpeedArray[UL];
      }
  } 
   else {
   SL=255;
   SR=255;
  }  
  }

void MoveRobot (){
  analogWrite(3, SR);
  analogWrite(11, SL);
}
void Forward () {
  digitalWrite(12, HIGH); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);    
  digitalWrite(13, HIGH);  
  digitalWrite(8, LOW);
  analogWrite(11, SL);   
}
void Backward () {
  digitalWrite(12, LOW); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);       
  digitalWrite(13, LOW); 
  digitalWrite(8, LOW); 
  analogWrite(11, SL);   
}
void Left (){
  digitalWrite(12, HIGH); 
  digitalWrite(9, LOW); 
  analogWrite(3, SR);     
  digitalWrite(13, LOW); 
  digitalWrite(8, LOW);
  analogWrite(11, SL);   
}
void Right () {
  digitalWrite(12, LOW); 
  digitalWrite(9, LOW); 
  analogWrite(3, SR);    
  digitalWrite(13, HIGH); 
  digitalWrite(8, LOW);  
  analogWrite(11, SL);   
}
void Stop (){
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
}  
void FrontRight () {
  SR=SR/2;
  digitalWrite(12, HIGH); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);    
  digitalWrite(13, HIGH);  
  digitalWrite(8, LOW);
  analogWrite(11, SL);
}
void FrontLeft (){
  SL=SL/2;
  digitalWrite(12, HIGH); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);    
  digitalWrite(13, HIGH);  
  digitalWrite(8, LOW);
  analogWrite(11, SL);
}
void BackRight (){
  SR=SR/2;
  digitalWrite(12, LOW); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);    
  digitalWrite(13, LOW);  
  digitalWrite(8, LOW);
  analogWrite(11, SL);
}
void BackLeft (){
  SL=SL/2;
  digitalWrite(12, LOW); 
  digitalWrite(9, LOW);
  analogWrite(3, SR);    
  digitalWrite(13, LOW);  
  digitalWrite(8, LOW);
  analogWrite(11, SL);
}

…R

You did not answer my question in Reply #3

What happens if your phone remains connected but you don't send any character for a long time?

I reckon if you do that the car should revert to autonomous mode - is that what happens?

Let's deal with that before worrying about disconnecting the phone.

And I have just realized that you have told us nothing about the program running on your phone.

...R

.

https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller this is the android app i am using for control my project.

and problem is, when i disconnect (close Bluetooth) it is not going to autonomous mode

Robin2: You did not answer my question in Reply #3

remains stopped(i can see in motor shield both Leds on A and B motor open). i think it waiting for next "case"/button to press.

try adding a default statement to your switch case.. I think it is receiving a value on the serial that it doesnt know how to work with.. add a break in the default..

Chaitanya1: try adding a default statement to your switch case.. I think it is receiving a value on the serial that it doesnt know how to work with.. add a break in the default..

I try this default:

break;

or this also default: digitalWrite(9, LOW); digitalWrite(8, LOW); break; but still nothing :(

Wormwood: remains stopped(i can see in motor shield both Leds on A and B motor open). i think it waiting for next "case"/button to press.

That suggests to me that you experience the exact same problem if you don't send a character as you experience when you disconnect the phone.

If so that means that the problem is NOT caused by disconnecting the phone.

Replace if(Serial.available() > 0){ with the following

byte numBytes = Serial.available();
if (numBytes > 0) {
   Serial.print("Num Serial Bytes = ");
   Serial.println(numBytes);
   SL=255;
   SR=255;   
   state = Serial.read();
   // etc
}

and tell us what gets printed, and in what circumstances

...R

try adding a forward in the first else statement or some kind of LED indicator to check if the program flow is actually going in the else statement once bluetooth is disconnected..

Robin2: That suggests to me that you experience the exact same problem if you don't send a character as you experience when you disconnect the phone.

If so that means that the problem is NOT caused by disconnecting the phone.

Replace if(Serial.available() > 0){ with the following

byte numBytes = Serial.available();
if (numBytes > 0) {
   Serial.print("Num Serial Bytes = ");
   Serial.println(numBytes);
   SL=255;
   SR=255;   
   state = Serial.read();
   // etc
}

and tell us what gets printed, and in what circumstances

...R

http://postimg.org/image/frckywkct/ that iam getting

http://postimg.org/image/xkb5dpinn/

I add a Debug function and add it here:

break; } } else { ReadSensor (); AvoidWalls (); MoveRobot (); Debug(); } } Check when i disconnect Bluetooth it start print the Debug function that mean its go on "else" something else is problem them with motor shield because all led (4 leds - 2 in A motor 2 in B motor) is open.

Wormwood: http://postimg.org/image/frckywkct/ that iam getting

You have not related the data to the circumstances in which it is received. It must be obvious to you that most of it is irrelevant and that I am only interested in the data when you are NOT transmitting a character.

FORGET COMPLETELY about the issue of disconnecting the Bluetooth and just concentrate on the different behaviour when you send commands from the phone and when you don't send commands. By "don't send commands" I just mean don't touch anything on the phone.

It just occurs to me to ask whether the Android program sends one character whenever you press a button (which is what it should do) or whether it sends a repeating stream of characters (which it should NOT do).

...R

http://postimg.org/image/frckywkct/

I am geting this print (with this random numbers) without press something in my phone.

and here; http://postimg.org/image/xkb5dpinn/ u can see when i disconnect bluetooth is going to "else" case, thats mean its read sensor as we can see on image but robot is not moving (because pin 8 - 9 is sending HIGH with no reason).

Is possible to reset Arduino with code so when it disconnect say to my code reset system and all project start again?