Controlling 5 LEDs, Want to Turn Off Entire Circuit

Hello All,

New to electronics hobbying, so I apologize for the noob question. I've only recently started adding Arduino to my projects.

The project - controlling 5 LEDs through the serial monitor. I have it completely working. If I enter any number between 0 through 5, the corresponding number of LEDs lights up. I have been trying to add a line of code that turns off all LEDs if the value is not 0 through 5. This is where I hit a wall.

For instance, if I hit the number 6 or the letter F, I want all LEDs to turn off.

Here is my current code (the last five lines is where I run into my problem):

const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 12;
const int ledPin5 = 13;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);

}

void loop()

{

int value;

if(Serial.available())

{

delay(50);

while(Serial.available() >0)

{

value=Serial.read();

if(value=='5'){digitalWrite(13,HIGH);}
if(value=='5'){digitalWrite(12,HIGH);}
if(value=='5'){digitalWrite(11,HIGH);}
if(value=='5'){digitalWrite(10,HIGH);}
if(value=='5'){digitalWrite(9,HIGH);}

if(value=='4'){digitalWrite(13,HIGH);}
if(value=='4'){digitalWrite(12,HIGH);}
if(value=='4'){digitalWrite(11,HIGH);}
if(value=='4'){digitalWrite(10,HIGH);}
if(value=='4'){digitalWrite(9,LOW);}

if(value=='3'){digitalWrite(13,HIGH);}
if(value=='3'){digitalWrite(12,HIGH);}
if(value=='3'){digitalWrite(11,HIGH);}
if(value=='3'){digitalWrite(10,LOW);}
if(value=='3'){digitalWrite(9,LOW);}

if(value=='2'){digitalWrite(13,HIGH);}
if(value=='2'){digitalWrite(12,HIGH);}
if(value=='2'){digitalWrite(11,LOW);}
if(value=='2'){digitalWrite(10,LOW);}
if(value=='2'){digitalWrite(9,LOW);}

if(value=='1'){digitalWrite(13,HIGH);}
if(value=='1'){digitalWrite(12,LOW);}
if(value=='1'){digitalWrite(11,LOW);}
if(value=='1'){digitalWrite(10,LOW);}
if(value=='1'){digitalWrite(9,LOW);}

if(value=='0'){digitalWrite(13, LOW);}
if(value=='0'){digitalWrite(12,LOW);}
if(value=='0'){digitalWrite(11,LOW);}
if(value=='0'){digitalWrite(10,LOW);}
if(value=='0'){digitalWrite(9,LOW);}

//if else (value==''){digitalWrite(13, LOW);} // Part I can't figure out
//if else (valie==''){digitalWrite(12,LOW);}
//if else (value==''){digitalWrite(11,LOW);}
//if else (value==''){digitalWrite(10,LOW);}
//if else (value==''){digitalWrite(9,LOW);}

Any help is appreciated!

you could use Switch Case https://www.arduino.cc/en/Tutorial/SwitchCase and put the “all off” into the default

matthew247:
Hello All,

New to electronics hobbying, so I apologize for the noob question. I've only recently started adding Arduino to my projects.

The project - controlling 5 LEDs through the serial monitor. I have it completely working. If I enter any number between 0 through 5, the corresponding number of LEDs lights up. I have been trying to add a line of code that turns off all LEDs if the value is not 0 through 5. This is where I hit a wall.

For instance, if I hit the number 6 or the letter F, I want all LEDs to turn off.

Here is my current code (the last five lines is where I run into my problem):

const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 12;
const int ledPin5 = 13;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);

}

void loop()

{

int value;

if(Serial.available())

{

delay(50);

while(Serial.available() >0)

{

value=Serial.read();

if(value=='5'){digitalWrite(13,HIGH);} if( value == '5' ) {
if(value=='5'){digitalWrite(12,HIGH);} .// put in your 5 digitalWrite()s
if(value=='5'){digitalWrite(11,HIGH);} }
if(value=='5'){digitalWrite(10,HIGH);}
if(value=='5'){digitalWrite(9,HIGH);}

if(value=='4'){digitalWrite(13,HIGH);} else if( value == '4' ) {
if(value=='4'){digitalWrite(12,HIGH);} .// put in your 5 digitalWrite()s
if(value=='4'){digitalWrite(11,HIGH);} }
if(value=='4'){digitalWrite(10,HIGH);}
if(value=='4'){digitalWrite(9,LOW);}

if(value=='3'){digitalWrite(13,HIGH);} else if( //..... repeat for each following group
if(value=='3'){digitalWrite(12,HIGH);}
if(value=='3'){digitalWrite(11,HIGH);}
if(value=='3'){digitalWrite(10,LOW);}
if(value=='3'){digitalWrite(9,LOW);}

if(value=='2'){digitalWrite(13,HIGH);}
if(value=='2'){digitalWrite(12,HIGH);}
if(value=='2'){digitalWrite(11,LOW);}
if(value=='2'){digitalWrite(10,LOW);}
if(value=='2'){digitalWrite(9,LOW);}

if(value=='1'){digitalWrite(13,HIGH);}
if(value=='1'){digitalWrite(12,LOW);}
if(value=='1'){digitalWrite(11,LOW);}
if(value=='1'){digitalWrite(10,LOW);}
if(value=='1'){digitalWrite(9,LOW);}

if(value=='0'){digitalWrite(13, LOW);}
if(value=='0'){digitalWrite(12,LOW);}
if(value=='0'){digitalWrite(11,LOW);}
if(value=='0'){digitalWrite(10,LOW);}
if(value=='0'){digitalWrite(9,LOW);}

else { digitalWrites( )s to turn off } // add this
// pay attention to all of the '{" and '}'

//if else (value==''){digitalWrite(13, LOW);} // Part I can't figure out
//if else (valie==''){digitalWrite(12,LOW);}
//if else (value==''){digitalWrite(11,LOW);}
//if else (value==''){digitalWrite(10,LOW);}
//if else (value==''){digitalWrite(9,LOW);}

Any help is appreciated!

That doesn't seem to work.

OK, let's cut to the chase.

Please go and read the instructions, then go back and modify your post using the "More --> Modify" option you will find to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as code, not as a quotation so we can examine it conveniently and accurately. Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, whatever code you do post could well be garbled and is certainly anything but easy to read. Yours seems to have survived so far but I didn't look too closely.

Note: Also mark up any data - as a separate section - in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code. You can then use the "copy for forum" function in order to paste it into a message with the "code" tags already provided.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.

matthew247:
That doesn't seem to work.

How about showing what you did.

Hi boolrules,

Here is how I changed the code as per your instruction:

if(value=='5'){digitalWrite(13,HIGH);}
if(value=='5'){digitalWrite(12,HIGH);}
if(value=='5'){digitalWrite(11,HIGH);}
if(value=='5'){digitalWrite(10,HIGH);}
if(value=='5'){digitalWrite(9,HIGH);}

else if(value=='4'){digitalWrite(13,HIGH);}
else if(value=='4'){digitalWrite(12,HIGH);}
else if(value=='4'){digitalWrite(11,HIGH);}
else if(value=='4'){digitalWrite(10,HIGH);}
else if(value=='4'){digitalWrite(9,LOW);}

else if(value=='3'){digitalWrite(13,HIGH);}
else if(value=='3'){digitalWrite(12,HIGH);}
else if(value=='3'){digitalWrite(11,HIGH);}
else if(value=='3'){digitalWrite(10,LOW);}
else if(value=='3'){digitalWrite(9,LOW);}

else if(value=='2'){digitalWrite(13,HIGH);}
else if(value=='2'){digitalWrite(12,HIGH);}
else if(value=='2'){digitalWrite(11,LOW);}
else if(value=='2'){digitalWrite(10,LOW);}
else if(value=='2'){digitalWrite(9,LOW);}

else if(value=='1'){digitalWrite(13,HIGH);}
else if(value=='1'){digitalWrite(12,LOW);}
else if(value=='1'){digitalWrite(11,LOW);}
else if(value=='1'){digitalWrite(10,LOW);}
else if(value=='1'){digitalWrite(9,LOW);}

else if(value=='0'){digitalWrite(13, LOW);}
else if(value=='0'){digitalWrite(12,LOW);}
else if(value=='0'){digitalWrite(11,LOW);}
else if(value=='0'){digitalWrite(10,LOW);}
else if(value=='0'){digitalWrite(9,LOW);}

else if (value){digitalWrite();}

matthew247:
Hi boolrules,

Here is how I changed the code as per your instruction:

Not quite:

if( value == '5' ) {
digitalWrite( 13, HIGH ); // the { } encloses a "block"
digitalWrite( 12, HIGH ); // everything in the block will be
digitalWrite( 11, HIGH ); // executed if the if( ) is true.
digitalWrite( 10, HIGH );
digitalWrite( 9, HIGH );
}

else if( value == '4' ) { // "else" means "otherwise"
digitalWrite( 13, HIGH ); // if the first if( ) is not true, this one will be tested
digitalWrite( 12, HIGH ); // and so on down the chain of "else if"s
digitalWrite( 11, HIGH );
digitalWrite( 10, HIGH );
digitalWrite( 9, LOW );
}

else if(value=='3'){digitalWrite(13,HIGH);} // fix the rest as above like for the value '4'
else if(value=='3'){digitalWrite(12,HIGH);}
else if(value=='3'){digitalWrite(11,HIGH);}
else if(value=='3'){digitalWrite(10,LOW);}
else if(value=='3'){digitalWrite(9,LOW);}

else if(value=='2'){digitalWrite(13,HIGH);}
else if(value=='2'){digitalWrite(12,HIGH);}
else if(value=='2'){digitalWrite(11,LOW);}
else if(value=='2'){digitalWrite(10,LOW);}
else if(value=='2'){digitalWrite(9,LOW);}

else if(value=='1'){digitalWrite(13,HIGH);}
else if(value=='1'){digitalWrite(12,LOW);}
else if(value=='1'){digitalWrite(11,LOW);}
else if(value=='1'){digitalWrite(10,LOW);}
else if(value=='1'){digitalWrite(9,LOW);}

else { // If none of the other tests were true
digitalWrite(13, LOW);} // this "else" will be executed.
digitalWrite(12,LOW);} // No test is necessary
digitalWrite(11,LOW);}
digitalWrite(10,LOW);}
digitalWrite(9,LOW);}
}

//else if (value){digitalWrite();} // I don't know what this is supposed to be

Quoting manually cause you spooged code all over the place.. “have been trying to add a line of code that turns off all LEDs if the value is not 0 through.”

But 0 turns off your LEDs right?

If you want more characters to do the same thing as zero then simply add in some if line to change the data to “0” if it was greater than 5...

If you want to send “F” via serial input you need to assign “value” as a string, not a Int..

Hello All,

That was a huge help! Thank you very much. Again, being a noob I appreciate the help and I will properly format my questions in the future on the forum.

Sincerely,
Matt

Why not start with this one?