Help needed with multiplexer sketch

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Help needed with multiplexer sketch

Post by davidestevens »

hi,

I've spent the morning slowly modifying the multiplexer sketch from the TCA9548A page, replacing the Magnetometer with code form the VL6180X sketch, and appear to have gotten rid of all the error messages except one, and I have no idea what's missing.

I've tried searching this error message on the Arduino site, but all the replies are specific to the questions, and not what the error message means generally.
I'd be really grateful if anyone could point out what's wrong, and also if I've added in the new code correctly. (I've eliminated all of the sketch test messages for now, to make the sketch more readable for me.)

The error is: error: expected primary-expression before '}' token
}

and the code:

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_VL6180X.h>

#define TCAADDR 0x70

/* Assign a unique ID to this sensor at the same time */
Adafruit_VL6180X vl1 = Adafruit_VL6180X(1);
Adafruit_VL6180X vl2 = Adafruit_VL6180X(2);


void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}



void setup(void) 
{
  Serial.begin(115200);
    Serial.println("Sensor Test"); Serial.println("");
 
   // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }
  
  /* Initialise the 1st sensor */
  tcaselect(0);
  if(!vl1.begin())
  
  
  /* Initialise the 2nd sensor */
  tcaselect(1);
  if(!vl2.begin())
 
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  
  tcaselect(0);
  
  {
  float lux = vl1.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);
  
  uint8_t range = vl1.readRange();
  uint8_t status = vl1.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }
  }
  
  tcaselect(1);
 
  {
  float lux = vl2.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);
  
  uint8_t range = vl2.readRange();
  uint8_t status = vl2.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }
  
  delay(100);
}
  }

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

This code at the end of setup() is incomplete:

Code: Select all

  /* Initialise the 1st sensor */
  tcaselect(0);
  if(!vl1.begin())
  
  
  /* Initialise the 2nd sensor */
  tcaselect(1);
  if(!vl2.begin())
 
}
You haven't provided bodies for the two conditionals. Though tcaselect(1); is syntactically the body for the first conditional, so the compiler is happy with it, and is complaining about the lack of a body for the second conditional before the function is closed (by the } )

Dave

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

Thanks for the input. Unfortunately, my understanding of Arduino programming is very limited, and though I get the gist of what you're saying, it doesn't help me sort this out.
But let's see what I can figure out...

line 12 starts transmission of values for pins 0-7 on the multiplexer at address 0x70 (this is from the published Magnetometer sketch).

(I'm using SCL/SDA 0 & 1 on the multiplexer.)

line 33 initialises SCL/SDA 0 (though I don't understand what it means to initialise that connection - is it saying "look for data from this pin"?) and it's referring back to the definition of variable vl1 near the top of the sketch)
line 38 does the same for SCL/SDA 1 (for variable vl2)

Is the "body" the bits of code inside loop?

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

Here's a C++ tutorial https://cplusplus.com/doc/tutorial/.

Having some understanding of the language will make the process easier and less error prone.

At the very least you'll want to do something like:

Code: Select all

  /* Initialise the 1st sensor */
  tcaselect(0);
  if(!vl1.begin()) {
    Serial.println("v1 failed to initialize");
    while(1);
  }
  
  /* Initialise the 2nd sensor */
  tcaselect(1);
  if(!vl2.begin()) {
    Serial.println("v2 failed to initialize");
    while(1);
  }
}
That will print a message if the initialization of one of them fails and then sit in a loop until restarted.

The key thing is that there is a block of code that is conditionally executed (i.e. if a sensor doesn't initialize) for each if conditional.

The begin() calls initialize the sensors connected to the selected I2C lines. What that means depends on what is connected, but it's whatever initialization is required to have the sensor/device set up and ready to use.

Dave

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

> The key thing is that there is a block of code that is conditionally executed (i.e. if a sensor doesn't initialize) for each if conditional.

Ah, that makes sense. Thanks for that.
And thanks for the link to the C++ tutorial; I'll start to look at that this afternoon.

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

Hmm. Reading the C++ docs, I'm getting a better understanding of how the sketch works, but I still can't get it to initialise properly.

Can I check - in the lines :
Adafruit_VL6180X vl1 = Adafruit_VL6180X(1);
Adafruit_VL6180X vl2 = Adafruit_VL6180X(2);
am I right in assuming that the numbers in the final brackets refer to the physical pin sets on the board? (ie ports 1 & 2).
So,
tcaselect(1) calls the tcaselect function with value 1, and then

Code: Select all

if(!vl1.begin()) {
    Serial.println("v1 failed to initialize");
    while(1);
This translates as "if you don't start receiving values from port 1, then print the error message".
So the port is not actually sending any data?

I can't see anything in the setup that is wrong (setup code below). But I keep getting the "v1 failed to initialize" message.
To make sure the the physical connections were all working, I uploaded the port scanner sketch from the Multiplexer page, and that can see the multiplexer ports and the two VL6180X attached to those ports.

I don't know if this is relevant, but this is displayed when I upload the main sketch:
Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
Device code: 0x44

I'm really not sure what to do next. I've looked at the Working with Multiple I2C code, and I may try that next; but it would be useful to figure out what's going wrong here.

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_VL6180X.h>

#define TCAADDR 0x70

/* Assign a unique ID to this sensor at the same time */
Adafruit_VL6180X vl1 = Adafruit_VL6180X(1);
Adafruit_VL6180X vl2 = Adafruit_VL6180X(2);


void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}


void setup(void) 
{
  Serial.begin(9600);
    Serial.println("Sensor Test"); Serial.println("");
 
   // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(200);
  }
  
  /* Initialise the 1st sensor */
  tcaselect(1);
  if(!vl1.begin()) {
    Serial.println("v1 failed to initialize");
    while(1);
  }
   
  /* Initialise the 2nd sensor */
  tcaselect(2);
  if(!vl2.begin()) {
    Serial.println("v2 failed to initialize");
    while(1);
  }
}

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

In these lines you shouldn't be passing an argument, assuming you are using the default I2C address on the sensors:

Code: Select all

Adafruit_VL6180X vl1 = Adafruit_VL6180X(1);
Adafruit_VL6180X vl2 = Adafruit_VL6180X(2);
So they should be:

Code: Select all

Adafruit_VL6180X vl1 = Adafruit_VL6180X();
Adafruit_VL6180X vl2 = Adafruit_VL6180X();
Documentation for the VL6180X library is here: https://adafruit.github.io/Adafruit_VL6 ... 180_x.html
This translates as "if you don't start receiving values from port 1, then print the error message".
No, it's "if you can't initialize the connection to the sensor, then print the error message". You only get values from the sensor when you ask for them.

What is the Programmer menu option (Tools menu) set to?? And what board are you using?

Dave

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

Tools/Programmer/ - nothing is actually selected (ticked)
Board - an Arduino Micro

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

Really? Can you post a screenshoot of the menu? I'm pretty sure there has to be something set.

Dave

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

ok, dumb question. How do I get a screenshot on my Desktop into this message box? The Insert Image option wants a URL - do I have to put the image online somewhere?
[edit] huh. I seem to have accidentally uploaded it twice by drag/dropping.
Attachments
Screenshot 2022-10-26 at 18.22.51.png
Screenshot 2022-10-26 at 18.22.51.png (175.35 KiB) Viewed 176 times
Screenshot 2022-10-26 at 18.22.51.png
Screenshot 2022-10-26 at 18.22.51.png (175.35 KiB) Viewed 176 times

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

Try selecting AVR ISP or AVR ISP mkII

Dave

User avatar
davidestevens
 
Posts: 35
Joined: Thu Oct 20, 2022 7:44 am

Re: Help needed with multiplexer sketch

Post by davidestevens »

So I began again, this time starting from the "Working with Multiple Same Address..." sketch from Carter Nelson, and adding the VL6180X code to a modified version of that. And lo! It works!
And I have the data usefully arriving in Max, so next I just have to add in the active terminators and I can start making the housings and so on.

Thanks so much Dave (dastels) for your help and patience as I tried to figure this out.

Here's the sketch (I have code for sensors on the first three ports of the Multiplexer, but I'm only using 2 at the moment). There may be unnecessary lines in there (I'm not sure if I explicitly need to call Wire in this sketch, but it was a part of the sketch I started from, so I left it in).

Code: Select all


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_VL6180X.h>

#define TCAADDR 0x70

// For each device, create a separate instance.
Adafruit_VL6180X vl1 = Adafruit_VL6180X();  // VL6180X #1
Adafruit_VL6180X vl2 = Adafruit_VL6180X();  // VL6180X #2
Adafruit_VL6180X vl3 = Adafruit_VL6180X();  // VL6180X0 #3

// Helper function for changing TCA output channel
void tcaselect(uint8_t channel) {
  if (channel > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << channel);
  Wire.endTransmission();  
}

void setup() {
  Serial.begin(57600);
  while(!Serial);
  Serial.println(F("Two VL6180X Example"));

  // NOTE!!! VERY IMPORTANT!!!
  // Must call this once manually before first call to tcaselect()
  Wire.begin();
  
  // Before using any BME280, call tcaselect to set the channel.
  tcaselect(0);      // TCA channel for bme1
  vl1.begin();      // use the default address of 0x77

  tcaselect(1);      // TCA channel for bme2
   vl2.begin();      // use the default address of 0x77

  tcaselect(2);      // TCA channel for bme3
  vl3.begin();      // use the default address of 0x77
}


void loop() {


 tcaselect(1);  
  float lux = vl2.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux1: "); Serial.println(lux);
  
  uint8_t range = vl2.readRange();
  uint8_t status = vl2.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range1: "); Serial.println(range);
    }

tcaselect(2);  
  float lux2 = vl3.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux2: "); Serial.println(lux2);
  
  uint8_t range2 = vl3.readRange();
  uint8_t status2 = vl3.readRangeStatus();

  if (status2 == VL6180X_ERROR_NONE) {
    Serial.print("Range2: "); Serial.println(range2);
    }    
}

User avatar
dastels
 
Posts: 15817
Joined: Tue Oct 20, 2015 3:22 pm

Re: Help needed with multiplexer sketch

Post by dastels »

That's great!

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”