Declaring analog pins as digital and setting up with a loop

I have a project where I simulate up to 26 wiegand readers using an arduino mega. I use this library for the wiegand output: GitHub - castrofernando/WiegandOutput-Arduino: Wiegand output library for 26 and 34 bits for arduino

Instead of declaring declaring the spesific pins to output the wiegand to each time, I have the pin pairs stored in an array so I can use the array as a "reader channel" insted.

My array for the pins looks like this:

// Define the pin pairs for each reader
const int pins[][2] = {
  {54, 55}, // Pin pairs for reader 1
  {56, 57},  // Pin pairs for reader 2
  {58, 59},   // Pin pairs for reader 3
  {60, 61},   // Pin pairs for reader 4
  {62, 63}, // Pin pairs for reader 5
  {64, 65}, // Pin pairs for reader 6
  {66, 67}, // Pin pairs for reader 7
  {68, 69},  // Pin pairs for reader 8
  {6, 7},   // Pin pairs for reader 9
  {8, 9},   // Pin pairs for reader 10
  {22, 23}, // Pin pairs for reader 11
  {24, 25}, // Pin pairs for reader 12
  {26, 27}, // Pin pairs for reader 13
  {28, 29}, // Pin pairs for reader 14
  {30, 31}, // Pin pairs for reader 15
  {32, 33}, // Pin pairs for reader 16
  {34, 35}, // Pin pairs for reader 17
  {36, 37}, // Pin pairs for reader 18
  {38, 39}, // Pin pairs for reader 19
  {40, 41}, // Pin pairs for reader 20
  {42, 43}, // Pin pairs for reader 21
  {44, 45}, // Pin pairs for reader 22
  {46, 47}, // Pin pairs for reader 23
  {48, 49}, // Pin pairs for reader 24
  {50, 51}, // Pin pairs for reader 25
  {52, 53}  // Pin pairs for reader 26
};

I then do this to initialize all the pins:

const unsigned int NUM_READERS = sizeof(pins) / sizeof(pins[0]);
WiegandOut* wiegandReaders[NUM_READERS];

  for (int i = 0; i < NUM_READERS; i++) {
    wiegandReaders[i] = new WiegandOut(pins[i][0], pins[i][1]);
  }


And when I want to send something to a readerchannel I do this

wiegandReaders[readerNumber - 1]->send(cardNumber, 26, true);

The rest of the code is recieving and parsing MQTT messages and forwarding carddata to spesific channels.

This setup works, and works in my current code as long as I only use analog pins. In the first snippet of this post i declare them as 54-69, but I have also used the A0-A15 naming convention. When I add the analog pins to the array and use either naiming convention, my code stops executing after the first mqtt message is recieved. The message is handeled correctly and the wiegand data is passed as it should, but for some reason it just stops. I am unfortuntely not able to share the whole code, but since I have narrowed the issue down to the contents of the array, I am hoping that fixing this fixes the code stopping.

So with this setup, what do I need to do to use analog pins in this scenario as digital outputs? I have run around in circles for a while now and can't make heads or tales of this any more even if it's a basic thing.

To use analog pins as digital pins it just need to declare the pins as digital pins i think number them from 54-69 should be enough .After getting the message the code stops ,it would be them because you not programmed your listening section again to work again after sending the wiegand data. if still not getting output try giving pinMode(pin,OUTPUT);to the pins you want to use .

So something like this?

const int analogPins[] = {A0, A1, A2};  // Choose the desired analog pins
const int digitalPins[] = {22, 23, 24}; // Choose the corresponding digital pin numbers

void setup() {
  for (int i = 0; i < sizeof(analogPins) / sizeof(analogPins[0]); i++) {
    pinMode(digitalPins[i], OUTPUT);
  }
}

There seem to be places where analog and digital are mistaken words in the description and the solution offered. I may need a second period of time later to sort it.

It seems unlikely, however. If you cannot or will not share the code, please make a minimal sketch that compiles and demonstrates your problem so that we can see what is really going on, and if indeed some analog pins used as digital pins truly cause problems.

Besides the few gotcha! analog pins that don't work as digital outputs, I am not aware of any other things like this that I would,d very much like to be aware of.

Perhaps it will all be clear when I take more time to read words, but at a glance the code you did post looks entirely plausible.

a7

Does not matter if you use the Ax or numberic designation for the analog pin, the Ax name is just a define for the number.

How much memory (ram) are you using? Since you are using "new" that memory will not be shown in the total from the compiler.

It sounds like you're contradicting yourself. Can you clarify?

Where do you do the initialisation of the WiegandReaders that contains the above snip. How much memory does one WiegandOut object consume?

When I say I have used both 54-69 and A0-A15, what I mean is that in the array I have tried both. When doing this I have done nothing else to the code than modify the array. So:

const int pins[][2] = {
  {54, 55}, // Pin pairs for reader 1
  {56, 57},  // Pin pairs for reader 2
  {58, 59},   // Pin pairs for reader 3
  {60, 61},   // Pin pairs for reader 4
  {62, 63}, // Pin pairs for reader 5
  {64, 65}, // Pin pairs for reader 6
  {66, 67}, // Pin pairs for reader 7
  {68, 69},  // Pin pairs for reader 8
  {6, 7},   // Pin pairs for reader 9
  {8, 9},   // Pin pairs for reader 10
  {22, 23}, // Pin pairs for reader 11
  {24, 25}, // Pin pairs for reader 12
  {26, 27}, // Pin pairs for reader 13
  {28, 29}, // Pin pairs for reader 14
  {30, 31}, // Pin pairs for reader 15
  {32, 33}, // Pin pairs for reader 16
  {34, 35}, // Pin pairs for reader 17
  {36, 37}, // Pin pairs for reader 18
  {38, 39}, // Pin pairs for reader 19
  {40, 41}, // Pin pairs for reader 20
  {42, 43}, // Pin pairs for reader 21
  {44, 45}, // Pin pairs for reader 22
  {46, 47}, // Pin pairs for reader 23
  {48, 49}, // Pin pairs for reader 24
  {50, 51}, // Pin pairs for reader 25
  {52, 53}  // Pin pairs for reader 26
};

and

const int pins[][2] = {
  {A0, A1}, // Pin pairs for reader 1
  {A2, A3},  // Pin pairs for reader 2
  {A4, A5},   // Pin pairs for reader 3
  {A6, A7},   // Pin pairs for reader 4
  {A8, A9}, // Pin pairs for reader 5
  {A10, A11}, // Pin pairs for reader 6
  {A12, A13}, // Pin pairs for reader 7
  {A14, A15},  // Pin pairs for reader 8
  {6, 7},   // Pin pairs for reader 9
  {8, 9},   // Pin pairs for reader 10
  {22, 23}, // Pin pairs for reader 11
  {24, 25}, // Pin pairs for reader 12
  {26, 27}, // Pin pairs for reader 13
  {28, 29}, // Pin pairs for reader 14
  {30, 31}, // Pin pairs for reader 15
  {32, 33}, // Pin pairs for reader 16
  {34, 35}, // Pin pairs for reader 17
  {36, 37}, // Pin pairs for reader 18
  {38, 39}, // Pin pairs for reader 19
  {40, 41}, // Pin pairs for reader 20
  {42, 43}, // Pin pairs for reader 21
  {44, 45}, // Pin pairs for reader 22
  {46, 47}, // Pin pairs for reader 23
  {48, 49}, // Pin pairs for reader 24
  {50, 51}, // Pin pairs for reader 25
  {52, 53}  // Pin pairs for reader 26
};

@david_2018 asked about memory consumption, how do I meassure this? I am very new to programming and I apologize for the basic questions.

I do have a earlier iteration where instead of MQTT I send commands over serial that works where I use the same loop to create the objects, their sizes are as follows:

MQTT version:
Sketch uses 23284 bytes (9%) of program storage space. Maximum is 253952 bytes.
Global variables use 2167 bytes (26%) of dynamic memory, leaving 6025 bytes for local variables. Maximum is 8192 bytes.

Serial verison:
Sketch uses 12414 bytes (4%) of program storage space. Maximum is 253952 bytes.
Global variables use 1416 bytes (17%) of dynamic memory, leaving 6776 bytes for local variables. Maximum is 8192 bytes.

There is a piece of code in this link: Measuring Memory Usage | Memories of an Arduino | Adafruit Learning System. Put calls to the freeMemory function in strategic places (e.g. after creating and populating the array).

Be aware of a possible problem with that code as described here: Classic Nano has too much RAM

Thank you to all replies so far. I have continued debugging and found the spesific line of code where everything grinds to a halt, still no clue why the content of my pins array is relevant since at that point in the process the pins in questions are not used.

However I wrongly assumed that the problem was with all Analog pins, the issue is spesifically if A4 and A5 (58 and 59) are used.
image

Looking into the datasheet of the ethernet board I am using it doesn't say it uses those, and they are not used elsewhere in the code. If anyone has any ideas why these two pins are special I'd appreciate the help, in the mean time I'll look into them and the libraries I use in case they are hardcoded somewhere or something.

Does the sketch use the I2C interface ?

Goddamn this forum is quick and to the point.

Yes it does, it controls relays and the problematic line does exactly that.

Which pins does the I2C interface use ?

This is the thing that threw me of, because I have not declared them so they have to be in a library somewewhere.

I am using the seed studio ethernet shield with Grove connectors

The grove connector is the one marked I2C Interface, so I assume SCL and SDA are the ones used? And the documentation does not mention (or at least I did not find that it does) that it uses any analog pins?

Though it seems improbable, if you're unwilling or unable to divulge the code, kindly provide a concise demonstration that compiles, shedding light on the issue at hand. This will enable us to discern the true nature of the problem, particularly if certain analog pins used as digital pins are indeed causing complications.

Aside from the occasional hiccup with analog pins masquerading as digital outputs, I'm not privy to any other peculiarities of this nature. However, I'm eager to uncover any additional intricacies that may elude my current awareness.

Perhaps with a more thorough perusal of the documentation, the fog surrounding the code will dissipate. At first glance, the code you've shared seems entirely plausible.
Best Regards:
Sonic Menu

If the shield is intended to be used by a Uno then SDA and SCL will be connected to A4 and A5 respectively

If you're using that shield on a Mega, A4 and A5 don't carry the I2C signals and the I2C connector marked I2C Interface will not carry the I2C signals. So A4/A5 for the Wiegand should not pose an issue.

The I2C interface is available on pins 20/21 of the Mega

I agree that it should not pose an issue, but for some reason it does. I had the same issue with using pins 2 and 3 for some reason. I shifted things around a bit and now I have an array that works. If I had the time I would closer into what the root cause is but since it now works I'll go with that.

Ill leave the thread open for a wile in case someone wants to weigh in, otherwise I will mark this as the solution, as in check that the pins aren't in use.

Thank you to everyone who responded even if I didn't reply directly.

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