Hello,
I am starting here a topic that I already started on the cloud iot forum https://forum.arduino.cc/t/arduino-iot-cloud-code-merging-not-showing-data/1097644
I thought that something with all the wifi settings and sending the data to the Arduino Cloud bugger the code I decided to start over, rearranged it, and tested as I go but this time on IDE and checking the serial monitor after each change. But the problem is still the same as I already mentioned in the above topic from the link.
So the problem is when I combine the Inclinometer, Encoder, Scale, and OLED Display the data from the Inclinometer is not looping anymore. The rest of the sensors ( encoder and scale) are working and printing data on the Serial monitor and the OLED display. After restarting the board (power on/off or after re-uploading the code) I am getting readings from the Inclinometer but only ones and then nothing. The display and the serial monitor show these captured readings and don't update with new readings as I move the sensor.
I just can figure out what am I doing wrong. Board is Nana 33 IoT.
Here's the code:
//Inclinometer
#include <witmotion_uart.h>
#define JY901_PORT Serial1 // JY901 TX / RX pins to Nano 33 IoT RX /TX pins respectively!
// Make sensor's class instance
JY901_UART JY901(JY901_PORT);
//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;
//Display
#include <Adafruit_GFX.h>
#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);
JY901_PORT.begin(115200);
JY901.begin();
//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());
//Display
tft.begin();
uint16_t time = millis();
tft.fillRect(0, 0, 128, 128, BLACK);
time = millis() - time;
}
void loop() {
//Inclinometer
static uint32_t prevReportTime;
// Parse incoming reports from the JY901 module
JY901.readReport();
if (millis() - prevReportTime >= 500UL) { // originaly 1333UL
prevReportTime = millis();
wmAngles_t wmAngles = JY901.getAngles();
Serial.println();
Serial.print("X ");
Serial.print(wmAngles.roll); // .roll() eq .x() for wmAngles struct
Serial.print(", ");
Serial.print("Y ");
Serial.print(wmAngles.y); // .y() eq .pitch() for wmAngles struct
Serial.print(", ");
Serial.print("Z ");
Serial.print(wmAngles.z); // .y() eq .yaw() for wmAngles struct
Serial.print(", ");
// 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.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();
}
}
//Display
tftdata();
}
//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);
}
void tftdata(){
tft.setTextColor(WHITE, BLACK);
//Set the font size
tft.setTextSize(1);
//Inclinometer
static uint32_t prevReportTime;
// Parse incoming reports from the JY901 module
JY901.readReport();
if (millis() - prevReportTime >= 500UL) // originaly 1333UL
prevReportTime = millis();
wmAngles_t wmAngles = JY901.getAngles();
// 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(wmAngles.roll);
tft.print(" d");
tft.setCursor(0, 30);
tft.print("Y: ");
tft.print(wmAngles.y);
tft.print(" d");
tft.setCursor(0, 40);
tft.print("Z: ");
tft.print(wmAngles.z);
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");
}