I have interfaced a height sensor and a weight sensor. The I2C address of the height and weight sensors are 0x29 and 0x2A respectively. I have merged the example of both the sensors.
This is the first time I am working with I2C and need help on this.
Do let me know about any mistakes or any sort of example which can help me to learn coding different I2C addresses, sensors, and interfacing.
#include <Wire.h>
#include "SparkFun_VL53L1X.h"
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
NAU7802 myScale;
SFEVL53L1X distanceSensor;
//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
#define INTERRUPT_PIN 3
void setup() {
Serial.begin(9600);
Wire.begin(0x2A);
if (myScale.begin() == false) {
Serial.println("Scale not detected. Please check wiring. Freezing...");
while (1);
}
Serial.println("Scale detected!");
Wire.begin(0x29);
if (distanceSensor.begin() != 0) { //Begin returns 0 on a good init
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
while (1) ;
}
Serial.println("Sensor online!");
}
// put your setup code here, to run once:
void loop() {
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
while (!distanceSensor.checkForDataReady()) {
delay(1);
}
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
distanceSensor.clearInterrupt();
distanceSensor.stopRanging();
Serial.print("Distance(mm): ");
Serial.print(distance);
float distanceInches = distance * 0.0393701;
float distanceFeet = distanceInches / 12.0;
Serial.print("\tDistance(ft): ");
Serial.print(distanceFeet, 2);
Serial.println();
if(myScale.available() == true) {
long currentReading = myScale.getReading();
Serial.print("Reading: ");
Serial.println(currentReading);
}
}