It works on nano but it crashes after a few hours on Attiny85

This code is to scan for beacon and if it finds it it turns on the led, it works great on the nano but on the attiny85 it would run good for half a day but it crashes after, how can I find out what’s wrong?

#include <SoftwareSerial.h>

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

short countOfftimes = 0;
char id[12];
char deviceid[12] = {'F', 'F', 'F', 'F', 'A', '0', '0', '5', 'E', '1', '9', '0'};
char exception[6] = {'C', 'h', 'a', 'r', 'g', 'e' };
bool exceptionFound = false;
//FFFFA005E190

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

void setup() {
  pinMode(ledPin, OUTPUT);
  MySerial.begin(9600);
  delay(1000);
  MySerial.print("AT+ROLE1"); 
  delay(1000);
  MySerial.print("AT+IMME1"); 
  delay(1000);
  MySerial.print("AT+RESET"); 
  delay(1000);

}


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

void checkID(){
    short k;
    char character;
    short matchExceptionCount = 0;
    exceptionFound = false;
    bool isDeviceThere = false;
    short countentries = 0;
    
    while(MySerial.available() > 0) {
          if(MySerial.read() == 58){
              while(MySerial.available() > 0){
                id[countentries] = MySerial.read();
                if(countentries < 6){
                  if(id[countentries] == exception[countentries]){
                    matchExceptionCount = matchExceptionCount + 1;
                    if(matchExceptionCount == 6){
                      exceptionFound = true;
                    }
                  } 
                }
                if(countentries == 12){
                  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(5000);
    }
    else{
      
      //digitalWrite(13, LOW);
      if(countOfftimes > 5){ //time
        digitalWrite(ledPin, LOW);
        countOfftimes = 0;
        }
      countOfftimes = countOfftimes + 1;
      
    } 
    
}

bool isDeviceFound(){
  int j;
  if(exceptionFound == true){
    return false;
  }
  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);    
  }
}

Probably a memory thing. IIRC the tiny85 has just barely enough memory to handle a software serial instance. So if you're going to use that you're going to have to be really careful about everything else you use. Get some F macros on the constant strings and see if that helps.

Look in checkID, you check for countentries being 12 AFTER you've already written to id[countentries]. So you might have tried to write to id[12]. id[12] doesn't exist, so you may be writing off the end of your array.

Delta_G:
Look in checkID, you check for countentries being 12 AFTER you've already written to id[countentries]. So you might have tried to write to id[12]. id[12] doesn't exist, so you may be writing off the end of your array.

Writing off the end of the array will definitely cause issues like that.

And compiling for different chip can result in things being in different parts of memory; I would venture a guess that on the Nano, whatever you're scribbling over doesn't happen to hose the whole sketch (at least in an immediately visible way) while on the tiny, something important happens to be after it.

Software serial doesn't use scads of memory AFAIK - it's terrible in numerous other ways, but memory is not one of them. That said, you might as well use the F() macro where appropriate!

Thanks I will use the F() macro, do you see any way I can make it crash so I don't have to wait a few hours to test if it works?

Sure, you could crash it but that wouldn't really test it would it?

Did you fix the array overrun as well? That's the more likely problem.

You have an array with indices that go from 0 to 11. Read through checkID and count as countentries goes up. You are right to stop it if you get to 12, but at the point you actually do that check and stop it you've already written to the array in spot 12. Either check the array index before you do the write or stop when you get to 11.

Thanks so I change if(countentries == 12) to if(countentries == 11)

very interesting, I am wondering how the array exception wouldn't occur on a nano but occur on a tiny85.

May be you can put a Serial.println("s1") after every loop/function, so you know which line fails after "hours" of run.

Also, are you sure you have the power specifications all met, may be it just resets(??), what are you powering the attiny with, and are there any more peripherals, if so what all?

Lastly what does the compilation verbose tell about the available dynamic memory?

saiko:
very interesting, I am wondering how the array exception wouldn't occur on a nano but occur on a tiny85.

It happens on both boards. It's just that on the nano whatever memory it stomped on wasn't something critical and didn't seem to harm anything. But on the 85, where the compilation is different and things end up in different places, whatever was after that array was something critical and it caused a problem.

Delta_G:
It happens on both boards. It's just that on the nano whatever memory it stomped on wasn't something critical and didn't seem to harm anything. But on the 85, where the compilation is different and things end up in different places, whatever was after that array was something critical and it caused a problem.

Thank you very much for the explanation!