pulse fading LED HELP

Ok, so I've been trying to create a program and I've been having so much difficulty..
My goal is to have an RGB led that fades back and forth in intensity with a color, but when a switch is turned on, it will slowly fade out of its current color and into a new one that fades in and out. I need this to work both ways. I dont want a sharp transitions into colors.

Ive gotten as far as having two different intensities of colors when a switch is pulled (I'll figure out what colors I want later, for now I'm trying to get the fade right).

//LED colors
int bluePin=3;
int greenPin=5;
int redPin=6;

//Fade effect values
int brightGreen=100;
int brightRed=100;
int brightBlue;
int fade=1; // Fade rate per cycle

//Magnet prox(Switch) trigger values
int magSwitch=9;
int trigger;

void setup() {

pinMode(bluePin,OUTPUT);
pinMode(redPin,OUTPUT); //LED pinModes
pinMode(greenPin,OUTPUT);

pinMode(magSwitch,INPUT); //Switch pinModes
digitalWrite(magSwitch,HIGH);
}

void loop() {

trigger=digitalRead(magSwitch);
brightBlue=brightBlue-fade;

if(trigger==1){
analogWrite(bluePin,brightBlue);
while(brightBlue<20){
brightBlue=brightBlue+1;
}
if(brightBlue<=20||brightBlue>=250){
fade=-fade;
}
delay(10);
}
if(trigger==0){
analogWrite(bluePin,brightBlue);
while(brightBlue>20){
brightBlue=brightBlue-1;
}
if(brightBlue<=0||brightBlue>=20){
fade=-fade;
}
delay(40);
}
}

This is what I have so far. The LED will fade back and forth through 20-250, and when the switch is toggled it will fade between 0-20. However, the thing I dont want is, when the switch is toggled, the loop will reset the brightBlue variable, causing it to change color sharply. So if the LED is at 200 when the switch is flipped, it will jump down to something like 50, and then slowly go down into the 0-20 range.

I feel like if I could redefine brightBlue as the loop goes on, then it would work. What do you guys think?
Ive been working on this for 3 days now; I need help!!

This while loop is not really needed because if brightBlue reaches 20 you will negate fade and brightBlue will start incrementing back up anyhow.

    while (brightBlue < 20) {
      brightBlue = brightBlue + 1;
    }

Also, I would replace:

  pinMode(magSwitch, INPUT);    //Switch pinModes
  digitalWrite(magSwitch, HIGH);

with:

  pinMode(magSwitch, INPUT_PULLUP);    //Switch pinModes

I will look at your code closer when I have more time.

Here is optimized code I think will solve your problem. It compiles but I can't test it. The idea is that you only change the sign of fade if you need to.

//LED colors
const int bluePin = 3;
const int greenPin = 5;
const int redPin = 6;
//Magnet prox(Switch) 
const int magSwitch = 9;

//Fade effect values
int brightGreen = 100;
int brightRed = 100;
int brightBlue = 0;
int fade = 1; // Fade rate per cycle

int trigger;

void setup() {
  pinMode(bluePin, OUTPUT);
  pinMode(redPin, OUTPUT);    //LED pinModes
  pinMode(greenPin, OUTPUT);
  pinMode(magSwitch, INPUT_PULLUP);    //Switch pinModes
}

void loop() {
  trigger = digitalRead(magSwitch);
  brightBlue = brightBlue - fade;
  analogWrite(bluePin, brightBlue);

  if (trigger) {
    if (brightBlue <= 20 && fade < 0
    ||  brightBlue >= 250 && fade > 0) {
      fade = -fade;
    }
    delay(10);
  }
  else {
    if (brightBlue <= 0 && fade < 0
    ||  brightBlue >= 20 && fade > 0) {
      fade = -fade;
    }
    delay(40);
  }
}

ToddL1962:
This while loop is not really needed because if brightBlue reaches 20 you will negate fade and brightBlue will start incrementing back up anyhow.

    while (brightBlue < 20) {

brightBlue = brightBlue + 1;
   }




Also, I would replace:



pinMode(magSwitch, INPUT);    //Switch pinModes
 digitalWrite(magSwitch, HIGH);




with:



pinMode(magSwitch, INPUT_PULLUP);    //Switch pinModes




I will look at your code closer when I have more time.

The reason I have that "while" statement is because if the switch was pulled while the LED was lower than 20, the next "if" statement switches the fade from negative to positive and vise versa when the LED is <= 20, causing it to alternate between the two indefinitely.
By waiting until I bring the LED up to 20 before switching the fade takes affect, I solve that issue.

I finally managed to get the effect I was looking for. I just had to put the a delay in the "while" statement, along with another analogWrite(bluePin, BrightBlue);. Now it fades the way I want it to when I pull the switch. I applied the change to the switch from INPUT to INPUT_PULLUP and worked perfectly!

Now the problem is the color change... I seem to have driven myself into a corner without thinking about it...

Im wanting the color with the switch off to be a deep blue, basically (0, 0, 255) and fade in and out brightness at a slow pace.
Once the switch is flipped, transition smoothly into a flame red, like (255, 30, 0) and fade in and out a bit faster than the blue.

Problem is, the code I'm using would allow me to set the delay for the fade rate, and the intensity of the color for each fade. It will transition smoothly between them, but only for the one color. I cant get it to work with two. If I simply use the same code for each RGB pin, then they will periodically go out of sync and not give me the color I want for each setting....

I'm so frustrated..Heres the code I currently have

//LED colors
int bluePin=3;
int greenPin=5;
int redPin=6;

//Fade effect values
int brightGreen=1;
int brightRed=1;
int brightBlue=1;
int fade=1; // Fade rate per cycle
int fade2=1;

//Magnet prox(Switch) trigger values
int magSwitch=9;
int trigger;

void setup() {

pinMode(bluePin,OUTPUT);
pinMode(redPin,OUTPUT); //LED pinModes
pinMode(greenPin,OUTPUT);

pinMode(magSwitch,INPUT_PULLUP); //Switch pinModes
}

void loop() {

trigger=digitalRead(magSwitch);

if(trigger==1){
analogWrite(bluePin,brightBlue);

while(brightBlue<20){
analogWrite(bluePin,brightBlue);
brightBlue=brightBlue+1;
delay(10);
}

brightBlue=brightBlue+fade;
if(brightBlue<=20||brightBlue>=250){
fade=-fade;
}
delay(10);
}

if(trigger==0){
analogWrite(bluePin,brightBlue);

while(brightBlue>20){
analogWrite(bluePin,brightBlue);
brightBlue=brightBlue-1;
delay(10);
}

brightBlue=brightBlue-fade;
if(brightBlue<=0||brightBlue>=20){
fade=-fade;
}
delay(40);
}
}

If you will use the code that I presented and duplicate it with a separate fade value for each color I believe you will achieve the effect you want. That way you could dispense with the while loops and uncomplicate your code.

If you want a more deliberate transition then simply save the state of the last trigger value and compare the trigger value each loop. When you see that you are transitioning you can execute specific code for the transition. Sort of like this:

void loop() {
  
  // If the fade mode is changing execute transition code
  if (trigger != last_trigger)
  {
    //
    // This code only executes when switch position has changed
    //
    if (trigger)
    {
      // TBD: put code here to process transition from "0-20" fade mode to "20-250" fade mode
    }
    else
    {
      // TBD: put code here to process transition from "20-250" fade mode to "0-20" fade mode
    }
  }

  if (trigger)
  {
    // TBD: process "20-250" fade mode
  }
  else
  {
    // TBD: process "0-20" fade mode
  }
  
  last_trigger = trigger;
}