0

I'm trying to rename pins so that they post as some kind of alpha-numeric combination in a database. For example, if I define the following....

#define APLHA 0
#define BRAVO 2
#define CHARLIE 5
#define DELTA 6

...I would like to rename the output of the pins, as recorded in the db, as something other than 0, 2, 5, and 6 (or whatever pin is used). Let's say A, B,C, and D for the purposes of this exercise.

Is this just as simple as creating if() function that is called upon prior to posting to the db (ex. if 0 then A, if 2, then B, etc)? I feel like there must be a better, dare I say more elegant(?), way of doing this?

Current output:
PIN | VALUE
0 | 1
2 | 1
5 | 0
6 | 0

Desired:
PIN | VALUE
A | 1
B | 1
C | 0
D | 0

3
  • 4
    I would like to rename the output of the pins, as recorded in the db - I don't understand this, sorry. You can give pins names, as in your list of defines. What is the issue here?
    – Nick Gammon
    Jun 18, 2016 at 3:17
  • Why not record the names in the database and let the database do the databasing instead. Jun 18, 2016 at 9:31
  • Sounds like I'm overcomplicating this.
    – acpilot
    Jun 18, 2016 at 12:32

1 Answer 1

1

If I understood your question correctly, you're looking for something like this:

static const byte PIN_NUMBER[4] = { 0, 2, 5, 6 };
static const char *PIN_NAME[4] = { "A", "B", "C", "D" };

// and use them like this:

void Log(byte index)
{
  byte val = digitalRead(PIN_NUMBER[index]);
  Serial.print(PIN_NAME[index]);
  Serial.print('|');
  Serial.println(val);
}

void setup()
{
  for (byte i = 0; i < 4; ++i) pinMode(PIN_NUMBER[i], INPUT);
}

void loop()
{
  Serial.println("PIN|VALUE");
  for (byte i = 0; i < 4; ++i) Log(i);
  delay(1000);
}

Also, in the special case of "A", "B", "C", "D" as pin names, you don't really need the PIN_NAME array, instead of

Serial.print(PIN_NAME[index]);

you can use:

Serial.print('A' + index);
1
  • This is exactly what I was looking for! Thank you. I'll try it and report back but this looks to be the solution.
    – acpilot
    Jun 21, 2016 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.