Controlling 10 74hc595n with Switch over Serial

Hi Guys,

I have a project that I am working on that has 10 shift registers...

The issue I am having is that I need to be able to send characters via serial to turn pins on and off.

the code below is what I have working, but instead of for example, case 'a' or case 'b' I need the unit to wait for more than 1 character before acting. this will enable me to have more than 255 options....

Can someone please help me. not sure what way to go here....

void loop(){
  
   if (Serial.available() > 0) {
     int inByte = Serial.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this example, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:

     switch (inByte) {
     case 'a':    
      setRegisterPin(0, HIGH);
      writeRegisters();       
       break;
     case 'b':    
      setRegisterPin(1, HIGH);
      writeRegisters();       
       break;

the below will not compile... but here is what I would like for example....

void loop(){
  
   if (Serial.available() > 0) {
     int inByte = Serial.read();
     // do something different depending on the character received.  
     // The switch statement expects single number values for each case;
     // in this exmaple, though, you're using single quotes to tell
     // the controller to get the ASCII value for the character.  For 
     // example 'a' = 97, 'b' = 98, and so forth:

     switch (inByte) {
     case '!0301':    
      setRegisterPin(0, HIGH);
      writeRegisters();       
       break;
     case '!0401':    
      setRegisterPin(0, LOW);
      writeRegisters();       
       break;
     case '!0302':    
      setRegisterPin(1, HIGH);
      writeRegisters();       
       break;
     case '!0402':    
      setRegisterPin(1, LOW);
      writeRegisters();       
       break;

You have the shift registers daisychained?
Make an array of 10 bytes,
update any byte you want then send all 10 out.

switch(incomingByte){
case 'a':
//clear 0-2
dataArray[0] = dataArray[0] & B11111101;
updateFlag = 1;
break;
case 'b':
// set 0-2
dataArray[0] = dataArray[0] | B00000010;
updateFlag =1;
break;
}
if (updateFlag == 1){
updateFlag = 0; // clear flag for next pass
digitalWrite (latchPin, LOW);
 for (x=0; x<10; x=x+1){
 SPI.transfer(dataArray[x]);
 }
digitalWrite(latchPin, HIGH);
}

etc.

yes they are daisy chained.... and I am using digital ports 4,5 & 6.

I am a little out of my depth with what you suggested. can you possibly explain a little more or put some more comments... sorry for being a little slow :slight_smile:

Why are you using 4,5,6? Go with 10 (SS),11 (MOSI),13 (SCK), much faster.
Import SPI into your sketch.
You have 10 bytes in an array
dataArray[0] to dataArray[9]

Set a bit high by ORing in a 1, leave the other bits alone
dataArray[0] = dataArray[0] | B00000010; // set bit 1 of dataArray[0]

Clear a bit low by ANDing in a 0, leave the other bits alone
dataArray[0] = dataArray[0] & B11111101; // clear bit 0 of dataArray[0]

Setting a flag to 1, and performing an action if the flag is set, is one to perform things as part of set of actions.
Clear the flag when the action is performed so you don't repeat the same action over & over.

I imagine the function
writeRegisters();
would do similar but as a subroutine.

How you set & clear bits based on the 'x' that you read, my example is but 1 way.