Serial monitor doesn't show anything

Hi there, this is regarding a fall detection. I have attached the code. It doesn't seem to print anything in the serial monitor. This is my first time doing this, and I can't find the error.

#include <Wire.h>         // I2C communication
#include <BMA250.h>       // Accelerometer library

// Accelerometer variables
BMA250 accel_sensor;
int x, y, z;
double temp;
float accelMagnitude;

// Fall detection thresholds
float impactThreshold = 2.5;
float freeFallThreshold = 0.5;
float recoveryThreshold = 1.0;
unsigned long recoveryDelay = 1000;

// Fall detection states
enum FallState { IDLE, IMPACT_DETECTED, FREE_FALL_DETECTED };
FallState fallState = IDLE;

unsigned long impactTime = 0;
unsigned long freeFallTime = 0;

// Setup Serial for different boards
#if defined(ARDUINO_ARCH_SAMD)
 #define SerialMonitorInterface SerialUSB
#else
 #define SerialMonitorInterface Serial
#endif

void setup() {
    SerialMonitorInterface.begin(115200);
    Wire.begin();

    SerialMonitorInterface.println("Initializing BMA250...");
    if (!accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms)) {
        SerialMonitorInterface.println("ERROR: BMA250 not detected!");
        while (1);
    }
    SerialMonitorInterface.println("BMA250 initialized successfully.");
}

void loop() {
    accel_sensor.read();  // Read accelerometer data
    x = accel_sensor.X;
    y = accel_sensor.Y;
    z = accel_sensor.Z;

    temp = ((accel_sensor.rawTemp * 0.5) + 24.0);

    if (x == -1 && y == -1 && z == -1) {
        SerialMonitorInterface.println("ERROR: Invalid data.");
        return;
    }

    accelMagnitude = sqrt(sq(x) + sq(y) + sq(z));  // Calculate magnitude

    switch (fallState) {
        case IDLE:
            if (accelMagnitude > impactThreshold) {
                SerialMonitorInterface.println("Impact detected!");
                impactTime = millis();
                fallState = IMPACT_DETECTED;
            }
            break;

        case IMPACT_DETECTED:
            if (accelMagnitude < freeFallThreshold) {
                SerialMonitorInterface.println("Free fall detected!");
                freeFallTime = millis();
                fallState = FREE_FALL_DETECTED;
            } else if (millis() - impactTime > recoveryDelay) {
                SerialMonitorInterface.println("False alarm, resetting to IDLE.");
                fallState = IDLE;
            }
            break;

        case FREE_FALL_DETECTED:
            if (accelMagnitude < recoveryThreshold && (millis() - freeFallTime) > 500) {
                SerialMonitorInterface.println("Fall confirmed!");
                fallState = IDLE;
            } else if (millis() - freeFallTime > recoveryDelay) {
                SerialMonitorInterface.println("False alarm, resetting to IDLE.");
                fallState = IDLE;
            }
            break;
    }

    showSerial();  // Debugging output
    delay(250);    // Adjust delay as necessary
}

void showSerial() {
    SerialMonitorInterface.print("X = ");
    SerialMonitorInterface.print(x);
    SerialMonitorInterface.print("  Y = ");
    SerialMonitorInterface.print(y);
    SerialMonitorInterface.print("  Z = ");
    SerialMonitorInterface.print(z);
    SerialMonitorInterface.print("  Temp (C) = ");
    SerialMonitorInterface.print(temp);
    SerialMonitorInterface.print("  Accel Magnitude = ");
    SerialMonitorInterface.println(accelMagnitude);
}

Serial monitor needs to be set to 115200, same value as is used in the begin() statement

serial monitor is set to 115200 also. doesn't print anything.

Instead of using "SerialMonitorInterface", try "Serial" (e.g. Serial.begin(115200), Serial.println).

Hi @Bruce123,

welcome to the arduino-forum.
Well done posting your code as a code-section in your very first posting.

You should add more information.
You did not include what exact BMA250-library you are using.
The library-manager doesn't find one
and on GitHub there are more than one BMA250-libraries.

What exact type of microcontroller-board are you using?

For almost all microcontroller-boards using "Serial" as the name for the serial interface will work

Additionally you should add a line of code that prints to the serial monitor
right below Serial.begin(115200);

unconditional so the printing is done always independant of any conditions
So the modificated code looks like this

#include <Wire.h>         // I2C communication
#include <BMA250.h>       // Accelerometer library

// Accelerometer variables
BMA250 accel_sensor;
int x, y, z;
double temp;
float accelMagnitude;

// Fall detection thresholds
float impactThreshold = 2.5;
float freeFallThreshold = 0.5;
float recoveryThreshold = 1.0;
unsigned long recoveryDelay = 1000;

// Fall detection states
enum FallState { IDLE, IMPACT_DETECTED, FREE_FALL_DETECTED };
FallState fallState = IDLE;

unsigned long impactTime = 0;
unsigned long freeFallTime = 0;


void setup() {
    Serial.begin(115200);
    delay(2000); // wait to seconds in case intialising Serial takes some time
    Serial.println("Setup-Start");
    Wire.begin();

    Serial.println("Initializing BMA250...");
    if (!accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms)) {
        Serial.println("ERROR: BMA250 not detected!");
        while (1);
    }
    Serial.println("BMA250 initialized successfully.");
}

void loop() {
    accel_sensor.read();  // Read accelerometer data
    x = accel_sensor.X;
    y = accel_sensor.Y;
    z = accel_sensor.Z;

    temp = ((accel_sensor.rawTemp * 0.5) + 24.0);

    if (x == -1 && y == -1 && z == -1) {
        Serial.println("ERROR: Invalid data.");
        return;
    }

    accelMagnitude = sqrt(sq(x) + sq(y) + sq(z));  // Calculate magnitude

    switch (fallState) {
        case IDLE:
            if (accelMagnitude > impactThreshold) {
                Serial.println("Impact detected!");
                impactTime = millis();
                fallState = IMPACT_DETECTED;
            }
            break;

        case IMPACT_DETECTED:
            if (accelMagnitude < freeFallThreshold) {
                Serial.println("Free fall detected!");
                freeFallTime = millis();
                fallState = FREE_FALL_DETECTED;
            } else if (millis() - impactTime > recoveryDelay) {
                Serial.println("False alarm, resetting to IDLE.");
                fallState = IDLE;
            }
            break;

        case FREE_FALL_DETECTED:
            if (accelMagnitude < recoveryThreshold && (millis() - freeFallTime) > 500) {
                Serial.println("Fall confirmed!");
                fallState = IDLE;
            } else if (millis() - freeFallTime > recoveryDelay) {
                Serial.println("False alarm, resetting to IDLE.");
                fallState = IDLE;
            }
            break;
    }

    showSerial();  // Debugging output
    delay(250);    // Adjust delay as necessary
}

void showSerial() {
    Serial.print("X = ");
    Serial.print(x);
    Serial.print("  Y = ");
    Serial.print(y);
    Serial.print("  Z = ");
    Serial.print(z);
    Serial.print("  Temp (C) = ");
    Serial.print(temp);
    Serial.print("  Accel Magnitude = ");
    Serial.println(accelMagnitude);
}

I could not compile this code because it is unclear which BMA250-library you are using

if (!accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms)) {
        SerialMonitorInterface.println("ERROR: BMA250 not detected!");
        while (1);

i got it working. this loop is the one with problem. not sure what is problem. but I took away the loop. now can print.

accel_sensor.begin(BMA250_range_2g, BMA250_update_time_64ms

Background information:
BMA250: TinyCircuits-BMA250-library
I'm using TinyZero Processor Board

Thanks for the help guys