Can someone help me figure out why the leds do not turn off when the if statement is false? the leds turn on when in the range but do not turn off when not in the range. this is my first time posting hope this is the best way to get help.
here is my code
const byte blueLED= 3; //set blue led to pin 3
const byte whiteLED= 5; // set white led to pin 5
const byte redLED= 6; // set red led to pin 6
const byte potPin= A1; // set potentiometer to pin A1
void setup() {
Serial.begin(9600); //start serial communication
pinMode(blueLED, OUTPUT); //set blue led as an output
pinMode(whiteLED, OUTPUT); //set white led as an output
pinMode(redLED, OUTPUT); //set red led as an output
pinMode(potPin, INPUT); //set potentiometer as an input
}
void loop() {
int rValue= analogRead(potPin); //read potentiometer and set the value = to rValue
Serial.print("Potentiometer Value "); //print to serial
Serial.println(rValue); //print rvalue to serial
if(rValue <= 255){ //check to see if rvalue is less than or equal to 255 if true turn on blue led
digitalWrite(blueLED, HIGH);
}else if(rValue >= 720){ //check to see if rvalue is grater than or equal to 720 if true turn on white led
digitalWrite(whiteLED, HIGH);
}else if ((rValue < 720) && (rValue >255)){ //check to see if rvalue is between 720 and 255 if true turn on red led
digitalWrite(redLED,HIGH);
}else{ //turn of led if statement is false
digitalWrite(blueLED, LOW);
digitalWrite(whiteLED,LOW);
digitalWrite(redLED, LOW);
}
There is no way that the else (that turns all LEDs off) can ever execute because all possible cases are covered in the if-if else statements. You should turn off the LEDs that you want off as well as turn the one on in each if or if else. The way that it is, if an LED is turned on there is nothing to turn it off.
Thanks for the help that makes sense on why they would not shut off then. how would one write the code for making 2 leds overlap in a range to have 2 on like set the blue led to 355 in the first if statement but have the red led still go off after the range is not true below 255. seeing you will have to remove the write low to the red led form the blue led if statement to get it to come on. would the best way be writing another else if statement to say turn the led off led if in the of range?
Simply switch off what needs to be switched off if a condition is true and switch on what needs to be switched on if the condition is true.
if (condition1)
{
// switch off what needs to be off
...
// switch on what needs to be on
...
}
else if (condition2
{
// switch off what needs to be off
...
// switch on what needs to be on
...
}
else if (condition3)
{
// switch off what needs to be off
...
// switch on what needs to be on
...
}
else
{
// switch all off
...
}
Replace conditionX by the conditions like 'rValue <= 255'.
const byte blueLED= 3; //set blue led to pin 3
const byte whiteLED= 5; // set white led to pin 5
const byte redLED= 6; // set red led to pin 6
const byte potPin= A1; // set potentiometer to pin A1
void setup() {
Serial.begin(9600); //start serial communication
pinMode(blueLED, OUTPUT); //set blue led as an output
pinMode(whiteLED, OUTPUT); //set white led as an output
pinMode(redLED, OUTPUT); //set red led as an output
pinMode(potPin, INPUT); //set potentiometer as an input
}
void loop() {
int rValue= analogRead(potPin); //read potentiometer and set the value = to rValue
Serial.print("Potentiometer Value "); //print to serial
Serial.println(rValue); //print rvalue to serial
if (rValue <= 255){ // If rValue <= 255 turn on only blue
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(whiteLED, LOW);
} else if(rValue <= 355) { // if rValue is between 256 and 355, turn on both red and blue
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(whiteLED, LOW);
} else if (rValue <= 720){ // if rValue is between 356 and 720, turn on only red
digitalWrite(blueLED, LOW);
digitalWrite(redLED,HIGH);
digitalWrite(whiteLED, LOW);
} else{ // must be > 720. Turn on only white
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(whiteLED,HIGH);
}
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Another way that works surprisingly well is to have a line that turns all the LEDs off before you start the IF statements. The correct ones will be turned on again so quickly that the human eye won't notice.