#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);

// --- Movement Detection Variables ---
// Threshold: How much acceleration (in G's) must change before we consider it movement?
// You will need to tune this value based on your setup and what counts as "movement".
const float MOVEMENT_THRESHOLD = 0.8; // Example: Detect movement if magnitude exceeds 0.8G

// Tracking variables for the previous reading
float last_accel_x = 0.0;
float last_accel_y = 0.0;
float last_accel_z = 0.0;

// A simple state machine to prevent spamming "movement detected" on every single loop cycle
bool movement_detected_flag = false;


void setup(void)
{
  Serial.begin(9600);
  while (!Serial);
  Serial.println("ADXL343 Movement Detector Initializing..."); Serial.println("");

  /* Initialise the sensor */
  if(!accel.begin())
  {
    Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
    while(1);
  }

  /* Set the range to whatever is appropriate for your project */
  accel.setRange(ADXL343_RANGE_16_G);

  // --- IMPORTANT CHANGE: We are no longer using interrupts for this function ---
  // The interrupt setup code is removed/commented out because we are polling in loop().
  /* 
  pinMode(INPUT_PIN_INT1, INPUT);
  attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING);
  g_int_config_enabled.bits.single_tap = true;
  accel.enableInterrupts(g_int_config_enabled);
  g_int_config_map.bits.single_tap = ADXL343_INT1;
  accel.mapInterrupts(g_int_config_map);
  */

  Serial.println("ADXL343 initialized. Ready to detect movement.");
}


void loop(void)
{
  // 1. Read the current sensor data
  sensors_event_t event;
  accel.getEvent(&event);

  float current_x = event.acceleration.x;
  float current_y = event.acceleration.y;
  float current_z = event.acceleration.z;

  // 2. Calculate the magnitude of acceleration (Total force vector)
  // Magnitude = sqrt(X^2 + Y^2 + Z^2)
  float current_magnitude = sqrt(current_x * current_x + current_y * current_y + current_z * current_z);

  // 3. Calculate the change in magnitude since the last reading (Movement proxy)
  // We compare the current total force to the previous total force.
  float last_magnitude = sqrt(last_accel_x * last_accel_x + last_accel_y * last_accel_y + last_accel_z * last_accel_z);
  
  // The difference between current and previous magnitude is a good indicator of change.
  float delta_magnitude = abs(current_magnitude - last_magnitude);


  // 4. Detection Logic
  if (delta_magnitude > MOVEMENT_THRESHOLD) {
    if (!movement_detected_flag) {
      Serial.println("=============================");
      Serial.print("MOVEMENT DETECTED! Delta Magnitude: ");
      Serial.println(delta_magnitude);
      Serial.println("=============================");
      movement_detected_flag = true; // Set flag to prevent spamming
    }
  } else {
    // If the change is small, reset the flag so we can detect movement again later
    if (movement_detected_flag) {
        Serial.println("Movement subsided.");
    }
    movement_detected_flag = false;
  }

  // 5. Update state for the next loop iteration
  last_accel_x = current_x;
  last_accel_y = current_y;
  last_accel_z = current_z;

  // Small delay to keep readings stable and prevent overwhelming serial output
  delay(50); 
}
