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!

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, ....