Code for Adafruit Trinket almost working, but not quite

Hello

I was wondering if anyone had any advice they might be willing to give me about my Trinket sketch as i’m baffled.

I am using a 5V Trinket from Adafruit and utilizing the PWM ability to cross fade 3 LEDs. When my input pin (GPIO #2) goes HIGH, the cross fade begins, and when it goes low, I want all 3 LEDs to turn off. However, after the cycle starts, if the input pin goes LOW, instead of actually turning the LEDs all completely off like i would like, they get “stuck” with either 1 or 2 of them still on (depending on where in the cross fade they are). Any help would be appreciated greatly, as i am still very new to programing arduinos and have been at this for many hours. Thank you in advance.

  //****REMEMBER TO RENAME INT & PARAMETER NAMES*****
  
  
  
  // Output
  int outLed   = 4;   // NAME COLOR LED LATER
  int greenPin = 0;  //  NAME COLOR LED LATER
  int bluePin  = 1;  //  NAME COLOR LED LATER
  
  //Input
  int inputPin = 2; //Input Actuator from Wave Shield
  
  // Program variables ***RENAME BEFORE FINAL***
  int redVal   = 255; // Variables to store the values to send to the pins
  int greenVal = 1;   // Initial values are Red full, Green and Blue off
  int blueVal  = 1;
  
  int i = 0;     // Loop counter    
  int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
  
  
  void setup()
  {
    //***REMAN BEFORE FINAL***
    PWM4_init();
    pinMode(outLed, OUTPUT); 
    pinMode(greenPin, OUTPUT);   
    pinMode(bluePin,  OUTPUT); 
   pinMode(inputPin, INPUT);
   
    }
  
  
  // Main program
  void loop(){
  
    int val = digitalRead(inputPin);
    if (val == HIGH) //Turns Trinket on if GPIO#2 is HIGH
    {
      crossfade();
    }
    else {      //Turns Trinket off if GPIO#2 is LOW
    
    redVal = 1;
    greenVal = 1;
    blueVal = 1;
    }
  }
    
  //CROSSFADE ROUTINE
  void crossfade()  {  
    
    i += 1;      // Increment counter
    if (i < 255) // First phase of fades
    {
      redVal   -= 1; // Red down
      greenVal += 1; // Green up
      blueVal   = 1; // Blue low
    }
    else if (i < 509) // Second phase of fades
    {
      redVal    = 1; // Red low
      greenVal -= 1; // Green down
      blueVal  += 1; // Blue up
    } 
    else if (i < 763) // Third phase of fades
    {
      redVal  += 1; // Red up
      greenVal = 1; // Green low
      blueVal -= 1; // Blue down
    }
    else // Re-set the counter, and start the fades again
    {
      i = 1;
    }  
  
    analogWrite4(redVal);   // Write current values to LED pins
    analogWrite(greenPin, greenVal); 
    analogWrite(bluePin,  blueVal);  
  
     delay(2); // ***THIS IS USED TO ADJUST SPEED OF INDIVIDUAL LED FADES***
      }
      
      //NECESSARY TO MAKE GPIO#4 WORK W/ PWM
  void PWM4_init() {
        // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1
        TCCR1 = _BV (CS10);           // no prescaler
        GTCCR = _BV (COM1B1) | _BV (PWM1B);  //  clear OC1B on compare
        OCR1B = 0;                  // duty cycle initialize to 50%
        OCR1C = 255;                  // frequency
      }
      // Function to allow analogWrite on Trinket GPIO #4
      void analogWrite4(uint8_t duty_value) { 
        OCR1B = duty_value;  // duty may be 0 to 255 (0 to 100%)
      }

Update*

Ok, so after some trial and error, and some experimentation, I actually got it working. In case anyone wants to use it, the program ain’t pretty, but it’ll do.

//When GPIO#2 is HIGH, Trinket performs a 3 LED crossfade using GPIO# 0,2,&4. 
//When GPIO#2 is LOW, Trinket fades all LEDs, essentially turning the Trinket Off
  
  // Output
  int outLed   = 4;   // NAME COLOR LED LATER
  int greenPin = 0;  //  NAME COLOR LED LATER
  int bluePin  = 1;  //  NAME COLOR LED LATER
  int brightness = 0;    // how bright the LED is
  int fadeAmount = 5;    // how many points to fade the LED by
  
  //Input
  int inputPin = 2; //Input Actuator from Wave Shield
  
  // Program variables ***RENAME BEFORE FINAL***
  int redVal   = 255; // Variables to store the values to send to the pins
  int greenVal = 1;   // Initial values are Red full, Green and Blue off
  int blueVal  = 1;
  
  int i = 0;     // Loop counter    
  int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
  
  
  void setup()
  {
    //***REMAN BEFORE FINAL***
    PWM4_init();
    pinMode(outLed, OUTPUT); 
    pinMode(greenPin, OUTPUT);   
    pinMode(bluePin,  OUTPUT); 
   pinMode(inputPin, INPUT);
   
    }
  
  
  // Main program
  void loop(){
  
    int val = digitalRead(inputPin);
    if (val == HIGH) //Turns Trinket on if GPIO#2 is HIGH
    {
      crossfade();
    }
  else{     
  fadeled1();
  
  fadeled2();
  
  fadeled3();                          
}
    
    //redVal = 0;
    //greenVal = 0;
    //blueVal = 0;
    }
  //}
    
  //CROSSFADE ROUTINE
  void crossfade()  {  
    
    i += 1;      // Increment counter
    if (i < 255) // First phase of fades
    {
      redVal   -= 1; // Red down
      greenVal += 1; // Green up
      blueVal   = 1; // Blue low
    }
    else if (i < 509) // Second phase of fades
    {
      redVal    = 1; // Red low
      greenVal -= 1; // Green down
      blueVal  += 1; // Blue up
    } 
    else if (i < 763) // Third phase of fades
    {
      redVal  += 1; // Red up
      greenVal = 1; // Green low
      blueVal -= 1; // Blue down
    }
    else // Re-set the counter, and start the fades again
    {
      i = 1;
    }  
  
    analogWrite4(redVal);   // Write current values to LED pins
    analogWrite(greenPin, greenVal); 
    analogWrite(bluePin,  blueVal);  
  
     delay(2); // ***THIS IS USED TO ADJUST SPEED OF INDIVIDUAL LED FADES***
      }
      
      //NECESSARY TO MAKE GPIO#4 WORK W/ PWM
  void PWM4_init() {
        // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1
        TCCR1 = _BV (CS10);           // no prescaler
        GTCCR = _BV (COM1B1) | _BV (PWM1B);  //  clear OC1B on compare
        OCR1B = 0;                  // duty cycle initialize to 50%
        OCR1C = 255;                  // frequency
      }
      // Function to allow analogWrite on Trinket GPIO #4
      void analogWrite4(uint8_t duty_value) { 
        OCR1B = duty_value;  // duty may be 0 to 255 (0 to 100%)
      }
      
      void fadeled1()  {
  // set the brightness of pin 10:
  analogWrite4(brightness);    

  

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

void fadeled2()  {
  // set the brightness of pin 11:
  analogWrite(greenPin, brightness);    

  

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

void fadeled3()  {
  // set the brightness of pin 9:
  analogWrite(bluePin, brightness);    

  

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}