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);
}
}