Sensor Package Not Functioning

Hey there. I'm in the competance range where I know enough to be dangerous to myself, but not enough to get out of a problem. We're wanting to do recordings of various factors in the project here and output them to serial. Individually everything works, but it seems we can't get it to function properly when we put all of the parts together. I think it has something to do with my I2C code. I was hoping someone would be able to help figure this out, and maybe help give some advice on how to make the code more efficient. My programming prior to this is limited to Qbasic, and TI graphing calculators. Thanks!


```cpp
#include <DFRobot_LIS2DW12.h>
#include <LiquidCrystal_I2C.h>
#include <RunningAverage.h>
#include <SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h>

NAU7802 myScale; // Create instance of the NAU7802 class
//Accelerometer
  //When using I2C communication, use the following program to construct an object by DFRobot_LIS2DW12_I2C
  /*!
   * @brief Constructor 
   * @param pWire I2c controller
   * @param addr  I2C address(0x18/0x19)
   */
  DFRobot_LIS2DW12_I2C acce(&Wire,0x18);

  float x;
  float y;
  float z;
  float pitch;
  float roll;

//Tachometer
  // Pin Definitions
  const byte irSensorPin = 3; // IR sensor connected to pin 18 (interrupt 5 on Mega)

  // Constants
  const byte teethPerRevolution = 20;
  const unsigned long measurementInterval = 1000; // 1 second

  // Variables
  volatile unsigned int pulseCount = 0;
  unsigned long lastMeasurementTime = 0;
  float rpm;

//Flow Meter and Pressure sensor

  /*
  Setup the pressure sensor
  Define the number of samples to keep track of. The higher the number, the
  more the readings will be smoothed, but the slower the output will respond to
  the input. Using a constant rather than a normal variable lets us use this
  value to determine the size of the readings array.
  */
  const int numReadings = 100;


  int readings[numReadings];  // the readings from the analog input
  int readIndex = 0;          // the index of the current reading
  int total = 0;              // the running total
  int average = 0;            // the average
  float Press_cal = 9.18;      // Pressure sensor calibration number
  int PressureSensor = A0;

  /*
  Set up display
  */

  LiquidCrystal_I2C lcd(0x27,20,4);
  //Use 5V power and ground as well as SDA and SCL

  /*
  Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

  Measure the liquid/water flow rate using this code. 
  Connect Vcc and Gnd of sensor to arduino, and the 
  signal line to arduino digital pin 2.
 
  */

  byte statusLed    = 13;

  byte sensorInterrupt = 0;  // 0 = digital pin 2
  byte sensorPin       = 2;

  // The hall-effect flow sensor outputs approximately 24.98 pulses per second per
  // GPM of flow.
  float calibrationFactor = 24.98;

  volatile byte pulseCountflow;  

  float flowRate;
  unsigned int flowMilliLitres;
  float totalMilliLitres;

  unsigned long oldTime;

//Setup Loadcell

  //Set up Running Average

  RunningAverage myRA(16);

   // Create instance of the NAU7802 class
  float weight;
  
void setup(void){

Serial.begin(9600);

//Set up LCD Display
  lcd.init();
  lcd.clear();         
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Flow: ");
  lcd.setCursor(0, 1);
  lcd.print("Tension: ");
  lcd.setCursor(0, 2);
  lcd.print("Pitch: ");
  lcd.setCursor(0, 3);
  lcd.print("Speed: ");

//Accelerometer
  x = 0;
  y = 0;
  z = 0;
  pitch = 0;
  roll = 0;
  while(!acce.begin()){
     Serial.println("Communication failed, check the connection and I2C address setting when using I2C communication.");
     delay(1000);
  }
  Serial.print("chip id : ");
  Serial.println(acce.getID(),HEX);
  //Chip soft reset
  acce.softReset();
  //Set whether to collect data continuously
  acce.continRefresh(true);
 
  acce.setDataRate(DFRobot_LIS2DW12::eRate_50hz);
  acce.setRange(DFRobot_LIS2DW12::e2_g);
  acce.setFilterPath(DFRobot_LIS2DW12::eLPF);
  acce.setFilterBandwidth(DFRobot_LIS2DW12::eRateDiv_4);
  acce.setPowerMode(DFRobot_LIS2DW12::eContLowPwrLowNoise2_14bit);
  delay(100);

//Tachometer
  pinMode(irSensorPin, INPUT_PULLUP); // Pull-up if your IR sensor has open-collector output
  attachInterrupt(digitalPinToInterrupt(irSensorPin), countPulse, FALLING); // Trigger on falling edge
  pinMode(11,OUTPUT);
  digitalWrite(11,HIGH);
  float rpm = 0.0;

//Flowmeter and Pressure Sensor
//Set up Pressure Sensor
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
   
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCountflow        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

//Load Cell
//Wire.begin(); // Join I2C bus
float weight = 0.0;

  //if (!myScale.begin()) {
   // Serial.println("Scale not detected. Check wiring. Freezing...");
   // while (1); // Freeze if no connection
 // }

  Serial.println("Scale detected!");

  // Optional: set up the scale
  myScale.setGain(128);          // Set gain (options: 1, 2, 4, 8, 16, 32, 64, 128)
  myScale.calibrateAFE();        // Internal calibration
  
}

void loop(void){
  //Accelerometer
  //Request a measurement under single data conversion on demand mode
    //acce.demandData();
    //The mearsurement range is ±2g,±4g,±8g or ±16g, set by the setRange() function.
    //Read the acceleration values
    x = acce.readAccX();
    y = acce.readAccY();
    z = acce.readAccZ();
    //Calculate pitch and roll
    roll = atan2(y , z) * 57.3;
    pitch = atan2((- x) , sqrt(y * y + z * z)) * 57.3;

 //Tachometer
   unsigned long currentTime = millis();

  if (currentTime - lastMeasurementTime >= measurementInterval) {
    noInterrupts(); // Prevent pulseCount from being updated while we're reading it
    unsigned int pulses = pulseCount;
    pulseCount = 0;
    interrupts();

    float revolutions = pulses / float(teethPerRevolution);
    float rpm = revolutions * 60.0;

    lastMeasurementTime = currentTime;
  }

  //Flow Meter
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = ((analogRead(PressureSensor)-86)/Press_cal);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  
   
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCountflow) / calibrationFactor;
    
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
    
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60)*100;
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
      
    unsigned int frac; 

    // Reset the pulse counter so we can start incrementing again
    pulseCountflow = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }

  //Load Cell
  if (myScale.available()) {
    float weight = myScale.getWeight(); // If you've calibrated, get weight in grams
  }
//Display Values and output to Cereal haha
lcd.setCursor(9, 0);
lcd.print(flowRate);
lcd.print("    ");
lcd.setCursor(9, 1);
lcd.print(weight);
lcd.print("    ");
lcd.setCursor(9, 2);
lcd.print(pitch);
lcd.print("    ");
lcd.setCursor(9, 3);
lcd.print(rpm);
lcd.print("    ");
Serial.print(average);
Serial.print(",");
Serial.print(flowRate);
Serial.print(",");
Serial.print(rpm);
Serial.print(",");
Serial.print(weight);
Serial.print(",");
Serial.print(pitch);
Serial.print(",");
Serial.print(roll);
Serial.println(",");

delay(250);
}
//Interrupt routines
//Tachometer

void countPulse() {
  pulseCount++;
}

//Flow Meter
void pulseCounter()
{
  // Increment the pulse counter
  pulseCountflow++;
}

Do you have links to the sources of your proprietary libraries? I found multiple sources for DFRobot/LIS. For example, GitHub - cdjq/DFRobot_LIS2DW12

You comment-out the initialization of myScale(), but then try using the instance... which causes issues.

Thanks for that. I realized that I had commented that out after I had put it up. I was trying to not have the loadcell connected to try to troubleshoot stuff but it was freezing up trying to find it.

I am using the same library you linked there. The LIS2DW12 library.

https://docs.arduino.cc/libraries/liquidcrystal/

If you are willing... fall-back to when everything worked. Move step-by-step from there, toward your goal. You can assume the LCD is good with anything (maybe)... and all I see are an accelerometer, a scale, a LCD display... but as you mentioned, I2C could have conflict.

Start with a I2C scanner... Here is the one I use (slightly modified from the others):

#include <Wire.h>
int address, deviceFound,  error;

void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.print("I2C bus device addresses:");

  for (address = 0; address < 128; address++ )  {
    Wire.beginTransmission(address);
    if (!Wire.endTransmission()) {
      showHex(address);
      deviceFound++;
    }
    else if (error == 4) {
      Serial.print(" Error at");
      showHex(address);
    }
  }
  if (!deviceFound)
    Serial.println(" No I2C devices found.");
}

void loop() {}

void showHex(int hex) {
  Serial.print(" 0x");
  if (hex < 0x10)
    Serial.print(0);
  Serial.print(hex, HEX);
}

However you proceed, keep asking questions until you find the solution.

An excellent strategy, colloquially known as divide and conquer, which amuses because you are in the mode of putting things together…

But when you don't have something the code expects, you can get in trouble, so you need to remove not just the part that complains, but anything that attempts to use the loadcell.

Here's where having all the loadcell stuff in a separate function or even just a separate section of code can make having/not having something be part of the current effort easier.

a7