// https://101010.fun/iot/m5stickc-plus-accel.html // 加速度センサで検知した振動をグラフにプロットするプログラム #include <M5StickCPlus.h> #define SAMPLE_PERIOD 20 // サンプリング間隔(ms) #define SAMPLE_SIZE 240 // サンプリング間隔(20) x 画面幅(240) = 4.8s #define BUTTON_A 37 bool isPause = false; void setup() { M5.begin(); M5.Lcd.setRotation(3); M5.IMU.Init(); M5.IMU.SetAccelFsr(M5.IMU.AFS_4G); pinMode(BUTTON_A, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON_A), handleInterrupt, FALLING); } float ax, ay, az[SAMPLE_SIZE]; #define X0 5 // 横軸の描画開始座標 // 水平静止で重力加速度1000mGが常にかかることを考慮する #define MINZ -1000 // 縦軸の最小値 mG #define MAXZ 3000 // 縦軸の最大値 mG void handleInterrupt() { isPause = !isPause; } void loop() { if (!isPause) { M5.Lcd.fillScreen(BLACK); // 画面クリア for (int i = 0; i < SAMPLE_SIZE; i++) { if (isPause) break; M5.IMU.getAccelData(&ax,&ay,&az[i]); // IMUから加速度を取得 az[i] *= 1000; // mGに変換 if (i == 0) continue; // Serial.println(az[i]); // シリアルモニタは115200baudで通信 int y0 = map((int)(az[i - 1]), MINZ, MAXZ, M5.Lcd.height(), 0); int y1 = map((int)(az[i]), MINZ, MAXZ, M5.Lcd.height(), 0); M5.Lcd.drawLine(i - 1 + X0, y0, i + X0, y1, YELLOW); delay(SAMPLE_PERIOD); } } }