Infinitely running while loop within switch/case statements?

Howdy,

I’m a fairly new user to arduino and working on a project and could use a little guidance on how to fix an issue I’m having with my code.

Essentially, I’m trying to develop system in which I have outputs from pins 2-9 with 8 LEDS that when a particular command is sent through the serial it will turn on the particular LED corresponding to its pin number, I’ve accomplished this using the switch/case statement and PORTD/B commands so far. As well I’m trying to implement a while loop as it’s own individual case statement when the particular command is sent through the serial it will loop from pins 2-9 in sequence pausing between pins every second and repeat this loop indefinitely, while as well being able to break this loop at any time and switch to the individual pins and back to the loop when necessary.

I’ve attempted to implement this while loop as it’s own statement, but the issue I seem to be coming across is that the loop will not run unless it is set as a default case statement and as well the loop will run not with the command I send to it, but rather it will loop a number of times for number of characters that is sent to it.

If anyone could give some advice to this, or any general suggestions to improve my code, I would be greatly appreciative.
Here’s my code:

int incomingByte; // a variable to read incoming serial data into
int timer = 100;
int Pins[]={2,3,4,5,6,7,8,9};

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  DDRD = 0xff; // registers ports 0-7 as outputs
  DDRB = 0xff; // registers ports 8-13 as outputs
  {
//  for (int i=0;  i < 8; i++)
//  {
//    pinMode(Pins[i],OUTPUT);
//  }
//  for (int thisPin = 2; thisPin <=9 ; thisPin++){
//    pinMode(thisPin, OUTPUT);
  }
}


void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
//    Serial.write(incomingByte);
    switch(incomingByte) {
        case '0':
          PORTD = 0b00000000; // sets all outputs for pins 0-7 to LOW
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '1':
          PORTD = 0b00000100; // sets output for pin 2 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '2':
          PORTD = 0b00001000; // sets output for pin 3 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '3':
          PORTD = 0b00010000; // sets output for pin 4 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '4':
          PORTD = 0b00100000; // sets output for pin 5 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '5':
          PORTD = 0b01000000; // sets output for pin 6 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '6':
          PORTD = 0b10000000; // sets output for pin 7 to HIGH
          PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
         break;
        case '7':
          PORTB = 0b00000001; // sets output for pin 8 to HIGH
          PORTD = 0b00000000; // sets all outputs for pints 0-7 to LOW
         break;
        case '8':
          PORTB = 0b00000010; // sets output for pin 9 to HIGH
          PORTD = 0b00000000; // sets all outputs for pints 0-7 to LOW
         break;
     default:
        case 'loop':
         PORTD = 0b00000000; // sets all outputs for pins 0-7 to LOW
         PORTB = 0b00000000; // sets all outputs for pints 8-13 to LOW
 {
  int i = 0;
  while (i < 8)
  {
    {
      digitalWrite(Pins[i],HIGH);
      delay(timer);
      digitalWrite(Pins[i],LOW);
      i++;
    }
  }
           
//          for (int thisPin = 2; thisPin <= 9; thisPin++) { 
//            // turn the pin on:
//              digitalWrite(thisPin, HIGH);   
//              delay(timer);                  
//              // turn the pin off:
//              digitalWrite(thisPin, LOW);  
//              
        }
      }
    }
  }
    int incomingByte = Serial.read();

You sure that shouldn’t be byte incomingInt? Or char incomingInt? Or char incomingFloat?

If there is ANY reason to use a type in a name, the type in the name should match the type of the named object.

    switch(incomingByte) {

So, whatever character was read from the serial port defines what to do. Makes sense.

        case 'loop':

Which ONE key to you press to get the ONE character in the single quotes?

int i = 0;
  while (i < 8)

Got a for loop allergy?