An array that contains other array names that contain integers handled in a nessted for loop

i have this code :

#include <CD74HC4067.h>

// s0 s1 s2 s3
CD74HC4067 mux1(0, 1, 2, 3);  // create a new CD74HC4067 object with its four control pins
CD74HC4067 mux2(4, 5, 6, 7);
CD74HC4067 mux3(8, 9, 10, 11);
CD74HC4067 mux4(12, 13, 14, 15);
CD74HC4067 mux5(16, 17, 18, 19);
CD74HC4067 mux6(20, 21, 22, 23);
CD74HC4067 mux7(24, 25, 26, 27);
CD74HC4067 mux8(28, 29, 30, 31);
CD74HC4067 mux9(32, 33, 34, 35);
CD74HC4067 mux10(36, 37, 38, 39);
CD74HC4067 mux11(40, 41, 42, 43);
CD74HC4067 mux12(44, 45, 46, 47);
CD74HC4067 mux13(48, 49, 50, 51);
CD74HC4067 mux14(52, 53, A1, A2);
CD74HC4067 mux15(A3, A4, A5, A6);
CD74HC4067 mux16(A7, A8, A9, A10);
CD74HC4067 mux17(A11, A12, A13, A14);
int * my_mux[] = {&mux1, &mux2, &mux3, &mux4, &mux5, &mux6, &mux7, &mux8, &mux9, &mux10, &mux11, &mux12, &mux13, &mux14, &mux15, &mux16, &mux17};

const int g_common_pin = A0; // select a pin to share with the 16 channels of the CD74HC4067

void setup()
{
  pinMode(g_common_pin, INPUT); // set the initial mode of the common pin.
  // This can be changed in loop() for for each channel.
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);
  pinMode(19, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(21, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, OUTPUT);
  pinMode(37, OUTPUT);
  pinMode(38, OUTPUT);
  pinMode(39, OUTPUT);
  pinMode(40, OUTPUT);
  pinMode(41, OUTPUT);
  pinMode(42, OUTPUT);
  pinMode(43, OUTPUT);
  pinMode(44, OUTPUT);
  pinMode(45, OUTPUT);
  pinMode(46, OUTPUT);
  pinMode(47, OUTPUT);
  pinMode(48, OUTPUT);
  pinMode(49, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(51, OUTPUT);
  pinMode(52, OUTPUT);
  pinMode(53, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);
  pinMode(A6, OUTPUT);
  pinMode(A7, OUTPUT);
  pinMode(A8, OUTPUT);
  pinMode(A9, OUTPUT);
  pinMode(A10, OUTPUT);
  pinMode(A11, OUTPUT);
  pinMode(A12, OUTPUT);
  pinMode(A13, OUTPUT);
  pinMode(A14, OUTPUT);
}

void loop()
{
  for (int x = 0; x < 16; x ++) {
    for (int i = 0; i < 16; i++) {
      my_mux[x].channel(i);
      mux17.channel(x);
      analogRead(g_common_pin);
    }
  }
}

why do i get this error ?

sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
sketch_apr21a:21:144: error: cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization
C:\Users\athan\OneDrive\Έγγραφα\Arduino\sketch_apr21a\sketch_apr21a.ino: In function ‘void loop()’:
sketch_apr21a:103:17: error: request for member ‘channel’ in ‘my_mux’, which is of non-class type ‘int*’
exit status 1
cannot convert ‘CD74HC4067*’ to ‘int*’ in initialization

It won’t make any difference to the error, but why not do yourself a favour and use a for loop instead of 68 lines of pinMode() commands

1 Like

you are correct ! i know that that will work ! i just want to keep it as simple as i can right now in order to be sure that there is nothing else creating any kind of syntax errors or any other kind of troubles .

Your code is simpler than a for loop ?
I don’t think so !

mux1 to mux 17 are objects and cannot be defined with type int.
int * my_mux = {& mux1, & mux2, & mux3, …

how should i declare my my_mux array ?

More specifically, “&mux1” is a pointer to an object of type CD74HC4067. You can’t store that pointer in an array that contains pointers ints (i.e. int *).

Why are you using pointers anyway? Why not just make an array of CD74HC4067 objects?

so
CD74HC4067 my_mux={mux1, mux2, mux3, …, mux17};
whould do the job ?

my_mux is not an array of pointers to int, they are pointers to CD74HC4067 objects, so it should be :

CD74HC4067 * my_mux[] = {&mux1, &mux2, ...

But, you could simply do that instead:

CD74HC4067 my_mux[] = {
  CD74HC4067(0, 1, 2, 3),
  CD74HC4067(4, 5, 6, 7),
  ...
1 Like

my main problem is to adress to mux1, mux2, mux3, … , mux17 inside that nested for loop !

void loop()
{
  for (int x = 0; x < 16; x ++) {
    for (int i = 0; i < 16; i++) {
      my_mux[x].channel(i);
      mux17.channel(x);
      analogRead(g_common_pin);
    }
  }
}

if you have an other idea you are more that welcome !

i have to take my daughter out for a walk ! i’ll try it later ! i think that we have a winer here !

thank you very much !

Forget about mux1, mux2, …
Delete those lines then do what @guix suggested:

#include <CD74HC4067.h>

// s0 s1 s2 s3
CD74HC4067 my_mux[] =
{
  CD74HC4067 (0, 1, 2, 3),  // create a new CD74HC4067 object with its four control pins
  CD74HC4067 (4, 5, 6, 7),
  CD74HC4067 (8, 9, 10, 11),
  CD74HC4067 (12, 13, 14, 15),
  CD74HC4067 (16, 17, 18, 19),
  CD74HC4067 (20, 21, 22, 23),
  CD74HC4067 (24, 25, 26, 27),
  CD74HC4067 (28, 29, 30, 31),
  CD74HC4067 (32, 33, 34, 35),
  CD74HC4067 (36, 37, 38, 39),
  CD74HC4067 (40, 41, 42, 43),
  CD74HC4067 (44, 45, 46, 47),
  CD74HC4067 (48, 49, 50, 51),
  CD74HC4067 (52, 53, A1, A2),
  CD74HC4067 (A3, A4, A5, A6),
  CD74HC4067 (A7, A8, A9, A10)
};

CD74HC4067 muxmux(A11, A12, A13, A14);

const int g_common_pin = A0; // select a pin to share with the 16 channels of the CD74HC4067

int analogReadMux(byte channel)
{
  byte mux = (channel >> 4) & 0x0F;  // Bits 7-4
  byte chan = channel & 0x0F;  // Bits 3-0
  my_mux[mux].channel(chan);
  muxmux.channel(mux);
  return analogRead(g_common_pin);
}

void setup()
{
  // DO NOT USE pinMode() ON PINS FOR analogRead().  Only for digitalRead()/digitalWrite()

  for (int pin = 0; pin <= 53; pin++)
    pinMode(pin, OUTPUT);

  for (int pin = A1; pin <= A14; pin++)
    pinMode(pin, OUTPUT);
}

void loop()
{
  for (int x = 0; x < 16; x ++)
  {
    for (int i = 0; i < 16; i++)
    {
      analogReadMux((x << 4) + i);
    }
  }
}

Alternatively:

  for (int address = 0; address < 256; address++)
  {
    analogReadMux(address);
  }

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