why is this code crashing after a few hours

I’m running this code on a arduino attiny85 and after a few hours D0 either stays on HIGH or LOW
is it running out of memory or the buffer gets full, what it could be?

#include <SoftwareSerial.h>

// Definitions
#define rxPin 3 // 3 //10
#define txPin 4 //4 //11
short ledPin = 0; //0 //13

short countOfftimes = 0;
char id[12];
char deviceid[12] = {'C', '1', '0', '0', 'D', '2', '0', '0', '0', '1', '0', 'D'};

SoftwareSerial MySerial(rxPin, txPin);
void establishContact();

void setup() {
  pinMode(ledPin, OUTPUT);
}


void loop() {
  
  establishContact();  // send a byte to establish contact until receiver responds
  checkID();
  delay(100);
}

void checkID(){
    short k;
    char character;
    short matchExceptionCount = 0;
    bool isDeviceThere = false;
    short countentries = 0;
    
    while(MySerial.available() > 0) {
          if(MySerial.read() == 58){
              while(MySerial.available() > 0){
                id[countentries] = MySerial.read();
                if(countentries == 11){
                  break;  
                }
                countentries = countentries + 1;
              }
              isDeviceThere = isDeviceFound();
              break;
          }
      }    
   
    if(isDeviceThere == true){
      for(k = 0;k < 12; k++){
        MySerial.print(id[k]);
      }
    MySerial.println();  
    }

   delay(100);  
 
    
    if(isDeviceThere == true){
      digitalWrite(ledPin, HIGH); 
      countOfftimes = 0;
      delay(10);
    }
    else{
      
      if(countOfftimes > 5){ //time
        digitalWrite(ledPin, LOW);
        countOfftimes = 0;
        }
      countOfftimes = countOfftimes + 1;
      
    } 
    
}

bool isDeviceFound(){
  int j;
  for(j = 0; j < 12; j++){
    if(id[j] != deviceid[j]){
      return false;
    }
  }
  return true;
}

void establishContact() {
  while (MySerial.available() <= 0){
    MySerial.print("AT+DISC?"); 
    delay(1000);    
  }
}

The establishContact() function blocks waiting on a reply from MySerial. if that never happens from whatever it is you are communicating with, then it will just sit in that function forever. Do you really need to establishContact that often? Regardless you might want to include some form of timeout for this function. Maybe even have it return true or false depending on whether a reply was received or it timed out.

bool establishContact() {
byte timeout = 0;
while (MySerial.available() <= 0){
MySerial.print(“AT+DISC?”);
delay(1000);
if (++timeout > 10) return false;
}
return true;
}

The bluetooth module always responds back with the matching "deviceid[12]" sometimes a different ID, if the ID matches D0 would stay high if the id does not match D0 would go low, so you still think that could be the problem?
but I will try it too, and let you know.

The minimum size of the short data type is int. It makes no sense to use it on the AVR where you want to be more explicit about the size of your data types. Use int.

Always is great... Until it doesn't happen and establishContact() sits waiting assuming "always". It was the only blocking potential I could see.

Curious... What Bluetooth module are you using? The HC-05 uses AT+DISC for a disconnect. Is this an HM-10? It looks close to what you are reading but not exactly the same reply.

Hi,
What are you driving output D0 into, what is the load?

Is this thread the same problem? ? ?

Thanks.. Tom.. :slight_smile:

@wzaggle thanks, I tried and still crashes, I'm using the HM-10 the "AT+DISC" is the discovering command to find which ibeacon bluetooth are in range.

@aarg thank you too, I change the short to int, but still the same problem.

@TomGeorge this a new problem I got with a new batch of MCU, I previously made 20 of them and they worked and I now I made a new batch and the new ones have problems, I'm worry that the problem is on the new MCU but I never seen something like this before. the load is a NPN transistor with a relay

Is AT+VERS? the same results for both batches? I have built several versions of these scanners with several different types of devices. Some are rock solid like the RedBear BLE Nano. The cheaper modules out of China have been hit and miss with reliability. I guess you get what you pay for. The Nano was much more expensive but allowed me to write a lot of custom code even with the Arduino IDE that really speed up the process. With the Nano doing a consistent scan and offering a local buffer of results. ESP-32 has promise, but even after the last year of updates, it also continues to have a rather unreliable BLE stack for now and only runs consistent scanning for a few hours before it just stops seeing beacons.