Getting RGB LEDs to light based on switch position

Hi there everyone, hopefully someone might be able to give me a hand with my code, I have stared at it for hours, tried a few variations but can't manage to get it right. I'm trying to get some RGB LEDs to light in a specific way based on a switch position and a test with an if statement.

Initially I want the LEDs green before the main body of loop() is run and once a switch is triggered the LEDs should change to blue signifying that the program has been activated. The loop then tests for a condition and if met the LEDs change to red until the condition no longer exists and then go back to blue until either the condition is met again whereby the LEDs once again change to red or the switch is turned off and the LEDs return to green.

The problem I have is that once the switch is turned on the blue LEDs do not turn off until the switch is turned off again and so, instead of getting red LEDs when the condition in the if statement is true, I get a pink LEDs.

The program and circuit are to test the RPM of a shaft, calculate the speed and then trigger a couple of relays once the drive speed reached 20KPH. The LEDs are to display the state of the program and the relays.

This is the code below, everything works with the exception of the blue LED state, I'm not sure if I have a fundamental coding problem, or the if I am calling digitalWrite(blue,LOW/HIGH) in the wrong places.

This is my first foray into arduino coding and have used code from a number of different people's examples and really appreciate their efforts!

I have included a pic of the circuit

//volatile byte revolutions;
float revolutions;
float rpmilli;
float speed;
int relayPin1 = 8;
int relayPin2 = 9;
int red = 4; //redLED
int green = 5; //greenLED
int blue = 6; //BlueLED
int on = 11; //switch
unsigned long timeold;

void setup()
{
  pinMode(on, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);

  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, RISING);

  revolutions = 0;
  rpmilli = 0;
  timeold = 0;
}

void loop()
{
  digitalWrite(green, HIGH); // with switch off, green LEDs on show circuit is ready
  digitalWrite(blue,LOW); // ensure blue LEDs are off
  
  while (digitalRead(on) == HIGH){ //switch turned on
    digitalWrite(green, LOW); //turn off the green LED 
    digitalWrite(blue, HIGH); //turn on the blue LED to show program running
  
  if (revolutions >= 4) { //number of magnets

    // calculate the revolutions per milli(second)
    rpmilli = revolutions/(millis()-timeold);
    timeold = millis();

    revolutions = 0;

    speed = rpmilli * 0.848229 * 900; //1 magnet 3600, 2 magnets 1800, 4 magnets 900

    Serial.print("RPM:");
    Serial.print(rpmilli * 60000,DEC);
    Serial.print(" Speed:");
    Serial.print(speed,DEC);
    Serial.println(" kph");
        
  if (speed > 20) {
    digitalWrite(relayPin1, HIGH); // trigger relay 1
    digitalWrite(relayPin2, HIGH); // trigger relay 2
    digitalWrite(red, HIGH); // turn on red LEDs
    digitalWrite(blue, LOW); // turn off blue LEDs
  }
  
  else{
    digitalWrite(relayPin1, LOW); // relay 1 open
    digitalWrite(relayPin2, LOW); // relay 2 open
    digitalWrite(red, LOW); //turn off red LEDs
    digitalWrite(blue, HIGH); //turn on blue LEDs
  }
}
}
}

void rpm_fun()
{
  revolutions++;
}

I'm trying to get some RGB LEDs to light in a specific way based on a switch position and a test with an if statement.

What does that have to do with revolutions, speed, etc.?

You know that loop() is called over and over, right? Turning the greed LED on, and the blue LED off, on every pass through loop() is hardly a good idea.

What does that have to do with revolutions, speed, etc.?

The LEDs change to red once a certain speed is reached

You know that loop() is called over and over, right? Turning the greed LED on, and the blue LED off, on every pass through loop() is hardly a good idea.

Any chance you could suggest a better alternative to ensure the LEDs are the right colour based on the switch position?

PaulS is encouraging you to think ;), or point to a likely source of the problem.

The first thing happening in loop() is that you digitalWrite() to both LEDs . That is many times each millisecond that will happen - regardless of what the rest of the code does. Your other code is carefully put inside if ( ) blocks, so LED/relay will only happen when a condition is met. So, ....

Any chance you could suggest a better alternative to ensure the LEDs are the right colour based on the switch position?

Yes. Make up your mind whether the LED color is a function of the switch position or the speed.

ahhh, I think I get it...thanks for the push in the right direction, funny how easily one can confuse yourself huh?