Need help understanding where my program is failing

I need some help with an assignment for a class. The class is an introduction to the Internet of Things. I have no coding experience outside of an introduction to HTML, CSS, and JavaScript. The instructor is leaning heavily towards using the M5stickC to code and demonstrate knowledge of how it works. The problem is that I have no experience and can't figure out what I am doing wrong.

The assignment is to program the M5stickC built-in LED to blink once the imu senses a drop larger than 10cm.

any advice is greatly appreciated.

link to the M5stickC that I am using

#include "M5StickCPlus2.h"

float accXSq = 0.0F;
float accYSq = 0.0F;
float accZSq = 0.0F;

float accMagSq = 0.0F;
float accMag = 0.0F;
float MaxAccMag = 0.0F;


void setup() {
    auto cfg = M5.config();
    StickCP2.begin(cfg);
    StickCP2.Display.setRotation(1);
    StickCP2.Display.setTextColor(GREEN);
    StickCP2.Display.setTextDatum(middle_center);
    StickCP2.Display.setFont(&fonts::FreeSansBold9pt7b);
    StickCP2.Display.setTextSize(1);
    #define BUILTIN_LED 19
}


void loop(void) {
    auto imu_update = StickCP2.Imu.update();
    if (imu_update) {
        StickCP2.Display.setCursor(0, 40);
        StickCP2.Display.clear();  // Delay 100ms 延迟100ms

        auto data = StickCP2.Imu.getImuData();

        accXSq = sq(accX);
        accYSq = sq(accY);
        accZSq = sq(accZ);

        accMagSq = accXSq + accYSq + accZSq;
        accMag = sqrt(accMagSq);


        if(accMag > maxAccMag){
          maxAccMag = accMag;
        }

        if(data.accel.z > 0.15){
          digitalWrite(LED_BUILTIN, HIGH);
          delay(100);
          digitalWrite(LED_BUILTIN, LOW);
          delay(100);
        }

        Serial.printf("ax:%f  ay:%f  az:%f\r\n", data.accel.x, data.accel.y,
                      data.accel.z);
        Serial.printf("gx:%f  gy:%f  gz:%f\r\n", data.gyro.x, data.gyro.y,
                      data.gyro.z);

        StickCP2.Display.printf("IMU:\r\n");
        StickCP2.Display.printf("%0.2f %0.2f %0.2f\r\n", data.accel.x,
                                data.accel.y, data.accel.z);
        StickCP2.Display.printf("%0.2f %0.2f %0.2f\r\n", data.gyro.x,
                                data.gyro.y, data.gyro.z);
        StickCP2.Display.println(maxAccMag);
    }
    delay(100);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Please edit your post, select all code and click the <CODE/> button and save your post. This will apply code tags (as described in How to get the best out of this forum) which makes your code easier to read, easier to copy and the formum software will display it correctly.

I just had a quick look at your post; you're missing a ```cpp before the first line of code; that should fix it.

appreciate your help. Think I got it fixed.

1 Like

I added a link to the M5stickC that I am using.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.