Hello again,
I have a Sparkfun thing plus c connected to a SparkFun qwiic I2C MUX, which connected with multiple SparkFun qwiic LSM6DSO.
When I using the following code, the sample values from the LSM6DSO are constantly 0.0, although properly initialization of the MUX and all of the LSM6DSO.
Can you please help me fix that problem?
#include <SPI.h>
#include <SD.h>
#include "SparkFunLSM6DSO.h"
#include <Wire.h>
#include <Arduino.h>
#define MUX_ADDR 0x70 //7-bit unshifted default I2C Address
const int numIMUs = 2;
LSM6DSO imus[numIMUs];
bool logging = false; // Define the logging flag
const int sd_cs = 5; //Thing Plus C SD card pin
String dataMessage;
File file;
//Enables a specific port number
void enableMuxPort(byte portNumber)
{
if (portNumber > 7) portNumber = 7;
//Read the current mux settings
Wire.requestFrom(MUX_ADDR, 1);
if (!Wire.available()) return; //Error
byte settings = Wire.read();
//Set the wanted bit to enable the port
settings |= (1 << portNumber);
//Write the updated settings
Wire.beginTransmission(MUX_ADDR);
Wire.write(settings);
Wire.endTransmission();
}
//Disables a specific port number
void disableMuxPort(byte portNumber)
{
if (portNumber > 7) portNumber = 7;
//Read the current mux settings
Wire.requestFrom(MUX_ADDR, 1);
if (!Wire.available()) return; //Error
byte settings = Wire.read();
//Clear the wanted bit to disable the port
settings &= ~(1 << portNumber);
//Write the updated settings
Wire.beginTransmission(MUX_ADDR);
Wire.write(settings);
Wire.endTransmission();
}
void setup() {
Serial.begin(115200);
delay(500);
if (!SD.begin(sd_cs)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
Wire.begin();
delay(10);
//Disable all eight mux ports initially, then we can enable them one at a time
for (byte x = 0 ; x <= 7 ; x++)
{
disableMuxPort(x);
}
//Initialize all the sensors
for (byte i = 0 ; i < numIMUs ; i++)
{
enableMuxPort(i); //Tell mux to connect to port X
if (imus[i].begin()) {
Serial.print("IMU");
Serial.print(i + 1);
Serial.println(" is Ready.");
} else {
Serial.print("Could not connect to IMU");
Serial.println(i + 1);
Serial.println("Freezing");
}
disableMuxPort(i);
}
file = SD.open("/datab.txt", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print("Reading IMUS \r\n")) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
Serial.println("Waiting for command: send 's' to start logging data");
}
void loop() {
if (Serial.available() > 0) {
// If there is data available from the serial port
char command = Serial.read();
if (command == 'S' || command == 's') {
// Start logging data when 'S' or 's' is received
Serial.println("Logging started.");
Serial.println("To end logging data: send 'e'");
// myLog.syncFile();
logging = true; // Set a flag to indicate logging has started
} else if (command == 'E' || command == 'e') {
// Stop logging data when 'E' or 'e' is received
Serial.println("Logging stopped.");
logging = false; // Set the flag to indicate logging has stopped
file.close();
}
}
if (logging) {
for (byte i = 0; i < numIMUs; i++) {
enableMuxPort(i);
// Read data from the current IMU
dataMessage = "IMU" + String(i + 1) + " AccX: " + String(imus[i].readFloatAccelX()) + "\r\n";
Serial.println(dataMessage);
file.print(dataMessage);
// Calculate the timestamp in seconds
unsigned long currentMicros = micros();
float timestampInSeconds = currentMicros / 1000000.0;
dataMessage = "Timestamp: " + String(timestampInSeconds) + "\r\n";
Serial.println(dataMessage);
file.print(dataMessage);
disableMuxPort(i);
// Continue to read and log data from the current IMU as needed
delay(1);
// delayMicroseconds(9615);
}
}
}
Thank you!