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;
 }
 }
 }
Ricardo
3,3802 gold badges25 silver badges55 bronze badges
asked Sep 9, 2014 at 17:05
2
  • 2
    This question appears to be off-topic because it is a programming question. Commented Sep 9, 2014 at 17:59
  • @MattYoung you are right i didn't take this in mind sorry 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);
answered Sep 9, 2014 at 17:29
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.