Arduino IoT Cloud code merging not showing data

Hello,
So I have merged 3 libraries to monitor the data of various sensors. So far merging 2 of them worked fine ( rotary encoder and bathroom scale). Now when I merged the third one ( inclinometer) the first two are still working as they should but the Inclinometer doesn't. It sort of works but it doesn't....the code when installed on its own in Arduino Nano 33 IoT work fine. But after it's been merged with the other two into the Arduino IoT Cloud it starts and does the self-calibration and it prints on the serial monitor x =0, y=0, z=0, and then it reads the sensor and captures some data then freezes the data and shows only that data over and over without reacting when I move the inclinometer.

  Serial1.begin(115200, SERIAL_8N1);
  // Special unlock/enable thing (not documented anywhere!)
  byte data0[] = { 0xFF, 0xF0, 0xF0, 0xF0, 0xF0 };
  Serial1.write(data0, 5);
  delay(2);
  // Unlock configuration
  byte data1[] = { 0xFF, 0xAA, 0x69, 0x88, 0xB5 };
  Serial1.write(data1, 5);
  delay(2);
  // Enable calibration mode
  byte data2[] = { 0xFF, 0xAA, 0x01, 0x01, 0x00 };
  Serial1.write(data2, 5);
  delay(5000);  // Wait for calibration to finish
  // Save configuration
  byte data3[] = { 0xFF, 0xAA, 0x00, 0x00, 0x00 };
  Serial1.write(data3, 5);```

Serial monitor readings just after the calibration:

```09:31:06.197 -> Setup scale with no weight on it. Press a key when ready.
09:31:29.713 -> ***** Arduino IoT Cloud - configuration info *****
09:31:29.713 -> Device ID: aa7010e9-7086-45a7-b085-972e52b7c8d2
09:31:29.713 -> MQTT Broker: mqtts-sa.iot.arduino.cc:8883
09:31:29.713 -> WiFi.status(): 0
09:31:29.713 -> Current WiFi Firmware: 1.5.0
09:31:29.753 -> Reading: 203943	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.753 -> Angle
09:31:29.753 -> 	X: 0.00
09:31:29.753 -> 	Y: 0.00
09:31:29.753 -> 	Z: 0.00
09:31:29.791 -> Reading: 204022	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.791 -> Reading: 203381	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.837 -> Reading: 204234	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.884 -> Reading: 203447	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.884 -> Reading: 204062	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.922 -> Reading: 204081	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.963 -> Reading: 203222	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:29.963 -> Reading: 203843	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.007 -> Reading: 203292	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.042 -> Reading: 203909	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.042 -> Reading: 203716	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.088 -> Reading: 203611	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.120 -> Reading: 203876	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.167 -> Reading: 203151	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.167 -> Reading: 204258	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.204 -> Reading: 203297	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:30.247 -> Reading: 203478	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:34.768 -> Connected to "BTHub6-Q73T"
09:31:35.479 -> Reading: 203411	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:35.479 -> Angle
09:31:35.479 -> 	X: 0.00
09:31:35.479 -> 	Y: 0.00
09:31:35.479 -> 	Z: 0.00
09:31:35.558 -> Reading: 203570	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:37.804 -> Reading: 203913	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:37.804 -> Angle
09:31:37.804 -> 	X: 0.00
09:31:37.804 -> 	Y: 0.00
09:31:37.804 -> 	Z: 0.00
09:31:37.804 -> 	Y: 0.00
09:31:37.804 -> 	Z: 0.00
09:31:37.883 -> Reading: 203696	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.404 -> Reading: 204147	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.451 -> Reading: 204292	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.498 -> Reading: 204157	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.544 -> Reading: 204396	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.590 -> Reading: 203719	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.631 -> Reading: 204058	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.668 -> Reading: 203977	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.713 -> Reading: 203446	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.746 -> Reading: 203951	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.778 -> Reading: 203228	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.826 -> Reading: 203527	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.864 -> Reading: 203192	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.911 -> Reading: 203545	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.957 -> Reading: 203266	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:38.998 -> Reading: 203497	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.034 -> Reading: 203590	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.080 -> Reading: 203601	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.121 -> Reading: 203998	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.158 -> Reading: 203545	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.158 -> Angle
09:31:39.158 -> 	X: -0.82
09:31:39.158 -> 	Y: 0.33
09:31:39.158 -> 	Z: -51.08
09:31:39.206 -> Reading: 203796	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.252 -> Reading: 203781	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.298 -> Reading: 203984	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.330 -> Reading: 203881	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.376 -> Reading: 203632	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.415 -> Reading: 204211	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.462 -> Reading: 204144	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.493 -> Reading: 203863	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.541 -> Reading: 204038	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.588 -> Reading: 204013	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.632 -> Reading: 203940	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.665 -> Reading: 203319	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.712 -> Reading: 203342	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:39.758 -> Reading: 203437	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.254 -> Connected to Arduino IoT Cloud
09:31:40.254 -> Thing ID: c02cbdc0-93b7-4554-b27e-ad6eb537ea60
09:31:40.254 -> Reading: 202988	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.286 -> Reading: 203227	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.360 -> Reading: 203262	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.395 -> Reading: 203563	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.427 -> Reading: 203688	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.474 -> Reading: 203571	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.520 -> Reading: 203808	Weight: inf	AvgWeight: inf	Scale not calibrated. Press 'c'.
09:31:40.520 -> Angle
09:31:40.520 -> 	X: -0.82
09:31:40.520 -> 	Y: 0.33
09:31:40.520 -> 	Z: -51.08

This is the code:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/c02cbdc0-93b7-4554-b27e-ad6eb537ea60 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudAngle rr_camber;
  CloudAngle rr_caster_variable;
  CloudAngle rr_turn_angle;
  CloudLength rear_track;
  CloudMass right_rear_weight;
  bool redLED;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
//Encoder
#include <Encoder.h>
#define A_PIN 2
#define B_PIN 3
Encoder rotary(A_PIN, B_PIN);
#define rstbtn 4     // reset button
long counter = 0;
const float R = 0.15;
float distance = 0;

//Scale
#include <Wire.h>
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_NAU8702
NAU7802 myScale; //Create instance of the NAU7802 class
bool settingsDetected = false; //Used to prompt user to calibrate their scale
//Create an array to take average of weights. This helps smooth out jitter.
#define AVG_SIZE 4
float avgWeights[AVG_SIZE];
byte avgWeightSpot = 0;
//float avgWeight = 5;

/*
Inclinometer
JY901 TX / RX pins to Nano 33 IoT RX /TX pins respectively!
*/
#include <witmotion_uart.h>
#define JY901_PORT Serial1
// Make sensor's class instance
JY901_UART JY901(JY901_PORT);

//LED
int myLED = 5;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(115200);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  
  // Defined in thingProperties.h
  initProperties();
  
  //Encoder
  pinMode(rstbtn, INPUT_PULLUP);
  
  //Scale
  Wire.begin();
  Wire.setClock(400000); //Qwiic Scale is capable of running at 400kHz if desired
  

  if (myScale.begin() == false)
  {
    Serial.println("Scale not detected. Please check wiring. Freezing...");
    while (1);
  }
  Serial.println("Scale detected!");

  //readSystemSettings(); //Load zeroOffset and calibrationFactor from EEPROM

  myScale.setSampleRate(NAU7802_SPS_320); //Increase to max sample rate
  myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel 

  Serial.print("Zero offset: ");
  Serial.println(myScale.getZeroOffset());
  Serial.print("Calibration factor: ");
  Serial.println(myScale.getCalibrationFactor());
  
  //Inclinometer sensor calibration
  Serial1.begin(115200, SERIAL_8N1);
  // Special unlock/enable thing (not documented anywhere!)
  byte data0[] = { 0xFF, 0xF0, 0xF0, 0xF0, 0xF0 };
  Serial1.write(data0, 5);
  delay(2);
  // Unlock configuration
  byte data1[] = { 0xFF, 0xAA, 0x69, 0x88, 0xB5 };
  Serial1.write(data1, 5);
  delay(2);
  // Enable calibration mode
  byte data2[] = { 0xFF, 0xAA, 0x01, 0x01, 0x00 };
  Serial1.write(data2, 5);
  delay(5000);  // Wait for calibration to finish
  // Save configuration
  byte data3[] = { 0xFF, 0xAA, 0x00, 0x00, 0x00 };
  Serial1.write(data3, 5);

  //Inclinometer - starting tx/rx
  Serial1.begin(115200);
  //Inclinometer init Serial port and class instance
  JY901_PORT.begin(115200);
  JY901.begin();
  
  //LED
  pinMode(myLED,OUTPUT);
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
 
 //Encoder
  static long lastCounter = 0;
  long currentCounter = rotary.read()>> 2;

  if (lastCounter != currentCounter) {
    distance = R * currentCounter;
    Serial.print("distance: ");
    Serial.println(distance);
    lastCounter = currentCounter;
  }

  if (digitalRead(rstbtn) == LOW) {
    rotary.write(0);
    lastCounter = 0;
  }
  
  rear_track = distance;
  
  //Scale
    if (myScale.available() == true)
  {
    long currentReading = myScale.getReading();
    float currentWeight = myScale.getWeight();

    Serial.print("Reading: ");
    Serial.print(currentReading);
    Serial.print("\tWeight: ");
    Serial.print(currentWeight, 2); //Print 2 decimal places

    avgWeights[avgWeightSpot++] = currentWeight;
    if(avgWeightSpot == AVG_SIZE) avgWeightSpot = 0;

    float avgWeight = 0;
    for (int x = 0 ; x < AVG_SIZE ; x++)
      avgWeight += avgWeights[x];
    avgWeight /= AVG_SIZE;

    Serial.print("\tAvgWeight: ");
    Serial.print(avgWeight, 2); //Print 2 decimal places

    if(settingsDetected == false)
    {
      Serial.print("\tScale not calibrated. Press 'c'.");
    }

    Serial.println();
    
    right_rear_weight = avgWeight;
  }

  if (Serial.available())
  {
    byte incoming = Serial.read();

    if (incoming == 't') //Tare the scale
      myScale.calculateZeroOffset();
    else if (incoming == 'c') //Calibrate
    {
      calibrateScale();
    }
  }
  
  //Inclinometer
  static uint32_t prevReportTime;

  // Parse incoming reports from the JY901 module
  JY901.readReport();

  if (millis() - prevReportTime >= 1333UL) {
    prevReportTime = millis();

    // Content -> Angle
    wmAngles_t wmAngles = JY901.getAngles();
    Serial.println("Angle");
    Serial.print("\tX: ");
    Serial.println(wmAngles.roll);  // .roll() eq .x()  for wmAngles struct
    Serial.print("\tY: ");
    Serial.println(wmAngles.y);  // .y() eq .pitch() for wmAngles struct
    Serial.print("\tZ: ");
    Serial.println(wmAngles.z);  // .y() eq .yaw()   for wmAngles struct
    
    rr_camber = wmAngles.roll;
    rr_caster_variable = wmAngles.y;
    rr_turn_angle = wmAngles.z;
  }
  
}

//Scale
//Gives user the ability to set a known weight on the scale and calculate a calibration factor
void calibrateScale(void)
{
  Serial.println();
  Serial.println();
  Serial.println(F("Scale calibration"));

  Serial.println(F("Setup scale with no weight on it. Press a key when ready."));
  while (Serial.available()) Serial.read(); //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10); //Wait for user to press key

  myScale.calculateZeroOffset(64); //Zero or Tare the scale. Average over 64 readings.
  Serial.print(F("New zero offset: "));
  Serial.println(myScale.getZeroOffset());

  Serial.println(F("Place known weight on scale. Press a key when weight is in place and stable."));
  while (Serial.available()) Serial.read(); //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10); //Wait for user to press key

  Serial.print(F("Please enter the weight, without units, currently sitting on the scale (for example '4.25'): "));
  while (Serial.available()) Serial.read(); //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10); //Wait for user to press key

  //Read user input
  float weightOnScale = Serial.parseFloat();
  Serial.println();

  myScale.calculateCalibrationFactor(weightOnScale, 64); //Tell the library how much weight is currently on it
  Serial.print(F("New cal factor: "));
  Serial.println(myScale.getCalibrationFactor(), 2);

  Serial.print(F("New Scale Reading: "));
  Serial.println(myScale.getWeight(), 2);
}

/*
  Since RedLED is READ_WRITE variable, onRedLEDChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRedLEDChange()  {
  // Add your code here to act upon RedLED change
  Serial.println(redLED);
  if(redLED){
    digitalWrite(myLED,HIGH);
  } else{
    digitalWrite(myLED,LOW);
    }
  }

Hi @bobeto !

Thanks for sharing your issue.

I'm not sure if I understood it completely. Have your problems started when you included the IoT Cloud library or when you included the Inclinometer library?

In order to isolate the issue, I would suggest testing everything without including the IoT Cloud library first.

It started after I merged the inclinometer library. I resolved the issue for now with another inclinometer library and a few other updates in the code. As you can see I am using now external EEPROM to store the scale calibration factor and a push button to tare the scale.
So far all is working fine.
What's left is to add a push button to zero one of the values ( Z axis ) from the inclinometer and after that, Ill add the Arduino Cloud code and see if everything is working. Actually, I am struggling with that push-button reset function for the inclinometer, so Ill start a new topic in the project guidance directory.

This is the code so far:

#include <JY901_Serial.h>

#include <SparkFun_External_EEPROM.h>
#include <Adafruit_GFX.h>

//Inclinometer
#include <JY901_Serial.h>
/*
Modified for Nano 33 IoT
JY901       Nano 33 IoT
  TX   <->   D1(Rx)
  RX   <->   D0(Tx)
  GND  <->    GND
  VCC  <->    VCC 
*/

//Encoder
#include <Encoder.h>
#define A_PIN 2
#define B_PIN 3
Encoder rotary(A_PIN, B_PIN);
#define rstbtn 4  // reset button
long counter = 0;
const float R = 0.15;
float distance = 0;

//Scale
//External EEPROM
#include <Wire.h>
ExternalEEPROM myMem;                                      //Qwiik Cable connected to Nano 33 IoT I2C as follow: Yellow to D19(SCL), Blue to D18(SDA), Red +3V3, Black to Ground!
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"  // Click here to get the library: http://librarymanager/All#SparkFun_NAU8702
NAU7802 myScale;                                           //Create instance of the NAU7802 class
//EEPROM locations to store 4-byte variables
#define LOCATION_CALIBRATION_FACTOR 0  //Float, requires 4 bytes of EEPROM
#define LOCATION_ZERO_OFFSET 10        //Must be more than 4 away from previous spot. Long, requires 4 bytes of EEPROM
bool settingsDetected = false;         //Used to prompt user to calibrate their scale
//Create an array to take average of weights. This helps smooth out jitter.
#define AVG_SIZE 4
float avgWeights[AVG_SIZE];
byte avgWeightSpot = 0;
#define tare 6  // tare button

//Display

#include <Adafruit_SSD1351.h>
#include <SPI.h>
// Screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 128

// You can use any (4 or) 5 pins
#define SCLK_PIN 13  //Yellow
#define MOSI_PIN 11  //Blue
#define DC_PIN 8     //Green
#define CS_PIN 10    //Orange
#define RST_PIN 9    //White

// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

// Option 1: use any pins but a little slower
Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(115200);
  delay(1500);  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found

  //Inclinometer
  Serial1.begin(115200, SERIAL_8N1);

  // Special unlock/enable thing (not documented anywhere!)
  byte data0[] = { 0xFF, 0xF0, 0xF0, 0xF0, 0xF0 };
  Serial1.write(data0, 5);
  delay(2);
  // Unlock configuration
  byte data1[] = { 0xFF, 0xAA, 0x69, 0x88, 0xB5 };
  Serial1.write(data1, 5);
  delay(2);
  // Enable calibration mode
  byte data2[] = { 0xFF, 0xAA, 0x01, 0x01, 0x00 };
  Serial1.write(data2, 5);
  delay(5000);  // Wait for calibration to finish
  // Save configuration
  byte data3[] = { 0xFF, 0xAA, 0x00, 0x00, 0x00 };
  Serial1.write(data3, 5);

  //Inclinometer
  Serial1.begin(115200);
  JY901.attach(Serial1);


  //Encoder
  pinMode(rstbtn, INPUT_PULLUP);

  //Scale
  Wire.begin();
  Wire.setClock(400000);  //Qwiic Scale is capable of running at 400kHz if desired
  pinMode(tare, INPUT_PULLUP);
  //EEPROM
  if (myMem.begin() == false) {
    Serial.println("No memory detected. Freezing.");
    while (1)
      ;
  }
  Serial.println("Memory detected!");

  Serial.print("Mem size in bytes: ");
  Serial.println(myMem.length());

  //Scale
  if (myScale.begin() == false) {
    Serial.println("Scale not detected. Please check wiring. Freezing...");
    while (1);
  }
  Serial.println("Scale detected!");

  readSystemSettings();  //Load zeroOffset and calibrationFactor from EEPROM
  myScale.setSampleRate(NAU7802_SPS_320);  //Increase to max sample rate
  myScale.calibrateAFE();                  //Re-cal analog front end when we change gain, sample rate, or channel
  Serial.print("Zero offset: ");
  Serial.println(myScale.getZeroOffset());
  Serial.print("Calibration factor: ");
  Serial.println(myScale.getCalibrationFactor());

  //Display
  tft.begin();
  uint16_t time = millis();
  tft.fillRect(0, 0, 128, 128, BLACK);
  time = millis() - time;
}

void loop() {
  //Inclinometer
  JY901.receiveSerialData();
  // print received data. Data was received in serialEvent;
  printAngle();

  // rr_camber = wmAngles.roll;
  // rr_caster_variable = wmAngles.y;
  // rr_turn_angle = wmAngles.z;
 

  //Encoder
  static long lastCounter = 0;
  long currentCounter = rotary.read() >> 2;

  if (lastCounter != currentCounter) {
    distance = R * currentCounter;
    Serial.print("D ");
    Serial.println(distance);
    Serial.print(", ");
    lastCounter = currentCounter;
  }
  if (digitalRead(rstbtn) == LOW) {
    rotary.write(0);
    lastCounter = 0;
  }
  //rear_track = distance;

  //Scale
  if (myScale.available() == true) {
    long currentReading = myScale.getReading();
    float currentWeight = myScale.getWeight();

    /*  Serial.print("Reading: ");
    Serial.print(currentReading);
    Serial.print("\tWeight: ");
    Serial.print(currentWeight, 2);*/
    //Print 2 decimal places

    avgWeights[avgWeightSpot++] = currentWeight;
    if (avgWeightSpot == AVG_SIZE) avgWeightSpot = 0;

    float rrweight = 0;
    for (int x = 0; x < AVG_SIZE; x++)
      rrweight += avgWeights[x];
    rrweight /= AVG_SIZE;

    Serial.print("\trrweight: ");
    Serial.print(rrweight, 2);  //Print 2 decimal places
    if (settingsDetected == false) {
      Serial.print("\tScale not calibrated. Press 'c'.");
    }
    Serial.println();

    //rr_weight = rrweight;
  }

  if (Serial.available()) {
    byte incoming = Serial.read();

    if (incoming == 't')  //Tare the scale
      myScale.calculateZeroOffset();
    else if (incoming == 'c')  //Calibrate
    {
      calibrateScale();
    }
}
if (digitalRead(tare) == LOW) {
    myScale.calculateZeroOffset();
  }
  //Display
  tftdata();
}

//Inclinometer
void printAngle() {
  Serial.print("Angle:");
  Serial.print(JY901.getRoll());  // .Roll() eq .x()
  Serial.print(" ");
  Serial.print(JY901.getPitch());  // .Pitch() eq .y()
  Serial.print(" ");
  Serial.print(JY901.getYaw());  // .Yaw() eq .z()
  Serial.print("\n");
}

//Scale
//Gives user the ability to set a known weight on the scale and calculate a calibration factor
void calibrateScale(void) {
  Serial.println();
  Serial.println();
  Serial.println(F("Scale calibration"));

  Serial.println(F("Setup scale with no weight on it. Press a key when ready."));
  while (Serial.available()) Serial.read();   //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10);  //Wait for user to press key

  myScale.calculateZeroOffset(64);  //Zero or Tare the scale. Average over 64 readings.
  Serial.print(F("New zero offset: "));
  Serial.println(myScale.getZeroOffset());

  Serial.println(F("Place known weight on scale. Press a key when weight is in place and stable."));
  while (Serial.available()) Serial.read();   //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10);  //Wait for user to press key

  Serial.print(F("Please enter the weight, without units, currently sitting on the scale (for example '4.25'): "));
  while (Serial.available()) Serial.read();   //Clear anything in RX buffer
  while (Serial.available() == 0) delay(10);  //Wait for user to press key

  //Read user input
  float weightOnScale = Serial.parseFloat();
  Serial.println();

  myScale.calculateCalibrationFactor(weightOnScale, 64);  //Tell the library how much weight is currently on it
  Serial.print(F("New cal factor: "));
  Serial.println(myScale.getCalibrationFactor(), 2);

  Serial.print(F("New Scale Reading: "));
  Serial.println(myScale.getWeight(), 2);

  writeSystemSettings();  //Commit these values to EEPROM
}

//Record the current system settings to EEPROM
void writeSystemSettings(void) {
  //Get various values from the library and commit them to NVM
  myMem.put(LOCATION_CALIBRATION_FACTOR, myScale.getCalibrationFactor());
  myMem.put(LOCATION_ZERO_OFFSET, myScale.getZeroOffset());
}

//Reads the current system settings from EEPROM
//If anything looks weird, reset setting to default value
void readSystemSettings(void) {
  float settingCalibrationFactor;  //Value used to convert the load cell reading to lbs or kg
  long settingZeroOffset;          //Zero value that is found when scale is tared

  //   //Look up the calibration factor
  myMem.get(LOCATION_CALIBRATION_FACTOR, settingCalibrationFactor);
  if (settingCalibrationFactor == 0xFFFFFFFF) {
    settingCalibrationFactor = 0;  //Default to 0
    myMem.put(LOCATION_CALIBRATION_FACTOR, settingCalibrationFactor);
  }

  //   //Look up the zero tare point
  myMem.get(LOCATION_ZERO_OFFSET, settingZeroOffset);
  if (settingZeroOffset == 0xFFFFFFFF) {
    settingZeroOffset = 1000L;  //Default to 1000 so we don't get inf
    myMem.put(LOCATION_ZERO_OFFSET, settingZeroOffset);
  }

  //   //Pass these values to the library
  myScale.setCalibrationFactor(settingCalibrationFactor);
  myScale.setZeroOffset(settingZeroOffset);

  settingsDetected = true;  //Assume for the moment that there are good cal values
  if (settingCalibrationFactor < 0.1 || settingZeroOffset == 1000)
    settingsDetected = false;  //Defaults detected. Prompt user to cal scale.
}

void tftdata() {
  tft.setTextColor(WHITE, BLACK);
  //Set the font size
  tft.setTextSize(1);

  // Encoder
  static long lastCounter = 0;
  long currentCounter = rotary.read() >> 2;
  if (lastCounter != currentCounter)
    distance = R * currentCounter;
  lastCounter = currentCounter;

  //Scale
  if (myScale.available() == true)
    long currentReading = myScale.getReading();
  float currentWeight = myScale.getWeight();

  avgWeights[avgWeightSpot++] = currentWeight;
  if (avgWeightSpot == AVG_SIZE) avgWeightSpot = 0;

  float rrweight = 0;
  for (int x = 0; x < AVG_SIZE; x++)
    rrweight += avgWeights[x];
  rrweight /= AVG_SIZE;
  if (settingsDetected == false)
    Serial.println();

  //Set the cursor coordinates
  tft.setCursor(30, 0);
  tft.print("Bobbys PLace");
  tft.setCursor(0, 20);
  tft.print("X:  ");
  tft.print(JY901.getRoll());
  tft.print(" d");
  tft.setCursor(0, 30);
  tft.print("Y:  ");
  tft.print(JY901.getPitch());
  tft.print(" d");
  tft.setCursor(0, 40);
  tft.print("Z:  ");
  tft.print(JY901.getYaw());
  tft.print(" d");
  tft.setCursor(0, 60);
  tft.print("W:  ");
  tft.print(rrweight, 2);
  tft.print(" kg");
  tft.setCursor(0, 80);
  tft.print("T:  ");
  tft.print(distance);
  tft.print(" mm");
}

This is a link to the other thread concerning the push button zeroing the printed readings of the "Z-axis" of the inclinometer.
https://forum.arduino.cc/t/push-button-reset-value-to-zero/1099972

Hi!
If it is a problem when you integrate the libraries (and not specifically the IoT Cloud one), I think that you are probably posting this issue in the wrong category. As this is "IoT Cloud", so most of the users might be able to help you with IoT Cloud issues.

I would suggest moving it (or reposting it) to "Programming Questions - Arduino Forum".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.