Problem with Toggle Switch Input

This is my first time posting for help here - though I have lurked for years so I will do my best to post as complete a question as possible. Some of the code is my own, most is from others and I make no claims to it being correct, well written, following best practices, smart or otherwise anything but trash - so before firing off such commentary know that I "get" that this is bad code, poorly written for a nonsensical task that the Arduino gods should probably strike me down for even considering.

The base code for this project is from Martin Currey and his excellent tutorial on Arduino serial control found here.

What I want to accomplish
I want to use 8 switches toggle switches to set high/low values and send those values to the Arduino when I press a monetary pushbutton. I want to use those values to trigger specific actions.

What works
I have attached a crappy photo of my terrible circuit - it uses 8 slide switches (in place of toggles), a tactile switch, pulldown resistors (,probably wrong values) a 74HC165 shift register, some led's with current limiting resistors (probably wrong values) and a Mega2560.

As intended, when you set the switches and press the momentary switch, the values display in the serial monitor. When you type commands into the serial monitor with start and end markers defined in the code by M.Currey the correct output occurs: sets pin 4 HIGH and the attached LED illuminates. sets pin 4 LOW and the attached LED extinguishes. The same is true for <11111111> and <10000000>.

What does not work
When entering data from the shift register, I need the 8 bits to form a "keyword" (for lack of a better term) that will work in the same way as when entered via the serial port. The series of bits displays correctly in the serial port, but it is not read by the Arduino as a command - it is just writing out the 1's and 0's from the FOR loop as expected.

What I do not understand how to do is to obtain a single output variable from the FOR loop in the display_pin_values function (below with some testing artifacts) or a separate function that will accomplish the same thing as if it were typed into the serial port and sent to the Arduino. The full code is attached - it exceeded to post size limit.

void display_pin_values()
{
  //start marker
  //Serial.print("<");

  //Serial.print("Experiment:\r\n");
  for (int i = 0; i < DATA_WIDTH; i++)
  {

    if ((pinValues >> i) & 1) {
      Serial.print("1");
    }
    else
      Serial.print("0");
  }
  //end marker
  //Serial.print(">");

  Serial.print("\r\n");
}

Attachments
Photo of circuit and .ino file.

Thanks for any help you are able to provide.

arduinoSerialControl_01_sw_input.ino (9.12 KB)

obtain a single output variable from the FOR loop

The data variable is the single output variable. Untested example.

int data = 0;  // added  **declare globally** 

void display_pin_values()
{
  //start marker
  //Serial.print("<");

  //Serial.print("Experiment:\r\n");
  for (int i = 0; i < DATA_WIDTH; i++)
  {

    if ((pinValues >> i) & 1) {
      Serial.print("1");
      bitSet(data, i);  // added
    }
    else
      Serial.print("0");
      bitClear(data, i);  // added
  }
  //end marker
  //Serial.print(">");
  
  Serial.print("\r\n");
}

How can I set "data" as an array populated by the FOR loop? I need to be able to parse it similar to this:

  if (data[0] == '2'  )
  {
    int tmp = 4;
    if ( data[1] == '5' && receivedChars[2] == '5' ) {
      digitalWrite(tmp, HIGH);
    }
    if ( data[1] == '5' && receivedChars[2] == '4' ) {
      digitalWrite(tmp, LOW);
    }
  } // PIN

I guess that I do not understand. You wanted a "single output variable". That does not sound like an array. What is the data array to contain?

I took "single output variable" to mean the numeric representation of the binary pinValues.

I guess that I do not understand.

No that would be me lol - I added your code and added a new parse function -

void dataInput(){
    //swtest 2
  // PIN
  if (data == 255  )
  {
    int tmp = 4;
    digitalWrite(tmp, HIGH);
  }
  if ( data == 254) {
    int tmp = 4;
    digitalWrite(tmp, LOW);
  }
}

This provides exactly what I was trying to accomplish - I cannot thank you enough, I have been working this problem for many hours. It was unclear to me why the parse function worked when sent from the serial port, but seemed to be unrecognized when send from the shift register. I had never encountered the bitSet or BitClear commands - they work perfectly.

If interested I have another problem to tackle that is similar - In the attached .ino I would like the expression builder to recognize floating point values. It seems like it should be simple, but it is not my code and attempts to change the data types, parse function etc. have not resulted in being able to do so.

In any case - thanks for the code suggestion!

TinyBasicPlus.ino (45.6 KB)