Hex or binary data from array to digital.write state

Hi, I am new to the forums and somewhat new to working with Arduino and my C++ is quite rusty, but I am good about searching before asking. Here is the setup...

I am making a device that will route power, ground, analog +, and analog - to different pins on various connectors. The mentioned signals are routed to each of the necessary wires of these connectors via a relay board that will be controlled by the Arduino. The relay board is the 16 Channel board from Sainsmart and requires that the output of the Arduino go LOW to turn the relay ON. The box will have 15 different connectors with a varying number of wires in each. The important thing is that I have 14 distinct "Test Configurations" for 110 possible valid entries from the keypad.

The device has a 3x4 keypad with a 16x2 LCD display. I have successfully written a state machine that updates the display, accepts input from the keypad while both printing the entry and storing it. I have also been able to convert the keypad entry from a char array to an int value which is then used to scan an array of valid entries confirming a match. Once a match has been found in the keypad entry, it then uses the same element address to "lookup" which Test Configuration corresponds to that keypad entry.

My next step is to take that Test Configuration number (1 through 13) and use it to perform a digitalWrite() on multiple pins to set some of the relays ON and some of them OFF based on the requirements of the test.

I know that I could simply do: (I will keep it short and only do 4 IO pins but my project will use 16)

if (entry == 1){
   digitalWrite(38, 1);   //We can have pin 38 as LSB for the example
   digitalWrite(39, 0);
   digitalWrite(40, 1);
   digitalWrite(40, 0);

} else if (entry == 2) {
   digitalWrite(38, 1);   
   digitalWrite(39, 1);
   digitalWrite(40, 1);
   digitalWrite(40, 1);

    }

But I was wondering if I could store hex or binary values in an 13 element array (one element per test configuration) and then be able to use that array element to set all of the IO pins accordingly.

Something like (Again using 2 test modes and 4 IO pins for the example)

int TEST_MODE[2] = {0b0101, 0b1111}; //or use {0x5, 0xf} respectively

int selection = 1;

void TEST_OUTPUT(int choice) {
   
for (int n = 0; n < 4; n++) {
    digitalWrite(n+38, /* LSB of TEST_MODE[choice] */); 
}

return;
}

//Each increment of n inside of TEST_OUTPUT() would ideally also increment the bit position in the array
// element as it works towards the MSB from the LSB

void setup() {

for (int x = 38; x < 42; x++) {
    pinMode(x, OUTPUT);
}


}

void loop() {

TEST_OUTPUT(selection);
delay(5000);


}

I was just looking for either a magical function that will translate a binary or hex value into a defined output state of the desired IO pins or just a more elegant approach that a pro would use.

I also understand it is frowned upon to not show the entire code but since it is currently close to 150 lines or more I will just attach it and let you play with it versus pasting it completely in the text.

-73

KX4MA

State_Machine_Test.ino (4.82 KB)

mask = 1;
for (int bit = 0; bit < 13; bit++) {
  if (value & mask) {
    // Bit “bit” is set
  } else {
    // Bit “bit” is clear
  }
  mask <<= 1;
}

I was just looking for either a magical function that will translate a binary or hex value into a defined output state of the desired IO pins

What you are looking for is called Port Manipulation

To make it easy, you are going to need 16 pins on the Mega which lie completely on two port registers. Refer to a mega pin map and what pins are available to you, but looks to me like digital pins 22-29 are PORTA 0-7 and digital pins 30-37 are PORTC 7-0.

You can make an array of pin numbers, then it is relatively painless to do bulk operations on a set of pins, just as port manipulation does, but one pin at a time. The only difference really, is speed (usually the extra code size is not so big as to be very important). For reading switches and setting relays, individually writing I/O bits is normally quite acceptable.