Skip to content

Conversation

@porterfencl
Copy link

first prototype

@YVishere
Copy link
Collaborator

Looks good. Onto file management.

Rename gyro.cpp and gyro.h to imu.cpp and imu.h respectively.

@YVishere
Copy link
Collaborator

YVishere commented Nov 16, 2025

Move these lines to imu.h and change the comments to however you like

// Basic demo for accelerometer/gyro readings from Adafruit ISM330DHCX

#include <Adafruit_ISM330DHCX.h>

// For SPI mode, we need a CS pin
#define LSM_CS PA4
// For software-SPI mode we need SCK/MOSI/MISO pins
#define LSM_SCK D13
#define LSM_MISO D12
#define LSM_MOSI D11

@YVishere
Copy link
Collaborator

YVishere commented Nov 16, 2025

Any code not listed here can be removed from main.cpp and not replaced in imu.cpp

Create a global variable in imu.cpp but there is no need to declare it in imu.h

Adafruit_ISM330DHCX ism330dhcx;

Declare a begin() function in imu.h and initialize it in imu.cpp with the imu related code in setup()

if (!ism330dhcx.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
    Serial.println("Failed to find ISM330DHCX chip1");
    while (1) {
      delay(10);
    }
  }

  Serial.println("ISM330DHCX Found!");

  ism330dhcx.configInt1(false, false, true); // accelerometer DRDY on INT1
  ism330dhcx.configInt2(false, true, false); // gyro DRDY on INT2

@YVishere
Copy link
Collaborator

The point of adding these next methods is that if we want to move away from the library and use our own custom methods, we can do it with minimal changes by modifying code within them

Declare in imu.h and initialize the following wrapper methods in imu.cpp: imuReadGyro(int *x, int *y, int *z), imuReadAccel(int *x, int *y, int *z), imuGetEvent(int *accel, int *gyro, int *temp)

@YVishere
Copy link
Collaborator

Update main.cpp to only call methods defined in imu.h

  1. only include <Arduino.h>, "imu.h"
  2. the only imu related code in setup.h should be the begin() function
  3. the loop() should call the wrapper functions defined in imu.h and read their data (have a delay)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants