bitClear / bitWrite / bitSet / bitRead .. Array ..

cattledog:
Robin2 has written an excellent tutorial on reading serial data. http://forum.arduino.cc/index.php?topic=288234.0

If you choose to input characters representing numbers in hex you will need to use strtol with base 16 rather than atoi to convert your character input to numbers.

void setup() {

Serial.begin(9600);
  int dataAreg[16] = { 0x01, 0x03, 0x05, 0x07, 0x09,
                      0x0B, 0x0D, 0x0F, 0x11, 0x13, 0x15, 0x17, 0x19, 0x1B,
                      0x1D, 0x1F
                    };
  char hex_string[] = "0x1D";
  Serial.println(hex_string);
  long val = strtol(hex_string, NULL, 16);
  Serial.println(val);
  for (int i = 0; i < 15; i++) {
    if (dataAreg[i] == val) {
      Serial.print ("match at index ");
      Serial.println(i);
    }
  }
}

void loop() {}




Since you are really only trying to find an array index matching the input, you could also have dataAreg and dataBreg be 2 dimensional arrays of character strings and use strcmp to test the input without conversion.


void setup() {
  Serial.begin (9600);
  char dataAreg[16][5] = { "0x01", "0x03", "0x05", "0x07", "0x09",
                          "0x0B", "0x0D", "0x0F", "0x11", "0x13", "0x15", "0x17", "0x19", "0x1B",
                          "0x1D", "0x1F"
                        };

char hex_string[5] = {"0x1D"};
  Serial.println(hex_string);

for (int i = 0; i < 16; i++) {
    if (strcmp(dataAreg[i], hex_string) == 0)
    {
      Serial.print("match at index ");
      Serial.println(i);
    }
  }
}

void loop() {}

I appreciate this...

However, I haven't got a problem with the input. It's translating it to bits. There are 16 options, but I need to fit it in to an array of 4.

One problem I can see is that when I wish to flip some of outputBufferB to low, one's that I need to remain high are then shut off.

I've tried to use outputBufferB1 = outputBufferB1 | outputBufferB0 etc, but this still is not working for me.

When the loop completes it doesn't seem to retain the data in the way that I require.