Notes
Notes - notes.io |
#include "ADXL345_I2C.h"
// Define the I2C pins for the ADXL345
// Assuming PB_11 is SDA and PB_10 is SCL based on the code snippet
ADXL345_I2C accelerometer(PB_11, PB_10);
// Global variables for raw and filtered output
int X_OUT_raw, Y_OUT_raw, Z_OUT_raw; // Inputs/Raw Data
int X_OUT, Y_OUT, Z_OUT; // Outputs/Filtered Data
int main() {
// Array to hold the initial raw readings (used in the snippet, typically for offsets or setup)
int readings[3] = {0, 0, 0};
// Setup serial communication for printing to PC
// Default mbed setup for Serial/USB
Serial pc(USBTX, USBRX);
pc.printf("Starting ADXL345 Test...n");
wait(0.001);
// Read and print the device ID (should be 0x2D for ADXL345)
pc.printf("Device ID is: 0x%2Xn", accelerometer.getDeviceID());
wait(0.001);
// --- Initialization and Configuration ---
// Set power control to a state where the device is not initialized/in standby (0x00)
if (accelerometer.setPowerControl(0x00)) {
pc.printf("Didn't intitialize power controln");
return 0;
}
wait(0.001);
// Set full resolution and 4mg/Lsb
// The constant 0x0B likely corresponds to a setting for Full Resolution (and range)
if (accelerometer.setDataFormatControl(0x0B)) {
pc.printf("didn't set data formatn");
return 0;
}
wait(0.001);
// Set data rate to 3200 Hz (ADXL345_3200HZ is likely a defined constant)
// The snippet mentions "3.2kHz data rate"
if (accelerometer.setDataRate(ADXL345_3200HZ)) {
pc.printf("didn't set data raten");
return 0;
}
wait(0.001);
// Set power control to Measurement Mode (ADXL345 is actively measuring)
// MeasurementMode is likely a defined constant (e.g., 0x08)
if (accelerometer.setPowerControl(MeasurementMode)) {
pc.printf("didn't set the power control to measurementn");
return 0;
}
// --- Main Loop for Reading and Filtering ---
while (1) {
wait(0.1); // Wait for 100ms before next reading
// Read the acceleration values from the sensor
accelerometer.getOutput(readings);
wait(0.1);
// Map the array readings back to individual raw variables
X_OUT_raw = (int16_t)readings[0];
Y_OUT_raw = (int16_t)readings[1];
Z_OUT_raw = (int16_t)readings[2];
// --- Low Pass Filter (FIR/IIR) Implementation ---
// This is a simple first-order IIR (Infinite Impulse Response) or
// Exponential Moving Average (EMA) filter.
// It's a common technique for smoothing sensor data.
// New Value = (1 - alpha) * Old Value + alpha * Raw Value
// Here, alpha = 0.06 and (1 - alpha) = 0.94
// Low pass filter for stable X,Y,Z values
X_OUT = 0.94 * X_OUT + 0.06 * X_OUT_raw;
Y_OUT = 0.94 * Y_OUT + 0.06 * Y_OUT_raw;
Z_OUT = 0.94 * Z_OUT + 0.06 * Z_OUT_raw;
// Print the filtered (and raw) values to the PC
// rn ensures a new line and carriage return
// Note: The format specifier %i is used for integers.
pc.printf("X_i,%i,X_i\r\n", X_OUT, Y_OUT, Z_OUT);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
