My hookup is like this:
GND - GND VCC - 5V CS - 5V SDO - GND SDA - A4 SCL - A5
Here is the code:
#include <Wire.h>
#include <ADXL345.h>
ADXL345 accelerometer;
void setup(void)
{
Serial.begin(9600);
// Initialize ADXL345
Serial.println("Initialize ADXL345");
if (!accelerometer.begin())
{
Serial.println("Could not find a valid ADXL345 sensor, check wiring!");
delay(500);
}
if (accelerometer.begin())
{
// Values for Free Fall detection
accelerometer.setFreeFallThreshold(0.35); // Recommended 0.3 - 0.6 g
accelerometer.setFreeFallDuration(0.1); // Recommended 0.1 s
// Select INT 1 for get activities
accelerometer.useInterrupt(ADXL345_INT1);
// Check settings
checkSetup();
}
}
void checkSetup()
{
Serial.print("Free Fall Threshold = "); Serial.println(accelerometer.getFreeFallThreshold());
Serial.print("Free Fall Duration = "); Serial.println(accelerometer.getFreeFallDuration());
}
void loop(void)
{
delay(50);
// Read values for activities
Vector norm = accelerometer.readNormalize();
// Read activities
Activites activ = accelerometer.readActivites();
if (activ.isFreeFall)
{
Serial.println("Free Fall Detected!");
}
}
It comes from this Github link: https://github.com/jarzebski/Arduino-ADXL345/blob/master/ADXL345_free_fall/ADXL345_free_fall.ino
Initially, it would return "Could not find a valid ADXL345 sensor, check wiring!" in serial monitor. After messing with some wiring and changing to SPI and back to I2C, it only returns "Initialize ADXL345".
I've added the second if() statement because it wouldn't say anything other than "Initialize ADXL345" even with the first if() statement, or with the first if() followed by an else. It should print the Free Fall Threshold and Duration but it hasn't. I have to leave and won't be back until tomorrow, so when I get back I'll try removing the checkSetup() command and maybe put something else in it's place to verify it's working. I feel like the answer is staring me in the face.
EDIT: Okay so after I finally got up from my computer for once, a thought popped into my mind and I figured the reason why checkSetup() isn't returning either string is because it's declared after setup() calls checkSetup()? Am I right? I feel like this would be the case in other languages.