|
| 1 | +/*! |
| 2 | + * @file 7semi_ADXL335.cpp |
| 3 | + * @brief Arduino library for the ADXL335 3-axis analog accelerometer |
| 4 | + * @details This library reads analog voltage from the X, Y, Z outputs of the ADXL335 sensor |
| 5 | + * and converts it into raw ADC values, voltage (V), or acceleration (g-force). |
| 6 | + * @author 7semi |
| 7 | + * @copyright (c) 2025 |
| 8 | + */ |
| 9 | + |
| 10 | +#include "7semi_ADXL335.h" |
| 11 | + |
| 12 | +ADXL335_7semi::ADXL335_7semi(uint8_t xPin, uint8_t yPin, uint8_t zPin) |
| 13 | + : x_pin(xPin), y_pin(yPin), z_pin(zPin) { |
| 14 | +} |
| 15 | + |
| 16 | +void ADXL335_7semi::begin() { |
| 17 | + pinMode(x_pin, INPUT); |
| 18 | + pinMode(y_pin, INPUT); |
| 19 | + pinMode(z_pin, INPUT); |
| 20 | +} |
| 21 | + |
| 22 | +void ADXL335_7semi::readRaw(int &x, int &y, int &z) { |
| 23 | + x = analogRead(x_pin); |
| 24 | + y = analogRead(y_pin); |
| 25 | + z = analogRead(z_pin); |
| 26 | +} |
| 27 | + |
| 28 | +void ADXL335_7semi::readVoltage(float &x, float &y, float &z) { |
| 29 | + int rawX, rawY, rawZ; |
| 30 | + readRaw(rawX, rawY, rawZ); |
| 31 | + |
| 32 | + x = (rawX / 1023.0) * REF_VOLTAGE; |
| 33 | + y = (rawY / 1023.0) * REF_VOLTAGE; |
| 34 | + z = (rawZ / 1023.0) * REF_VOLTAGE; |
| 35 | +} |
| 36 | + |
| 37 | +void ADXL335_7semi::readG(float &x, float &y, float &z) { |
| 38 | + float voltX, voltY, voltZ; |
| 39 | + readVoltage(voltX, voltY, voltZ); |
| 40 | + |
| 41 | + x = (voltX - ZERO_VOLTAGE) / SENSITIVITY; |
| 42 | + y = (voltY - ZERO_VOLTAGE) / SENSITIVITY; |
| 43 | + z = (voltZ - ZERO_VOLTAGE) / SENSITIVITY; |
| 44 | +} |
0 commit comments