Hi All. An Arduino newbie here.
I have an ADXL335 whose job is to give me the z values while it is attached to a swinging pendulum and the pendulum is kicked hard to get different z values.
Here is the problem.....if you look at the 4th row in the output below, you will see that the z value is 305 after the pendulum was kicked hard.
It should have been something much greater than 342 ( trust me ) when the pendulum was kicked. Instead the z value went the opposite direction on the number line. I ran this code 1,000 times and this is the first time that I saw such an issue.
The ADXL335 is oscillating when these numbers were changing. I feel that the Arduino didn't catch that change from 342 16600984 to a higher number, in the nick of time.
The Arduino is 'dropping' sensor data that is coming in from the ADXL335 at the wrong time.
How can we fix this issue?
341 16578204
342 16589592
342 16600984
305 16612372
390 16624104
403 16635832
359 16647564
356 16659548
356 16671280
Here are my thoughts and what I found after looking around for a solution:
- Increase the sampling frequency
- Optimize the code
- Use interrupts
- Bare metal programming
Can some please advice how I can stop my Arduino from 'dropping' sensor data like the way it is doing? Ty
#include <ADXL335.h>
ADXL335 accelerometer;
void setup() {
Serial.begin(9600);
accelerometer.begin();
}
void loop() {
// Get the current timestamp in microseconds
unsigned long timestamp = micros();
// Read the Z-axis value from the accelerometer
float z = accelerometer.getZ();
// Print the Z-axis value and timestamp over the serial monitor
Serial.print("Z: ");
Serial.print(z);
Serial.print("\tTimestamp: ");
Serial.println(timestamp);
}