LoginSignup
0
0

M5StickCでローラー式距離計を作ってみる

Posted at

先日 micro:bitで距離計を作成しました

一応は動くのですが もう少し安定動作してコンパクトなものを作りたくなり
M5StickC Plusとホールセンサーを利用して同じものを作ってみました
image.png

M5Stackシリーズのマイコンを利用するのは初めてなので覚え書きです。

初期設定

全く関連の環境を使ったことがなかったので初期設定から
VSCode拡張機能のC/C++と PlatformIO をインストール
image.png
PlatformIO のメニューから HOME >New Priject
image.png
プロジェクト名を設定して ボードを指定
image.png
LibrariesからM5StickCPlusのライブラリをインポート
image.png
M5Stickと単に入れただけではうまく検索されなかったのでここで別のライブラリ入れてしまったりで軽く躓いた

初期テスト

とりあえず ホールセンサーで回転をカウントして
一定カウントになるとBeepを鳴らすコードを目標に作成

イロハのイも分かっていない状態でホールセンサーユニットのサンプルコードをそのまま動かして
画面が更新しないのに戸惑ったりしました

#include <M5StickCPlus.h>
#define HALL 33
int mag_count;
bool flag;
int flag_count;
int beep_count;

void setup() {
  M5.begin();
  pinMode(HALL, INPUT);
  M5.Lcd.setRotation(0);
  flag = true;
  arr_index = 0;
  beep_count = 50;
}
void loop() {
  M5.update();
    bool Hall_status = digitalRead(HALL);
    if (Hall_status == false){
      if(flag){
        mag_count++;
        flag = false;
          if (mag_count % beep_count == 0 ){
            //Beep
            M5.Beep.beep();
            delay(200);
            M5.Beep.end();
            }
      }
    }else{
      flag =true;
    }
  M5.Lcd.setTextSize(6);
  M5.Lcd.setCursor(0, 20);
  M5.Lcd.printf("%3d", mag_count/10);

このコードだとBeepを鳴らしている間 センサーのカウントがされません
タスク機能を使う必要なようです

マルチタスク

こういったマルチタスク機能を触るのも初めてでしたが
センサーのカウント部分を別タスクに分けました

#include <Arduino.h>
#include <M5StickCPlus.h>
#define HALL 33
int mag_count;
bool flag;
int flag_count;
int arr1[] = {50, 100, 150, 200, 250, 300};
int arr_index;
int beep_count;

/* カウントタスク */
void task0(void* arg) {
  while(1){
    bool Hall_status = digitalRead(HALL);
    if (Hall_status == false){
      if(flag){
        mag_count++;
        flag = false;
      }
    }else{
      flag =true;
    }
    vTaskDelay(10);
  }
}

void initCount(int beep_count){
  mag_count = 0;
  flag_count = 0;
  M5.Lcd.fillScreen(0x000000);
  M5.Lcd.setTextSize(1);
  M5.Lcd.setCursor(20, 200);
  M5.Lcd.printf("BeepDistance: %d", beep_count/10);
}

void setup() {
  M5.begin();
  pinMode(HALL, INPUT);
  M5.Lcd.setRotation(0);
  flag = true;
  arr_index = 0;
  beep_count = arr1[arr_index];
  xTaskCreatePinnedToCore(task0, "Task0", 4096, NULL, 1, NULL, 1);

  initCount(beep_count);
}


void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    initCount(beep_count);
    }
  if (M5.BtnB.wasPressed()) {
    arr_index++;
    if (arr_index > sizeof(arr1)-1){arr_index = 0;}
    beep_count = arr1[arr_index];
    initCount(beep_count);
    }

  M5.Lcd.setTextSize(6);
  M5.Lcd.setCursor(0, 20);
  M5.Lcd.printf("%3d", mag_count/10);
  M5.Lcd.setTextSize(3);
  M5.Lcd.setCursor(80, 80);
  M5.Lcd.printf(".%d", mag_count%10);

  if ( mag_count != flag_count and mag_count % beep_count == 0 ){
    //
    M5.Beep.beep();
    delay(100);
    M5.Beep.end();
    delay(50);
    M5.Beep.beep();
    delay(100);
    M5.Beep.end();
    flag_count = mag_count;
    }
}

カウントのリセットと Beepを鳴らすまでのカウント間隔をプリセットから変更できるように作成
とりあえずは動いている様子なので これで試してみることとします

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0