9 years, 10 months ago.

array of analogin pins.

Hello, Is it possible to have an array of analog in pins?

It would be really helpful if I can find a way to create an array of analog pins.

For(int i=0;i<n;i++) { sensor_value[i] = analogpin[i].read(); }

Something like this...

Please let me know if something similar is possible If not, at least let me know an alternative. Thank you..

2 Answers

9 years, 10 months ago.

As mentioned by Greg an array of pointers is possible (btw for DigitalInOut you can also use BusInOut, which does that for you). But you can also directly define an array:

AnalogIn analogpin[] = {p10, p11, p12};  //And then with your pin names

Accepted Answer

thank you, this is exactly what i was looking for :)

posted by Aalok Bhat 06 Jun 2014

class array{ private : AnalogIn _analogpin[]; int _n; public : array (PinName pins[], int N) : analogpin(pins){ _n=N; } }

this AnalogIn-array is a private member in a class and I wish to initialize the class member with an array of PinName (as in the code above)

Can u suggest a way to do so?

posted by Pulkit Agarwal 11 Jun 2014

So it should have a variable length? Then you cannot do it in such a way with a member variable, that should have a fixed length. Using pointers then makes more sense: You simply make a pointer array which is long enough for the worst case scenario, and since pointers barely take space that isn't an issue.

See for example how BusIn does it: https://mbed.org/users/minicube/code/mbed-src-LPC1114FN28/file/b3acfef78949/common/BusIn.cpp and http://mbed.org/users/mbed_official/code/mbed-src/file/143cac498751/cpp/BusIn.h (they are different versions, so it might be that the .h and .cpp file don't exactly match anymore).

posted by Erik - 12 Jun 2014
9 years, 10 months ago.

You can have an array of pointers to analog pins. For example, I do this with digital i/o in http://mbed.org/users/gsteiert/code/mbd2pmd/