Serial Coms fail after two cycles

Hello everyone. I have a arduino program that sends at commands over serial to the hc06 Bluetooth module. It works great but after I send and receive data two times its like the serial connection freezes and noting happens. The program is suppost to set a relay to high when it detects my phones mac address in range so I have it continually scanning the area but it freezes after two scans

#include <SoftwareSerial.h>
int unlockTimes =0;
int comFail =0;
int session =0;
#define DEBUG true
#define bt_power 7
#define bt_key_power 8
#define indication_led 13 
SoftwareSerial esp8266(10, 11); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  // set the pins to OUTPUT
  pinMode(bt_power, OUTPUT);  
  pinMode(bt_key_power, OUTPUT);
  pinMode(indication_led, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  
  // set the pins to LOW
  digitalWrite(bt_power, LOW);
  digitalWrite(bt_key_power, LOW);
  digitalWrite(indication_led, LOW);
  
  /************************************************
  Setting the pins to low is important because 
  in order for us to get into AT mode the key pin
  has to be set to Ground FIRST. Many tutorials out
  there fail to mention this important fact and 
  therefore many people have problems with getting 
  into the AT mode of the HC-05
  ************************************************/
  
  // make sure the key has been LOW for a bit
  delay(100);
  
  // set the key pin to High
  digitalWrite(bt_key_power, HIGH);
  
  // small delay
  delay(100);
  
  // now power on the BT
  digitalWrite(bt_power, HIGH);
  
  // start our serial so we can send and recieve
  // information from the BT module
  Serial.begin(9600);
  esp8266.begin(38400); // your esp's baud rate might be different
  Serial.print("Bluetooth scanner booting, Please wait....\r\n");
 
  sendData("AT+CMODE=0\r\n",2000,DEBUG); // reset module
  Serial.print("Setting Role: ");
  sendData("AT+ROLE=1\r\n",1000,DEBUG); // configure as access point
  Serial.print("Initiating device: ");
  sendData("AT+INIT\r\n",1000,DEBUG); // get ip address
 // sendData("AT+BIND=C14,20,E1F04E\r\n",1000,DEBUG); // configure for multiple connections
//  sendData("AT+PAIR=C14,20,E1F04E,30\r\n",1000,DEBUG); // turn on server on port 80
  Serial.print("Bluetooth module initiated successfully. Starting program!\r\n");
}

void loop()
{
  String c = "";
  c = sendData("AT+RNAME?C14,20,E1F04E\r\n",5000,DEBUG); // get ip address
  //Serial.print(c);
  if(c == "+RNAME:SCH-I535\r\nOK\r\n")
  {
    if(unlockTimes == 0)
    {
      unlock();
    }
    Serial.print("Security Status: Unlocked\r\n");
    Serial.println("Rebooting. . .");
    /*delay(100); // Give the computer time to receive the "Rebooting. . ." message, or it won't show up
    void (*reboot)(void) = 0; // Creating a function pointer to address 0 then calling it reboots the board.
    reboot();
    unlockTimes = 1;
    */
  }
  else
  {
    if(comFail == 0)
    {
      lock();
    }
    Serial.print("Security Status: Locked\r\n");
    //Serial.print(c);
    unlockTimes = 0;
    comFail = 1;
  }

}

String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    
    esp8266.print(command); // send the read character to the esp8266
    
    long int time = millis();
    
    while( (time+5000) > millis())
    {
      while(esp8266.available())
      {
        
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }
    
    if(debug)
    {
      Serial.print(response);
    }
    
    return response;
}

String unlock()
{
    digitalWrite(13, HIGH);
    delay(750);
    digitalWrite(13, LOW);

}
String lock()
{
  digitalWrite(2, HIGH);
  delay(750);
  digitalWrite(2, LOW);
  
}

Any help would be greatly appreciated!

  // now power on the BT
  digitalWrite(bt_power, HIGH);

It looks like you are powering the BT module from an Arduino pin. How much current does it use because there is a limit as to how much current a pin can safely provide.

I see Strings used throughout the program. Why, when strings (null terminated char arrays) are so much more memory efficient ?

String unlock()
{
  digitalWrite(13, HIGH);
  delay(750);
  digitalWrite(13, LOW);

}
String lock()
{
  digitalWrite(2, HIGH);
  delay(750);
  digitalWrite(2, LOW);

}

Why have functions with return types (especially Strings) that do not return anything ?

SoftwareSerial esp8266(10, 11); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3

I presume that your wiring matches the code rather than the comments.

  pinMode(1, OUTPUT);Why ?

I don't think this is your problem, but this will fail after a while, when millis() rolls over:

   long int time = millis();
   
    while( (time+5000) > millis())

should be:

   unsigned long int time = millis();
   
    while( millis() - time <= 5000)

for reasons that would be repetitious to describe here. But the requirements are spelled out in the millis() documentation.

aarg:
I don't think this is your problem, but this will fail after a while, when millis() rolls over:

   long int time = millis();

while( (time+5000) > millis())



should be:


unsigned long int time = millis();
 
    while( millis() - time <= 5000)



for reasons that would be repetitious to describe here. But the requirements are spelled out in the millis() documentation.

I was testing an idea and I forgot to change it back before posting. Thanks!

UKHeliBob:

  // now power on the BT

digitalWrite(bt_power, HIGH);


It looks like you are powering the BT module from an Arduino pin. How much current does it use because there is a limit as to how much current a pin can safely provide.

I see Strings used throughout the program. Why, when strings (null terminated char arrays) are so much more memory efficient ?



String unlock()
{
  digitalWrite(13, HIGH);
  delay(750);
  digitalWrite(13, LOW);

}
String lock()
{
  digitalWrite(2, HIGH);
  delay(750);
  digitalWrite(2, LOW);

}



Why have functions with return types (especially Strings) that do not return anything ?



SoftwareSerial esp8266(10, 11); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3


I presume that your wiring matches the code rather than the comments.

`  pinMode(1, OUTPUT);`Why ?

The current draw on the Bluetooth module im not sure off. I got that sequence from an online post. What is the max current draw? I will see how much the hc06 draws.

As of now I was trying to get the program working and then work on the efficiency side of things. I haven't quite figured out the pros and cons of different variables yet.

Also the comments weren't updated from when I was editing the code. I know its good practice to, but it just slipped my mind. also with the pinmode 1 I was just telling the arduino to set pin 1 to output for the unlock function. I know in the code I posted the unlock function is tied to 13, but that's temporary so I can notice when the arduino sees my phone

Sorry about the mess of code. Will try to clean it up

If you are usingdigitalWrite(bt_power, HIGH); to switch on power for the Bluetooth module it suggests you are drawing power directly from an Arduino I/O pin, The ABSOLUTE max for them is 40mA and 20mA is much more sensible.

A Bluetooth module will need a lot more current than that. You are risking danage to your Arduino.

...R

From Uno reference page

A maximum of 40mA is the value that must not be exceeded on any I/O pin to avoid permanent damage to the microcontroller.

As of now I was trying to get the program working and then work on the efficiency side of things. I haven't quite figured out the pros and cons of different variables yet.

What if using inappropriate variables prevents you getting the program working ?

I was just telling the arduino to set pin 1 to output for the unlock function.

That's odd because the unlock() function does not use pin 1

Robin2:
If you are usingdigitalWrite(bt_power, HIGH); to switch on power for the Bluetooth module it suggests you are drawing power directly from an Arduino I/O pin, The ABSOLUTE max for them is 40mA and 20mA is much more sensible.

A Bluetooth module will need a lot more current than that. You are risking danage to your Arduino.

...R

Thank you! I will switch it away from the I/O pin

UKHeliBob:
From Uno reference pageWhat if using inappropriate variables prevents you getting the program working ?
That's odd because the unlock() function does not use pin 1

The unlock function will use pin 1 in application. Its using pin 13 for testing because of the onboard led

The unlock function will use pin 1 in application.

Are you planning on doing any output using hardware serial in your application ?

UKHeliBob:
Are you planning on doing any output using hardware serial in your application ?

Im not quite sure what you mean by that. If you mean using a terminal then no. Once in application im hoping for it to be a set it and leave it type of situation.

The reason I ask is that pin 1 is used by the hardware serial interface (Serial.print() etc) as its transmit pin so if you plan to output anything to Serial, even if nothing is there to receive it, then using pin 1 for something else is not to be recommended.

UKHeliBob:
The reason I ask is that pin 1 is used by the hardware serial interface (Serial.print() etc) as its transmit pin so if you plan to output anything to Serial, even if nothing is there to receive it, then using pin 1 for something else is not to be recommended.

I never even thought about that. Thanks!