0

I need a way to rewrite the switch case part in a more efficient way.

int y , x ;
byte dataArray[6];

void setup () {
   pinMode(8,OUTPUT);  
   pinMode(11,OUTPUT);
   pinMode(12,OUTPUT);

   Serial.begin(9600);
   dataArray[0] = 0x3 ; //00000011
   dataArray[1] = 0x5 ; //00000101
   dataArray[2] = 0x9 ; //00001001
   dataArray[3] = 0x11; //00010001
   dataArray[4] = 0x21; //00100001
   dataArray[5] = 0x41; //01000001
}

void loop () {
   while (Serial.available()>0) {
     y = Serial.read();
     x = Serial.read();
     switch ( x ) {
     case 1:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW);
     break;

     case 2:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW); 
     break;

     case 3:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW); 
     break;

      case 4:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW); 
     break;

      case 5:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW); 
     break;

      case 6:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,dataArray[y]);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW); 
     break;

     default:
        digitalWrite(8,LOW);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        shiftOut(11,12,LSBFIRST,0);
        digitalWrite(8,HIGH);
        delay(10);
        digitalWrite(8,LOW);
     break;
     }
   }
 }
2
  • 2
    This question appears to be off-topic because it is a programming question.
    – Matt Young
    Commented Sep 9, 2014 at 17:59
  • @MattYoung you are right i didn't take this in mind sorry
    – Abdelrahman Tarief
    Commented Sep 9, 2014 at 18:07

1 Answer 1

2
unsigned char i;

digitalWrite(8,LOW);
for(i=0;i<=6;i++){
    if(x+i==6) shiftOut(11,12,LSBFIRST,dataArray[y]);
    else shiftOut(11,12,LSBFIRST,0);
    }
digitalWrite(8,HIGH);
delay(10);
digitalWrite(8,LOW);
1
  • that s exactly what i need thanks alot (y)
    – Abdelrahman Tarief
    Commented Sep 9, 2014 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.