1

I'm attempting to re-write a library for the CD74HC4067 multiplexer. There is a library available for this device on the Arduino Playground, however, it's limited in its functionality.

I have this as a private data member in the class header file:

uint8_t channelSelects[16][4];

And this in the class constructor:

CD74HC4067::channelSelects[16][4] = {
    { 0,0,0,0 }, // channel 0
    { 1,0,0,0 }, // channel 1
    { 0,1,0,0 }, // channel 2
    { 1,1,0,0 }, // channel 3
    { 0,0,1,0 }, // channel 4
    { 1,0,1,0 }, // channel 5
    { 0,1,1,0 }, // channel 6
    { 1,1,1,0 }, // channel 7
    { 0,0,0,1 }, // channel 8
    { 1,0,0,1 }, // channel 9
    { 0,1,0,1 }, // channel 10
    { 1,1,0,1 }, // channel 11
    { 0,0,1,1 }, // channel 12
    { 1,0,1,1 }, // channel 13
    { 0,1,1,1 }, // channel 14
    { 1,1,1,1 }  // channel 15
};

And I'm getting this error:

cannot convert '<brace-enclosed initializer list>' to 'uint8_t {aka unsigned char}'

Could someone please explain what I'm not understanding here ?

Many thanks in advance.

3
  • 1
    Are you sure that brace enclosed initializer list can be used in normal assigment beyond variable/const init ?
    – TMa
    Feb 12 '17 at 12:07
  • Actually, no I'm not. The only difference I see here is the library available on the Arduino Playground has the channel selects defined as external (extern) and defined outside the class. Feb 12 '17 at 12:31
  • Aha, the channel select is static init ? So add type: uint8_t CD74HC4067::channelSelects[16][4] = { .... };
    – TMa
    Feb 12 '17 at 19:10
1

I moved the initialization to the header file where it was first declared... It compiled without errors.

1
  • You may just print out the array as well, compiling w/o errors doesn't neccesarily mean it does exactly what you think it does ;p Also, the values in your array are 4-bit LSB representations of the integers 0to15, you can store this much more efficiently or calculatie it runtime. Or consider using an array of objects/(bit-)structs instead of an array of arrays of unsigned chars, only to represent bools.
    – Paul
    Mar 15 '17 at 8:59
1

If you have in your constructor this:

CD74HC4067::channelSelects[16][4] = ...

then you are trying to assign something into the element at position [16][4]. Thats why it's telling you

cannot convert '' to 'uint8_t {aka unsigned char}'

Anyway - you don't even need that second dimension. You can use binary representation like 0b00001000 ...

And if you'll think about previous line, you don't even need any array, you can do it with simple counter value and getting its bits.

However if you want to rely on this array, the constructor should look like:

CD74HC4067::CD74HC4067() :
  channelSelects {
    { 0,0,0,0 }, // channel 0
    { 1,0,0,0 }, // channel 1
    { 0,1,0,0 }, // channel 2
    { 1,1,0,0 }, // channel 3
    { 0,0,1,0 }, // channel 4
    { 1,0,1,0 }, // channel 5
    { 0,1,1,0 }, // channel 6
    { 1,1,1,0 }, // channel 7
    { 0,0,0,1 }, // channel 8
    { 1,0,0,1 }, // channel 9
    { 0,1,0,1 }, // channel 10
    { 1,1,0,1 }, // channel 11
    { 0,0,1,1 }, // channel 12
    { 1,0,1,1 }, // channel 13
    { 0,1,1,1 }, // channel 14
    { 1,1,1,1 }  // channel 15
  }
{
   // constructor code
}

(maybe one more {} enclosure, I'm not sure right now exactly)

EDIT: I'd go this way (if you need only one or two instances):

// ADC READ channel + Address selection pins
template <uint8_t ADC_Chan, uint8_t Addr0, uint8_t Addr1, uint8_t Addr2, uint8_t Addr3>
class Mux16b {
public:
  void initPins () {
    pinMode(Addr0, OUTPUT);
    pinMode(Addr1, OUTPUT);
    pinMode(Addr2, OUTPUT);
    pinMode(Addr3, OUTPUT);
    pinMode(ADC_Chan, INPUT);
  }

  int readMux(uint8_t channel) {
    digitalWrite(Addr0, channel&1 != 0);
    digitalWrite(Addr1, channel&2 != 0);
    digitalWrite(Addr2, channel&4 != 0);
    digitalWrite(Addr3, channel&8 != 0);
    // maybe some delay to stabilize voltage at the ADC_Chan input ??
    return analogRead(ADC_Chan);
  }

  // not necessary but it's easier to use   mux(10) than mux.readMux(10)
  int operator()(uint8_t channel) { return readMux(channel); }
};

// analogRead from A0, Address pins: 4 9 5 2
Mux16b<A0,4,9,5,2>  mux;

void setup() {
  Serial.begin(115200);
  mux.initPins();
}

void loop() {
  Serial.println(mux.readMux(15));

  Serial.println(mux(12)); // using overloaded operator ()

  delay(5000);
}
5
  • Thank you, KIIV. However, this initialization is based on the original library for the device as well as an example at bildr.org/2011/02/cd74hc4067-arduino. There are 4 separate control pins the device needs to connect to on the Arduino, hence the second dimension of the array. Feb 12 '17 at 13:51
  • In the example there is declaration and initialization at once. In the class you have declaration in the class and initialization later. In the definition brackets means size (number of elements). Anywhere else it means accessing element at passed position (index)
    – KIIV
    Feb 12 '17 at 13:59
  • Thanks again, KIIV. I somewhat understand. I moved the initialization to the header file where it was first declared, then got undefined reference for the destructor. I commented-out the destructor in the header file and it compiled without errors. Not real sure I like the destructor being commented-out, though. Feb 12 '17 at 14:38
  • Concerning my last comment, I was missing the destructor in the .cpp file. I added it, and it compiled without errors. Feb 12 '17 at 14:44
  • @GregoryR.Pace I can imagine that code without any variable just by using template class. It's perfect if you have one instance (or two). For more instances with different template parameters it might take more code space.
    – KIIV
    Feb 12 '17 at 17:01
-1

You need , at the channel 15 line too.

2
  • Thank you, Andreas. I just added the comma, but still getting the same error. Feb 12 '17 at 11:45
  • 1
    That last comma is optional.
    – Nick Gammon
    Feb 13 '17 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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