Bluetooth App Buttons Go Wrong After An Event

Hello everyone. I'm currently working on a small beginner Arduino project. Summary of what I'm doing : sensor motion waits and sends warning to phone via SMS and Bluetooth app if it detects motion, then it pauses until I press the "Reset" button on the app to allow the whole process to go again. The app works just fine with connecting to Bluetooth device, pressing buttons before connecting to Bluetooth device is also done easily, it can even trigger the alarm process. However after the connection, all buttons become unavailable to press, and the phone says that "App is not responding". My prediction is that something is wrong with the Clock blocks so can anyone look into it for me please. I will post my whole blocks and Arduino file for your best undestanding. Thank you!

code_do_an.txt (2.7 KB)

Here is the code, to save time:

#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
#include <TinyGPS++.h>
int pir = 0;
int mode = 0;
int i;
int reset = 1;
byte bt[1]={0};
String sdt = "0703696455";
SoftwareSerial gsm(4,5);
AltSoftSerial ss;
TinyGPSPlus gps;
void gsm_setup();
void gsm_alert();
void gsm_button();
void choosemode();

void setup()
{
  Serial.begin(9600);
  gsm.begin(9600);
  ss.begin(9600);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(13, INPUT);
  pinMode(10, INPUT);
  attachInterrupt(digitalPinToInterrupt(2),choosemode,RISING);
  gsm_setup();
}

void loop()
{ 
  while (ss.available())
  gps.encode(ss.read());
  pir = digitalRead(10);
  while (Serial.available())
  {
    Serial.readBytes(bt,1);
  }
  switch (bt[0])
  {
    case 0:
    {
      break;
    }
    case 1:
    {
      reset=1;
      bt[0]=0;
      break;
    }
  }
  delay(1000);
  switch (mode)
  {
    case 0:
    {
      break; 
    }
    case 1:
    {
      if (pir == HIGH)
      { 
        if (reset == 1)
        {
          digitalWrite(11, HIGH);
          delay(1000);
          digitalWrite(11, LOW);
          delay(1000);
          gsm_alert();
          Serial.write(1);
          reset=0;
          break;
        }
        else 
        {
          break;
        }
      }
      else
      {
       break; 
      }
    }
  }
}

void gsm_setup()
{
  gsm.println("ATE0");                            
  delay(1000);
  gsm.println("AT+IPR=9600");              
  delay(1000);
  gsm.println("AT+CMGF=1");                
  delay(1000);
  gsm.println("AT+CLIP=1");                  
  delay(2000);
  gsm.println("AT+CNMI=2,2,0,0,0");              
  delay(2000);
}

void gsm_alert()
{
  gsm.println("AT+CMGS=\"" + sdt + "\"");
  delay(3000);
  if (gps.time.isValid())
  {
    int realhour = gps.time.hour() + 7;
    if (realhour > 24) realhour = realhour - 24;
    if (realhour < 10) gsm.print("0");
    gsm.print(realhour);
    gsm.print(":");
    if (gps.time.minute() < 10) gsm.print("0");
    gsm.print(gps.time.minute());
    gsm.print(":");
    if (gps.time.second() < 10) gsm.print("0");
    gsm.print(gps.time.second());
  }
  else
  {
    gsm.print("INVALID");
  }
  gsm.print("  Co ke dot nhap");
  delay(100);
  gsm.println((char)26);
  delay(100);
}

void choosemode()
{
    switch (mode)
    {
      case 0:
      { 
        reset=1;
        mode=1;
        digitalWrite(11, HIGH);
        delay(1000); 
        digitalWrite(11,LOW);
        delay(1000);
        break; 
      }
      case 1:
      {
        mode=0;
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);
        delay(1000);
        break;
      }
    }
}

Regarding your AI2 blocks, I see you are repeatedly asking the BlueTooth client for an Unsigned ByteNumber regardless of whether or not there might be any bytes that have arrived in the BlueTooth Client's internal buffer.

Most BT apps guard such requests by a test (if/then) to see if the buffer has any Bytes Available (>0) before deciding to ask for a byte from the message.
That helps to prevent the app from getting stuck in the unrequited BT Receive block.

Oh wow my friend you just saved my day. It worked just like how I expected it to. Guess I still have a lot of practice to do. Anyway thank you again. I really appreciate your help ^^

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.