Fading RGB with speed change

Hello,

I have a working code but I dont know how to build in a button which I can use to change the speed of the fade. I also need to make the fade go away before the next fade starts. Could U help me?

// Output
int redPin = 9;   // Red LED,  
int grnPin = 10;  // Green LED, 
int bluPin = 11;  // Blue LED,  

//kleuren waarden
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };



// Set initial color
int redVal = black[0];
int grnVal = black[1]; 
int bluVal = black[2];

int wait = 1;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
int j = 0;          // Loop counter for repeat

// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;

// Set up the LED outputs
void setup()
{
  pinMode(redPin, OUTPUT);   // sets the pins as output
  pinMode(grnPin, OUTPUT);   
  pinMode(bluPin, OUTPUT); 

  if (DEBUG) {           // If we want to see values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput 
  }
}

// Main program: list the order of crossfades
void loop()
{
  crossFade(red);
  crossFade(green);
  crossFade(blue);
 

  if (repeat) { 
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}


int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero, 
    step = 1020/step;              //   divide by 1020
  } 
  return step;
}



int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    } 
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    } 
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  } 
  else if (val < 0) {
    val = 0;
  }
  return val;
}

void crossFade(int color[3]) {
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G); 
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);      
    analogWrite(bluPin, bluVal); 

    delay(wait); // Pause for 'wait' milliseconds before resuming the loop

    if (DEBUG) { // If we want serial output, print it at the 
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGB: #");
        Serial.print(i);
        Serial.print(" | ");
        Serial.print(redVal);
        Serial.print(" / ");
        Serial.print(grnVal);
        Serial.print(" / ");  
        Serial.println(bluVal); 
      } 
      DEBUG += 1;
    }
  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

I dont know how to build in a button

Leave the buttons sewn onto the shirt. They won't work worth a damned with the Arduino. Use a switch, instead.

What do you suppose the digitalRead() function does?

which I can use to change the speed of the fade.

HOW do you want to use the fact that a switch is, or is not, pressed, to change the speed of the fade? Each time the switch is noticed to be pressed, the speed should randomly go up or down some amount between 0 and 4567237278452158?

I also need to make the fade go away

What the heck does that mean?

PaulS:
Leave the buttons sewn onto the shirt. They won't work worth a damned with the Arduino. Use a switch, instead.

What do you suppose the digitalRead() function does?
HOW do you want to use the fact that a switch is, or is not, pressed, to change the speed of the fade? Each time the switch is noticed to be pressed, the speed should randomly go up or down some amount between 0 and 4567237278452158?
What the heck does that mean?

The problem is that I need to use a button, switch is not allowed. Also the button need to speed up the fading proces with 100ms with every press.

With your last quote I ment that the rgb led wil completely fade out before starting a new fade with another color.

The problem is that I need to use a button, switch is not allowed.

Bullshit. A plastic shirt button is NOT usable on an Arduino.

If you mean a push-button switch, the style of the switch matters not a whit.

Also the button need to speed up the fading proces with 100ms with every press.

So, look at the state change detection example to determine when the switch becomes pressed. When it does, increment or decrement some value. I can't imagine that adding 100 to some value used as the interval between changes is going to speed up anything. YMMV.

I had a look at the context of the project. What they mean is: The duration of the fading proces is linked to the time a button is pressed. for example, If the button is pressed for 2 seconds then the fading proces takes 2 seconds.

This is the code I currently have

/*
  Fading

  This example shows how to fade an LED using the analogWrite() function.

  The circuit:
  - LED attached from digital pin 9 to ground.

  created 1 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Fading
*/

int ledR = 9;    // LED connected to digital pin 9
int ledG = 10;
int ledB = 11; 
void setup() {

}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(9, fadeValue);
  
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite( 9, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
    // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(10, fadeValue);
  
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite( 10, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(11, fadeValue);
  
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite( 11, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

Look at the state change examples that come with the IDE.

Currently you have a fixed delay of 30 ms. Store that delay value in a variable and use that variable in the delay function.

Next use the detection of a state change to change the value stored in the above mentioned variable.

That will allow you to change the feeding speed and can be a first step to a solution of your project.