I was able to get the MPU6050 Accelerometer working without an interrupt. The basic code reads date from the MPU6050 and displays it live on the OLED. It also keeps track of Max G for X,Y,X and displays that on the OLED
In case I forgot the MPU6050 library is from Electronic Cats. The MPU6050 code is based on the MPU6050-DMP6 example from the Electronic Cats library.
Here is the code. Comments added to explain code functions
Any advice or suggestions is greatly appreciated
Next up is to have this write the live and Max G values to an SD card
//
// Display Max G on OLED
//DOES NOT USE INTERRUPT
//
#include "heltec.h"
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
SSD1306AsciiWire oled;
MPU6050 mpu;
bool dmpReady = false; // set true if DMP init was successful
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
float MaxGx = 0, MaxGy = 0, MaxGz = 0;
// orientation motion variables
Quaternion q; // [w, x, y, z] quaternion container
//VectorInt16 aa; // [x, y, z] accel sensor measurements
//VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
//VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
//VectorFloat gravity; // [x, y, z] gravity vector
//float euler[3]; // [psi, theta, phi] Euler angle container
//float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
// ==========
// === SETUP ===
// ==========
void setup() {
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, true /*Serial Enable*/);
//Heltec.display -> display();
oled.begin(&Adafruit128x64, 0x3C, 16);
oled.setFont(System5x7);
oled.clear();
oled.setCursor(15,3);
oled.print("G-force Monitor");
delay(1000);
oled.clear();
Wire.begin(); //pin 21 and 22 for ESP32
Wire.setClock(400000); // 400kHz I2C clock
// initialize serial monitor
Serial.begin(115200);
// initialize I2C devices
Serial.println(F("Initializing I2C devices..."));
mpu.initialize();
// verify I2C device connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
// load and configure the DMP
Serial.println(F("Initializing DMP..."));
oled.setCursor(0,0);
oled.println("Calibrate Acc/Gyro");
devStatus = mpu.dmpInitialize();
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
// make sure it worked (returns 0 if MPU6050 working)
if (devStatus == 0) {
// Calibration Time: generate offsets and calibrate our MPU6050
mpu.CalibrateAccel(6);
mpu.CalibrateGyro(6);
mpu.PrintActiveOffsets();
// turn on the DMP, now that it's ready
Serial.println(F("Enabling DMP..."));
oled.print("Accel/Gyro Ready");
delay(1000);
oled.clear();
mpu.setDMPEnabled(true);
// set our DMP Ready flag so the main loop() function knows it's okay to use it
Serial.println(F("DMP ready!"));
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = initial memory load failed
// 2 = DMP configuration updates failed
// (if it's going to break, usually the code will be 1)
Serial.print(F("DMP Initialization failed (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
}
// ===================
// === MAIN PROGRAM LOOP ===
// ===================
void loop() {
// if MPU6050nprogramming failed, don't do anything
if (!dmpReady) return;
// wait for MPU extra packet(s) available
while (fifoCount < packetSize) {
if (fifoCount < packetSize) {
// try to get out of the infinite loop
fifoCount = mpu.getFIFOCount();
}
}
// get current FIFO count
fifoCount = mpu.getFIFOCount();
if(fifoCount < packetSize){
}
// check for FIFO overflow
else if (fifoCount >= 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
// fifoCount = mpu.getFIFOCount(); // will be zero after reset
Serial.println(F("FIFO overflow!"));
}
{
// read a packet from FIFO
while(fifoCount >= packetSize){
mpu.getFIFOBytes(fifoBuffer, packetSize);
// track FIFO count in case there is > 1 packet available
fifoCount -= packetSize;
}
// display quaternion values in easy matrix form: w x y z
mpu.dmpGetQuaternion(&q, fifoBuffer);
Serial.print("ACCEL\t");
//Serial.print(q.w);
//Serial.print("\t");
Serial.print(q.x);
Serial.print("\t");
Serial.print(q.y);
Serial.print("\t");
Serial.println(q.z);
//calculate Max Accel X,Y,Z
if (abs(q.x) > abs(MaxGx))
{
MaxGx = q.x;
} else{
MaxGx = MaxGx;
}
if (abs(q.y) > abs(MaxGy))
{
MaxGy = q.y;
} else{
MaxGy = MaxGy;
}
if (abs(q.z) > abs(MaxGz))
{
MaxGz = q.z;
} else{
MaxGz = MaxGz;
}
//FOR DEBUGGING Serial.println(("MAX X=") + String(MaxGx) + (" Y=") + String(MaxGy) + (" Z=") + String(MaxGz));
//display Accel G data on OLED
oled.setCursor(0,0);
oled.clearToEOL();
oled.println("ACCEL X Y Z");
oled.setCursor(0,1);
oled.clearToEOL();
oled.setCursor(20,1);
oled.print(q.x,2);
oled.setCursor(60,1);
oled.print(q.y,2);
oled.setCursor(95,1);
oled.println(q.z,2);
//display MAX G on OLED
oled.setCursor(0,4);
oled.println("MaxG X Y Z");
oled.setCursor(0,5);
oled.clearToEOL();
oled.setCursor(20,5);
oled.print(MaxGx);
oled.setCursor(60,5);
oled.print(MaxGy);
oled.setCursor(95,5);
oled.println(MaxGz);
}
}