Serial Inputs and button to prevent them.

I am trying to write a sketch that will take serial inputs to illuminate LEDs and a hardware switch to block the serial inputs while the switch is closed/HIGH. I seem to have the first part working well. I can increase and decrease the number of LEDs illuminated with Serial commands. The second part is giving me fits. I have tried "while" statements and "if" statements to examine the condition of the "toggle". It works in that it stops any additional serial commands from illuminating LEDs but when the "toggle" is switched back to "LOW" the LEDs light up with any Serial commands that I tried while the "toggle" was HIGH. As if my serial commands were stored, just waiting for the digital inputs from the toggle to go LOW and then queuing out to the LEDs one at a time.
I thought if I could stop the void loop it would stop the Serial.read and that would prevent any additional serial commands from coming through to the arduinoUNO.
Help
Oh yeah
If you hadn't figured it out already
I am new
see attached sketch

5_led_serial_control.ino (2.22 KB)

I can't see your code on my device. I (and many others) could see your code if you post the code as described in the how to use this forum-please read stickies.

Ooops sorry

Here is my code

char q;
int count = 0;

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int toggle = 8;
void setup() {
// put your setup code here, to run once:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(toggle, INPUT);
Serial.begin(9600);

}

void loop() {

if (digitalRead(toggle) == HIGH)return;
// while (digitalRead(toggle) == HIGH){
//Serial.println(" system disabled ");
//delay(1500);
// }

if (Serial.available() > 0) {
q = Serial.read();
if ((q == 'a') && (count < 5)) {
Serial.println(" Yay Add 1");
count++;
Serial.println(count);
} else if (q == 's') {
Serial.println(" Yay Subtract 1");
count = count - 1;
Serial.println(count);
} else if (q == 'z') {
Serial.println(" zero");
count = 0;
Serial.println(count);
}
}
Serial.println(count);
delay(1500);
switch (count) {
case 0:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
break;
case 1:
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
break;

case 2:
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
break;

case 3:
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
break;

case 4:
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
break;

case 5:
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
break;
default:
break;

}
}
// put your main code here, to run repeatedly:

groundFungus:
I (and many others) could see your code if you post the code as described in the how to use this forum-please read stickies.

Part of what is meant by that is your code is enclosed in code tags. So bits of it aren't converted to smilies.

It works in that it stops any additional serial commands from illuminating LEDs but when the "toggle" is switched back to "LOW" the LEDs light up with any Serial commands that I tried while the "toggle" was HIGH. As if my serial commands were stored, just waiting for the digital inputs from the toggle to go LOW and then queuing out to the LEDs one at a time.

The serial commands being stored, and then queued out to the LEDs one at a time, is actually exactly what is happening.

Serial data is stored in a buffer, and waits there for you to read it out. The serial.available command tells you how many characters are in the buffer, so when you check for serial.available > 0 you are checking to see if there are any characters in the buffer, then when there are you read them out. When you turn the toggle switch on, you are no longer reading characters out of the buffer, so they just accumulate there until the toggle switch is turned off, at which time the characters are read out one at a time each time you go through the loop.

Since you want to ignore any commands send while the toggle switch is on, you need to continue reading the serial input, but don't act upon it. Just read the character and skip over the if statements that alter the count variable.