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++;
}