Help with RGBs that fade into different colors as bicycle speed increases

Hey all! I have been trying to write a function with the goal of having an RGB LED that fades from color to color as bicycle speed changes.

I divided the fades into increments of 10 MPH. My plan was: For 0-10 MPH fade from blue to green. from 11-20 MPH fade green to orange and from 21-30 fade orange to red.

I tried to make a variable in the RGB values that corresponds with current MPH. Those variables are colorFadeUp for increasing values and colorFadeDown for decreasing values.

I think the problem is that the readings of the variables colorFadeUp and colorFadeDown multiplies by an increasingly large MPH value as MPH increases which creates an RGB value greater than or less than 255.

What I need help with:
I need a way to correspond RGB values to MPH readings so each MPH creates a certain RGB value.

I am using a reed switch and magnet to measure wheel revolution speed. I have tested the MPH readings and they are accurate.

Any help would be much appreciated! :slight_smile:

int MPH;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;


int colorFadeDown;                      
int colorFadeUp;


//choose RGB value for RGB LED

void setColor (int red, int green, int blue) {


  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

}

void colorSpeed() {

  colorFadeDown = 255 - (MPH * (255 / 10));  // The intention is to fade from 255 to 0
  colorFadeUp =  0 + (MPH * (255 / 10));       // The intention is to fade from 0 to 255

 if (MPH == 0) {       //at 0 MPH LED is blue

 setColor(0,0,255);

}
  
  else if (MPH > 0 && MPH <= 10 ) {       //1-10 MPH green values increase and blues decrease

    setColor(0, colorFadeUp, colorFadeDown);                 

  } 
  else if (MPH > 10 && MPH <= 20 ) {    // 11 - 20 MPH red values increase and green stays 255

    setColor(colorFadeUp , 255 , 0);               

  else if (MPH  > 20 && MPH <=30  {     //21 - 30 MPH green values decrease an red value stays 255
 
    setColor(255, colorFadeDown, 0);

  } else if (MPH >30) {                         //red if  MPH > 30

   setColor(255,0,0);

}
  


}
 if (MPH == 0) {       //at 0 MPH LED is blue

 setColor(0,0,255);

}
 
  else if (MPH > 0 && MPH <= 10 ) {

If MPH is not 0, is there any way that it can be negative? If not, then it must be greater than 0, so there is no need to test that it is positive.

The same holds true for the rest of your else if statements.

Where is MPH set?