Hello, I apologize for the basic question but I am wondering if this would be the preferred process for you all. I am just getting my feet wet and this forum has been super helpful in the past.
I have been using the serial timestamp with my data coming from the Nano 33 BLE, typically the readings are 17:46:27.316, 17:46:27.420, 17:46:27.529... etc. I am then exporting this into an excel spread sheet to calculate the exact time differences on consecutive data outputs to get .104, .109 etc.
I am using this time difference to calculate a few different things in excel but am looking to move all the calculations to the board and (hopefully) not use any excel tables for calculations.
One particular calculation is coming from an ultrasonic sensor, where I take the change in height divided by the change in time to find the velocity for that segment of data output.
I have seen online that the timestamp is actually coming from the serial monitor and not the Arduino so there could be discrepancies on when the data was recorded and the timestamp on the serial monitor (not to mention I cannot use the timestamp and store its time to then calculate). I am currently using delay(100).
Would adding Millis() be a suitable replacement for the time stamp? As I try a sample milli sketch, I see it is constantly printing at 100 milliseconds, but how can I be sure my data is coming in every millisecond? Based on the timestamps stated above sometimes it is .105 or .109, I would have some calculation error if I just divided each distance by 100 milliseconds.
Would there be an alternate way of doing this calculation on the board itself?
Here is the code I have now.
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
#define trigPin 10
#define echoPin 12
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
}
void loop() {
float Ax, Ay, Az;
float duration, distance;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(Ax, Ay, Az);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * .000343;
if (Az*9.80665 >= 15 || distance <= 0) {
Serial.println("Out of range");
}
else {
Serial.print(Ax*9.80665);
Serial.print(' ');
Serial.print(Ay*9.80665);
Serial.print(' ');
Serial.print(Az*9.80665);
Serial.print(' ');
Serial.print("Distance = ");
}
if (distance >= 2 || distance <= 0) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" m/s ");
}
delay(100);
}
}
Thank you for your time.