Scanning through array not working as desired

Greetings,
I have a program to communicate with X-Plane flight simulator where I scan through an
array made up of structures. Scanning though inputs connected to switches I update the
"currentState" for the associated input. No problem there. Each input has a X-Plane
Dataref key associate to it. Some Datarefs have multiple inputs. For example ignition
key switch has 5 positions with 4 inputs. Positions are OFF, LEFT, RIGHT, BOTH and
START. Only one associated input is active per position. Inputs attached to inputs are
LEFT, RIGHT, BOTH and START with OFF having no input. See structure and populate
array below:

// Switch/button array using structures
  struct switchStructure {
    char datarefKey[100];  // contains Dataref key
    int16_t activated;  // Value when switch is set
    int currentState;  // Last read state of switch
    unsigned long debounce;  // Millis time since last read
    uint8_t Pin; // GPIO pin used for switch
    uint8_t PullUp;  // Indicate if pin configured for internal pull-up resistor
    int16_t multipleInput; // Holds current value for switches that have multiple inputs, i.e. ignition key switch
    } switchStructure;

  // Initialize of an array of switch structures
  struct switchStructure switches[] = {
    { "sim/cockpit/electrical/strobe_lights_on", 1, 1, 0, 2, INPUT_PULLUP, 0},
    { "sim/cockpit/electrical/nav_lights_on", 1, 1, 0, 3, INPUT_PULLUP, 0},
    { "sim/cockpit2/switches/custom_slider_on[23}", 1, 1, 0, 4, INPUT_PULLUP, 0},
    { "sim/cockpit2/engine/actuators/ignition_key[0]", 1, 1, 0, 5, INPUT_PULLUP, 0},
    { "sim/cockpit2/engine/actuators/ignition_key[0]", 2, 1, 0, 6, INPUT_PULLUP, 0},
    { "sim/cockpit2/engine/actuators/ignition_key[0]", 3, 1, 0, 7, INPUT_PULLUP, 0},
    { "sim/cockpit2/engine/actuators/ignition_key[0]", 4, 1, 0, 8, INPUT_PULLUP, 0}
  };
 
  int switch_array_length = sizeof(switches) / sizeof(switchStructure);

After updating the "currentState" of the switch who's status has changed,
i.e. turned on or turned off, I clear the "currentState" for the associated switch.
Then I clear the "currentState" of of the inputs that have the same associates
Dataref as the switch that just changed. The final step is to set the "multipleInput"
field of the structure for all inputs with the same Dataref to the "activated" value
of the changed input. The purposed for doing this is to send an update message
with the status of all the switch/Dataref to X-Plane periodically to ensure nothing
was missed when sending on status changes when they occur.
Where my headaches occur is in the setting of the "multipleInput" fields and
checking if Datarefs with multiple inputs match. See "if" statement in "void
SetMultiplesForDataref(int key)" code below:

void checkSwitch(){
  for(int s = 0; s < switch_array_length; s++){
    int buttonState = 0; // variable for reading the pushbutton status
     
    // Have we passed the bounce range since last reading?
    // If so, read button
    unsigned long timer = millis() - switches[s].debounce;
    if(timer > debounceThreshold){
      // read the state of the pushbutton value:
      buttonState = digitalRead(switches[s].Pin);
      // ButtonState has changed
      // Update array and send appropriate message to controller
      switch(buttonState != switches[s].currentState){
        case true:
          switches[s].debounce = millis(); // Reset debounce
          switch(buttonState){
            case isActivated:
              sendESPNow(switches[s].datarefKey, switches[s].activated);  // Send state message to controller
              switches[s].currentState = isActivated;
              switches[s].multipleInput = switches[s].activated;
              deactivateMatchingDataref(s); // Update all matching Dataref key in array
              SetMultiplesForDataref(s);
              break;
            default:
              sendESPNow(switches[s].datarefKey, 0);  // Send state message to controller
              switches[s].currentState = isDeactivated;
              switches[s].multipleInput = 0; // Update all matching Dataref key in array
              break;
          }
            break;
      }
    }
  }
}

**void SetMultiplesForDataref(int key)**{
// Switches may have multiple inputs
// For example ignition switch has 5 positions with 4 inputs
// Positions are OFF, LEFT, RIGHT, BOTH and START
// Only one associated input is active per position
// Inputs attached to LEFT, RIGHT, BOTH and START
// with OFF having no input
// loop through array and update multipleInput for all inputs for associate Dataref
  for(int scan = 0; scan < switch_array_length; scan++){
	  debug(scan, key);
     
    **if(switches[scan].datarefKey == switches[key].datarefKey)**{
      switches[scan].multipleInput = switches[key].activated;
    }
  }
}


void debug(int scan, int key){
    Serial.println();
    Serial.print("***key ");
    Serial.print(key);
    Serial.print(": scan - ");
    Serial.print(scan);
    Serial.print(": multipleInput - ");
    Serial.print(switches[scan].multipleInput);
    Serial.print(": activated - ");
    Serial.print(switches[scan].activated);
    Serial.print(": switches[scan].datarefKey - ");
    Serial.print(switches[scan].datarefKey);
    Serial.print(": switches[key].datarefKey - ");
    Serial.print(switches[key].datarefKey);
    Serial.print(": true/false - ");
    Serial.println(switches[scan].datarefKey == switches[key].datarefKey);
}

Only the "multipleInput" associated with the input being change is up
dated. I have a debug routine to print the status of all inputs every 10 seconds.

Serial.println("+++++++++++++++++++++++++++++++++++++++++");
  for(int x = 0; x < switch_array_length; x++){
    Serial.print(x);
    Serial.print(" - ");
    Serial.print(switches[x].datarefKey);
    Serial.print(" - ");
    Serial.print(switches[x].activated);
    Serial.print(" - ");
    Serial.print(switches[x].currentState);
    Serial.print(" - ");
    Serial.print(switches[x].multipleInput);
    Serial.println();

See output below:

index - datarefkey - activated - currentState - multipleInput
+++++++++++++++++++++++++++++++++++++++++
0 - sim/cockpit/electrical/strobe_lights_on - 1 - 1 - 0
1 - sim/cockpit/electrical/nav_lights_on - 1 - 1 - 0
2 - sim/cockpit2/switches/custom_slider_on[23} - 1 - 1 - 0
3 - sim/cockpit2/engine/actuators/ignition_key[0] - 1 - 1 - 0
4 - sim/cockpit2/engine/actuators/ignition_key[0] - 2 - 0 - 2
5 - sim/cockpit2/engine/actuators/ignition_key[0] - 3 - 1 - 0
6 - sim/cockpit2/engine/actuators/ignition_key[0] - 4 - 1 - 0
------------------------------------------

Code from checking input to the setting of the "multipleInput" fields
with another debug routine to print the field as it rolls through the for
loop is below:

void checkSwitch(){
  for(int s = 0; s < switch_array_length; s++){
    int buttonState = 0;   // variable for reading the pushbutton status
     
    // Have we passed the bounce range since last reading?
    // If so, read button
    unsigned long timer = millis() - switches[s].debounce;
    if(timer > debounceThreshold){
      // read the state of the pushbutton value:
      buttonState = digitalRead(switches[s].Pin);
      // ButtonState has changed
      // Update array and send appropriate message to controller
      switch(buttonState != switches[s].currentState){
        case true:
          switches[s].debounce = millis();  // Reset debounce
          switch(buttonState){
            case isActivated:
              sendESPNow(switches[s].datarefKey, switches[s].activated);   // Send state message to controller
              switches[s].currentState = isActivated;
              switches[s].multipleInput = switches[s].activated;
              deactivateMatchingDataref(s);     // Update all matching Dataref key in array
              SetMultiplesForDataref(s);
              break;
            default:
              sendESPNow(switches[s].datarefKey, 0);   // Send state message to controller
              switches[s].currentState = isDeactivated;
              switches[s].multipleInput = 0;  // Update all matching Dataref key in array
              break;
          }
            break;
      }
    }
  }
}

void SetMultiplesForDataref(int key){
 for(int scan = 0; scan < switch_array_length; scan++){
	 ** debug(scan, key);**
     
    **if(switches[scan].datarefKey == switches[key].datarefKey)**{
      switches[scan].multipleInput = switches[key].activated;
    }
  }
}


void debug(int scan, int key){
    Serial.println();
    Serial.print("***key ");
    Serial.print(key);
    Serial.print(": scan - ");
    Serial.print(scan);
    Serial.print(": multipleInput - ");
    Serial.print(switches[scan].multipleInput);
    Serial.print(": activated - ");
    Serial.print(switches[scan].activated);
    Serial.print(": switches[scan].datarefKey - ");
    Serial.print(switches[scan].datarefKey);
    Serial.print(": switches[key].datarefKey - ");
    Serial.print(switches[key].datarefKey);
    Serial.print(": true/false - ");
    Serial.println(switches[scan].datarefKey == switches[key].datarefKey);
}

Expected output from the debug function is bellow:

***key 4: scan - 0: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit/electrical/strobe_lights_on: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 1: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit/electrical/nav_lights_on: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 2: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit2/switches/custom_slider_on[23}: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 3: multipleInput - 2: activated - 1: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 1
***key 4: scan - 4: multipleInput - 2: activated - 2: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 1
***key 4: scan - 5: multipleInput - 2: activated - 3: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 1
***key 4: scan - 6: multipleInput - 2: activated - 4: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 1

The actual output is below:

***key 4: scan - 0: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit/electrical/strobe_lights_on: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 1: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit/electrical/nav_lights_on: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 2: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit2/switches/custom_slider_on[23}: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 3: multipleInput - 0: activated - 1: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 4: multipleInput - 2: activated - 2: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 1
***key 4: scan - 5: multipleInput - 0: activated - 3: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0
***key 4: scan - 6: multipleInput - 0: activated - 4: switches[scan].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: switches[key].datarefKey - sim/cockpit2/engine/actuators/ignition_key[0]: true/false - 0

Notice the "multipleInput" and the "true/false" at the end of the line for the last four
lines. They have matching Dataref values, but the "if" statement is only setting the
one with a matching index as true and not the rest.

I cannot figure out what I'm doing wrong. Any assistance/advice would be
greatly appreciated.

Would you correct the markup on the text part of your post?

Fixed, I think. Markup that is

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.