my sketch seems to hang/freeze at random before resuming operation

ok so Ive used an arduino nano to control an RGB light pulse inside a drum, the trigger is a piezo the red green blue digital outputs use the 3 PWM pins that then control some mjl21194 power transistors.

for some reason the sketch Hangs/freezes at random for a random period of time. im pretty sure its in the sketch and not the hardware because i created a very basic boring blink sketch that can fire off when the piezo is hit and that sketch works perfectly, no hangups, the problem sketch is a little more complicated because is has the light value fading away after the hit. i will attach the sketch. if anyone can help point what in my sketch might be causing the hang up that would be great.

thank you
Eric

simpleoneshotlights_with_blue_fade.ino (5.15 KB)

OP’s sketch so everybody can have a look without downloading it.

//---------------------------------------------lights variables------------------------------------------------------------------
const byte pwmLEDb = 6;
const byte pwmLEDg = 5;
const byte pwmLEDr = 3; 

// constants for min and max PWM
const int minPWM  = 0;
const int maxPWM  = 180; //255 is full power
const int maxPWMb = 65; //255 is full power

byte colorValueb = 0;
byte colorValueg = 0;
byte colorValuer = 0;

boolean OKtoFade = false;
 

 
// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;
int fadeValueb = 0;
 
// How smooth to fade?
byte fadeIncrement = 40;
byte fadeIncrementb = 2;

// millis() timing Variable, just for fading
unsigned long previousFadeMillis;

unsigned long currentMillis;
 
// How fast to increment?
int fadeInterval = 20;
 
//-------------------------------------- pad variables-----------------------------------------------------------------
int pinRead;
int pinAssignments[2] ={
  A0, A1};

int PadCutOff[2] = 
{
  350,1000};           // Minimum Analog value to cause a drum hit
int MaxPlayTime[2] = {
  70,70};               // Cycles before a 2nd hit is allowed

//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[2] = {
  0,0};                   // Array of flags of pad currently playing
int PinPlayTime[2] = {
  0,0};                     // Counter since pad started to play
int pin = 0;     
int hitavg = 0;

//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup() 
{
  // put pwmLED into known state (off)
  analogWrite(pwmLEDb, colorValueb);
  analogWrite(pwmLEDg, colorValueg);
  analogWrite(pwmLEDr, colorValuer);
  pinMode(A0, INPUT); 
  //Serial.begin(57600);
  
}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop() 
{
//-------------------------------------------pad program-------------------------------------------------------------  
  for(int pin=0; pin < 2; pin++)                          //
  {
       
    hitavg = analogRead(pinAssignments[pin]);  
     
    if((hitavg < PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        //Serial.print(hitavg);
        //Serial.print(" ");
        //Serial.println(maxPWM);
         OKtoFade = true;
         fadeValue = maxPWM;
         fadeValueb = maxPWMb;
       
        PinPlayTime[pin] = 0;
        activePad[pin] = true;
      }
      else
      {
        PinPlayTime[pin] = PinPlayTime[pin] + 1;
      }
    }
    else if((activePad[pin] == true))
    {
      PinPlayTime[pin] = PinPlayTime[pin] + 1;
      if(PinPlayTime[pin] > MaxPlayTime[pin])
      {
        activePad[pin] = false;
      }
    }
  } 
  //-------------------------------------------------lights program---------------------------------------------------
    if (OKtoFade == true )
  {   
   // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  currentMillis = millis();
    
  // is it time to update yet?
  // if not, nothing happens
  if (currentMillis - previousFadeMillis >= fadeInterval - 3) {
    // yup, it's time!
 
 
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      fadeValueb = fadeValueb - fadeIncrementb;
      if (fadeValue <= minPWM && fadeValueb <= minPWM) {
        // At min, limit and change direction
        fadeValue  = minPWM;
        fadeValueb = minPWM;
        OKtoFade   = false;
      }
    
    // Only need to update when it changes
    if (fadeValue > maxPWMb) {
    int xb = map(fadeValue, 0, 255, 0, 225);
    int xg = map(fadeValue, 0, 255, 0, 255);
    int xr = map(fadeValue, 0, 255, 0, 110);
    int yb = map(fadeValue, 0, 255, 0, 30);   // original setting is 30
    int yg = map(fadeValue, 0, 255, 30, 1);  // original setting is 30
    int yr = map(fadeValue, 0, 255, 10, 1);  // original setting is 10
    
    colorValueb = xb + yb;
    colorValueg = xg/yg;
    colorValuer = xr/yr;
    
    } else {
      
        colorValueg = 0;
        colorValuer = 0;
        int xb = map(fadeValueb, 0, 255, 0, 255);
        colorValueb = xb;
      
    }
    
    analogWrite(pwmLEDb, colorValueb);
    //Serial.print(colorValueb);
    //Serial.print(" ");
    analogWrite(pwmLEDg, colorValueg);
    //Serial.print(colorValueg);
    //Serial.print(" ");
    analogWrite(pwmLEDr, colorValuer);
    //Serial.println(colorValuer);
      
 
    // reset millis for the next iteration (fade timer only)

    previousFadeMillis = currentMillis;
    
  }
  
   
  }
  delay(1);

}