VCNL4040 ambient sensor i2c and serial in loop after some time MCU freeze

VCNL4040 ambient sensor i2c and serial in loop after some time arduino nano freeze and both communication failed, when we restart serial communication comes again but I2C not...

please look into my code, may be i have done something wrong....

#include <AT24Cxx.h>
#include <SoftwareSerial.h>
#include <Wire.h>

//Click here to get the library: http://librarymanager/All#SparkFun_VCNL4040
#include "SparkFun_VCNL4040_Arduino_Library.h"

#define DEBUG 0


#define DATA 7  //PD3
#define CLOCK 4  //PD4
#define LATCH 5 //PD5
#define OE 6   //PD6
#define DP1 A0
#define DP2 A1
#define DP3 A2
#define DP4 A3

#define pinCONTROL 2

#define rxPin 2
#define txPin 3

#define i2c_address 0x50


byte temps[4];
unsigned char MODULE_ID[6] = {'0', '0', '.', '0', '2', '\0'};
byte MyAddress[2] = {48,50}; 

int a[32];
int j[4]={24,16,8,0};
unsigned char pre[5];
unsigned int ambientValue;
unsigned int proxValue;
unsigned int whiteValue;
int brightness;

byte byte_receive;
byte  state=0;
byte  cont=1;
byte  trace_OK=0;
byte  data[12];
byte buf[3];


long previousMillis = 0;
unsigned long currentMillis;
long interval = 5000;
unsigned int VAL;


AT24Cxx eep(i2c_address, 32);
SoftwareSerial RS485(rxPin, txPin); 
VCNL4040 proximitySensor;


void PinConfigure(){

  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(pinCONTROL,OUTPUT);
  
   
}

 
void setup()
{
  Serial.begin(9600);
  RS485.begin(9600);
  
  PinConfigure();
  Wire.begin(); //Join i2c bus
  
  proximitySensor.begin(); //Initialize the sensor
  TWBR = 12;
  //Turn on everything
  proximitySensor.powerOnProximity();
  proximitySensor.powerOnAmbient();
  proximitySensor.enableWhiteChannel();

  digitalWrite(pinCONTROL,LOW);
  digitalWrite(CLOCK, LOW);
  digitalWrite(LATCH, LOW);
  analogWrite(OE, 240);  

  
 
}
  
void loop()
{
 digitalWrite(DATA, LOW);
 digitalWrite(CLOCK, LOW);
 digitalWrite(LATCH, LOW);
 digitalWrite(pinCONTROL,LOW);

if(48 == MyAddress[0] && 49 == MyAddress[1]){
                
    ambientValue = proximitySensor.getAmbient();
    VAL = map(ambientValue,0,65535,0,240);

    if(VAL > 240){
     VAL = 240; 
     }else if(VAL < 0){
      VAL = 0; 
     }
    VAL = 240 - VAL; //REVERSE 
    
    if(VAL==0 || VAL < 0){ //once
       buf[0] = 48;
       buf[1] = 48;
       buf[2] = 48;
    }else if(VAL > 0 && VAL < 10){ //once
       
       dtostrf(VAL, 1, 0, &buf[2]);
       buf[0] = 48;
       buf[1] = 48;
    }else if(VAL > 9 && VAL < 100){ //tens
     
      dtostrf(VAL, 2, 0, &buf[1]);
       buf[0] = 48;
    }else if(VAL > 99 && VAL < 256){
      dtostrf(VAL, 2, 0, &buf[0]);
    }
 
    
    if(buf[0] == eep.read(50) && buf[1] == eep.read(51) && buf[2] == eep.read(52)){
      
    }
    else{
      eep.write(50, buf[0]);delay(15);
      eep.write(51, buf[1]);delay(15);
      eep.write(52, buf[2]);delay(15);
     
     sendACK(60,80,66,84,MyAddress[0],MyAddress[1], buf[0], buf[1], buf[2], buf[3], 66,62);
    
      
    }
 }

   if(Serial.available()){
    //Serial.println("SR");
   while (Serial.available() > 0){

   //Serial.println("WHILE : ");
    byte_receive=Serial.read();
    
    //Serial.print(byte_receive);
    
     if(byte_receive == 40){ //if first byte is < 
       state=1;
       cont=1;
       //Serial.println("40");
     } else if (state==1 && cont<12){ // if second byte received then save rest bytes in another array
       data[cont-1] = byte_receive;

       state=1;
       cont=cont+1;
 

       if(cont==12){
        cont=1;
        state=0;

        if(data[0] == 67 && data[1] == 83 && data[9] == 89 && data[10] == 41)
        {
          //Serial.println("TRUE COMMAND");
          
          
          if(data[2] == MyAddress[0] && data[3] == MyAddress[1]){

                 
                  eep.write(0, data[4]);delay(10);
                  eep.write(1, data[5]);delay(10);
                  eep.write(2, data[6]);delay(10);
                  eep.write(3, data[7]);delay(10);
                  eep.write(4, data[8]);delay(10);

                 sendACK(60,80,75,data[2],data[3], data[4], data[5], data[6], data[7], data[8], 66,62);
  
                 Show_Price();
              
              
             }
             
          }
          else if(data[0] == 67 && data[1] == 77 && data[9] == 89 && data[10] == 41)
         {
                print_message(MODULE_ID);
                delay(5000);               
                Show_Price();
           
            
          }
          else if(data[0] == 67 && data[1] == 66 && data[9] == 89 && data[10] == 41)
         {
                temps[0] = data[5];
                temps[1] = data[6];
                temps[2] = data[7];
                temps[3] = data[8];
                temps[4] = NULL;
                brightness =  (int)strtod(temps, NULL);
                analogWrite(OE, brightness);
           
          }
          else if(data[0] == 67 && data[1] == 69 && data[9] == 89 && data[10] == 41)
         { 
              if(data[2] == MyAddress[0] && data[3] == MyAddress[1]){

               
                  sendACK(60,80,80,MyAddress[0],MyAddress[1], 48, 48, 48, 48, 48, 66,62);

              }
            
          }
          
        }
        
     }
     
     } 
    
   }

I'm missing a wiring diagram of your setup.

  TWBR = 12;

You should not change the I2C hardware registers directly if you use the Wire library. There's a method to do this (setClock()).

That code doesn't compile! Post complete code!

#include <AT24Cxx.h>
#include <SoftwareSerial.h>
#include <Wire.h>

//Click here to get the library: http://librarymanager/All#SparkFun_VCNL4040
#include "SparkFun_VCNL4040_Arduino_Library.h"

#define DEBUG 0


#define DATA 7  //PD3
#define CLOCK 4  //PD4
#define LATCH 5 //PD5
#define OE 6   //PD6
#define DP1 A0
#define DP2 A1
#define DP3 A2
#define DP4 A3

#define pinCONTROL 2

#define rxPin 2
#define txPin 3

#define i2c_address 0x50


byte temps[4];
unsigned char MODULE_ID[6] = {'0', '0', '.', '0', '2', '\0'};
byte MyAddress[2] = {48,50}; 

int a[32];
int j[4]={24,16,8,0};
unsigned char pre[5];
unsigned int ambientValue;
unsigned int proxValue;
unsigned int whiteValue;
int brightness;

byte byte_receive;
byte  state=0;
byte  cont=1;
byte  trace_OK=0;
byte  data[12];
byte buf[3];

// State of each switch (0 or 1)
int SW1;
int SW2;
int SW3;
int SW4;

long previousMillis = 0;
unsigned long currentMillis;
long interval = 5000;
unsigned int VAL;


AT24Cxx eep(i2c_address, 32);
SoftwareSerial RS485(rxPin, txPin); 
VCNL4040 proximitySensor;


void PinConfigure(){

  pinMode(DATA, OUTPUT);
  pinMode(CLOCK, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(DP1, INPUT);
  pinMode(DP2, INPUT);
  pinMode(DP3, INPUT);
  pinMode(DP4, INPUT);
  pinMode(pinCONTROL,OUTPUT);
  
   
}

 
void setup()
{
  Serial.begin(9600);
  RS485.begin(9600);
  
  PinConfigure();
  Wire.begin(); //Join i2c bus
  
  proximitySensor.begin(); //Initialize the sensor
    //Turn on everything
TWBR = 12;
  proximitySensor.powerOnProximity();
  proximitySensor.powerOnAmbient();
  proximitySensor.enableWhiteChannel();

  digitalWrite(pinCONTROL,LOW);
  digitalWrite(CLOCK, LOW);
  digitalWrite(LATCH, LOW);
  analogWrite(OE, 240);  

  
SW1 = digitalRead(DP1);
SW2 = digitalRead(DP2);
SW3 = digitalRead(DP3);
SW4 = digitalRead(DP4);

     if(SW1==HIGH && SW2==LOW && SW3==LOW && SW4==LOW)    {MODULE_ID[3]='0'; MODULE_ID[4]='1'; MyAddress[0]=48; MyAddress[1]=49;}
else if(SW1==HIGH && SW2==HIGH && SW3==LOW && SW4==LOW)   {MODULE_ID[3]='0'; MODULE_ID[4]='2'; MyAddress[0]=48; MyAddress[1]=50;}
else if(SW1==HIGH && SW2==HIGH && SW3==HIGH && SW4==LOW)  {MODULE_ID[3]='0'; MODULE_ID[4]='3'; MyAddress[0]=48; MyAddress[1]=51;} 
else if(SW1==HIGH && SW2==HIGH && SW3==HIGH && SW4==HIGH) {MODULE_ID[3]='0'; MODULE_ID[4]='4'; MyAddress[0]=48; MyAddress[1]=52;} 


      
   print_message(MODULE_ID);
   delay(5000);               
  
  
}
  
void loop()
{
 digitalWrite(DATA, LOW);
 digitalWrite(CLOCK, LOW);
 digitalWrite(LATCH, LOW);
 digitalWrite(pinCONTROL,LOW);

if(48 == MyAddress[0] && 49 == MyAddress[1]){
                
    ambientValue = proximitySensor.getAmbient();
    VAL = map(ambientValue,0,65535,0,240);

    if(VAL > 240){
     VAL = 240; 
     }else if(VAL < 0){
      VAL = 0; 
     }
    VAL = 240 - VAL; //REVERSE 
    
    if(VAL==0 || VAL < 0){ //once
       buf[0] = 48;
       buf[1] = 48;
       buf[2] = 48;
    }else if(VAL > 0 && VAL < 10){ //once
       
       dtostrf(VAL, 1, 0, &buf[2]);
       buf[0] = 48;
       buf[1] = 48;
    }else if(VAL > 9 && VAL < 100){ //tens
     
      dtostrf(VAL, 2, 0, &buf[1]);
       buf[0] = 48;
    }else if(VAL > 99 && VAL < 256){
      dtostrf(VAL, 2, 0, &buf[0]);
    }
 
    
    if(buf[0] == eep.read(50) && buf[1] == eep.read(51) && buf[2] == eep.read(52)){
      
    }
    else{
      eep.write(50, buf[0]);delay(15);
      eep.write(51, buf[1]);delay(15);
      eep.write(52, buf[2]);delay(15);
     
     sendACK(60,80,66,84,MyAddress[0],MyAddress[1], buf[0], buf[1], buf[2], buf[3], 66,62);
    
      
 }
 }

   if(Serial.available()){
    //Serial.println("SR");
   while (Serial.available() > 0){

   //Serial.println("WHILE : ");
    byte_receive=Serial.read();
    
    //Serial.print(byte_receive);
    
     if(byte_receive == 40){ //if first byte is < 
       state=1;
       cont=1;
       //Serial.println("40");
     } else if (state==1 && cont<12){ // if second byte received then save rest bytes in another array
       data[cont-1] = byte_receive;

       state=1;
       cont=cont+1;
 

       if(cont==12){
        cont=1;
        state=0;

        if(data[0] == 67 && data[1] == 83 && data[9] == 89 && data[10] == 41)
        {
          //Serial.println("TRUE COMMAND");
          
          
          if(data[2] == MyAddress[0] && data[3] == MyAddress[1]){

                 
                  eep.write(0, data[4]);delay(10);
                  eep.write(1, data[5]);delay(10);
                  eep.write(2, data[6]);delay(10);
                  eep.write(3, data[7]);delay(10);
                  eep.write(4, data[8]);delay(10);

                 sendACK(60,80,75,data[2],data[3], data[4], data[5], data[6], data[7], data[8], 66,62);
  
                 
              
              
             }
             
          }
          else if(data[0] == 67 && data[1] == 77 && data[9] == 89 && data[10] == 41)
         {
                print_message(MODULE_ID);
                delay(5000);               
               
           
            
          }
          else if(data[0] == 67 && data[1] == 66 && data[9] == 89 && data[10] == 41)
         {
                temps[0] = data[5];
                temps[1] = data[6];
                temps[2] = data[7];
                temps[3] = data[8];
                temps[4] = NULL;
                brightness =  (int)strtod(temps, NULL);
                analogWrite(OE, brightness);
           
          }
          else if(data[0] == 67 && data[1] == 69 && data[9] == 89 && data[10] == 41)
         { 
              if(data[2] == MyAddress[0] && data[3] == MyAddress[1]){

               
                  sendACK(60,80,80,MyAddress[0],MyAddress[1], 48, 48, 48, 48, 48, 66,62);

              }
            
          }
          
        }
        
     }
     
     } 
    
   }

   

 
}

That code doesn't compile! Post complete code!

Does the posted code really compile for you? In my IDE it doesn't. Please don't waste our time. And format the code, it's horrible to read it! The IDE has an AutoFormat option in the Tools menu.