#include <Arduino.h>
#include <M5Unified.h>
int num = 0;
char buf[100];
void setup() {
auto cfg = M5.config();
cfg.serial_baudrate = 115200; // 通信速度をbpsで設定
M5.begin(cfg);
M5.Display.setRotation(3);
M5.Display.setFont(&fonts::lgfxJapanGothic_16);
M5.Display.println("シリアルテスト serial02");
M5.Display.setTextScroll(true);
}
void loop() {
int pos = 0;
while (Serial.available()) { //PCから送信された文字があるあいだ、くりかえす
char c = Serial.read(); // 1バイト読み取る
buf[pos] = c; // 配列buf に格納
pos++; // 格納位置をひとつ右へ
}
if (pos > 0) {
buf[pos] = 0; // さいごに Null (文字列の終端)を追加(これを忘れるとどうなる?)
M5.Display.print("> from pc: ");
M5.Display.print( buf );
Serial.print("> from pc: ");
Serial.print( buf ); // 格納しておいた文字列を表示
if (buf[0] == '0' && pos == 2) { // buf={ 48(='0'), 10(=改行) } のとき
num = 0; // num を 0 にする
M5.Display.println("Reset num");
Serial.println( "Reset num" );
}
delay(2000);
}
Serial.printf("%d \n" , num );
num++;
delay(1000);
}