Help us understand the problem. What is going on with this article?

M5Stackでジェスチャーセンサーを使ってみた。

やってみたこと

M5stackとGrove - Gesture(PAJ7620U2)を使って簡単なジェスチャー認識をやってみました。
使用したのはM5stack Basic。環境はArduinoIDEです。

400px-Gesture_sensor_3.png

コードを書くまでにやること

まず下記のGithubからライブラリーをZIPでダウンロードします。
https://github.com/Seeed-Studio/Gesture_PAJ7620

次にArduino IDEを立ち上げてダウンロードしたZIPファイルをインクルードします。
1.png

プログラム

下記のコードをIDEにコピーしてアップロードします。
このコードはジェスチャーセンサーから受け取る、
上・下・右・左・時計回り・反時計回り・手を振るに合わせてM5Stackの画面の色を変更するプログラムになっています。

PAJ7620U2.ino
#include <M5Stack.h>
#include <Wire.h>
#include "paj7620.h"

#define GES_REACTION_TIME       500             // You can adjust the reaction time according to the actual circumstance.
#define GES_ENTRY_TIME          800             // When you want to recognize the Forward/Backward gestures, your gestures' reaction time must less than GES_ENTRY_TIME(0.8s). 
#define GES_QUIT_TIME           1000

void up()
{
    M5.Lcd.clear(BLACK);
    M5.Lcd.drawString("UP",0,0,4);  
}

void down()
{
    M5.Lcd.clear(BLACK);
    M5.Lcd.drawString("DOWN",0,0,4);  
}

void left()
{
    M5.Lcd.clear(RED);
    M5.Lcd.drawString("LEFT",0,0,4);  
}

void right()
{
    M5.Lcd.clear(RED);
    M5.Lcd.drawString("RIGHT",0,0,4);  
}

void forward()
{
    M5.Lcd.clear(GREEN);
    M5.Lcd.drawString("FORWARD",0,0,4);
}

void backward()
{
    M5.Lcd.clear(GREEN);
    M5.Lcd.drawString("BACKWARD",0,0,4);
}

void clockwise()
{
    M5.Lcd.clear(BLUE);
    M5.Lcd.drawString("CLOCKWISE",0,0,4);
}

void anti_clockwise()
{
    M5.Lcd.clear(BLUE);
    M5.Lcd.drawString("ANTI-CLOCKWISE",0,0,4);
}

void wave()
{
    M5.Lcd.clear(WHITE);
    M5.Lcd.drawString("WAVE",0,0,4);
}

void setup()
{
    M5.begin();
    uint8_t error = 0;

    error = paj7620Init();          // initialize Paj7620 registers
    if (error) 
    {
        M5.Lcd.print("INIT ERROR,CODE:");
        M5.Lcd.println(error);
    }
    else
    {
        M5.Lcd.drawString("INIT OK",0,0,4);
    };
}

void loop()
{
    uint8_t data = 0, data1 = 0, error;

    error = paj7620ReadReg(0x43, 1, &data);             // Read Bank_0_Reg_0x43/0x44 for gesture result.
    if (!error) 
    {
        switch (data)                                   // When different gestures be detected, the variable 'data' will be set to different values by paj7620ReadReg(0x43, 1, &data).
        {
            case GES_RIGHT_FLAG:
                delay(GES_ENTRY_TIME);
                paj7620ReadReg(0x43, 1, &data);
                if(data == GES_FORWARD_FLAG) 
                {
                    forward();
                    delay(GES_QUIT_TIME);
                }
                else if(data == GES_BACKWARD_FLAG) 
                {
                    backward();
                    delay(GES_QUIT_TIME);
                }
                else
                {
                    right();
                }          
                break;
            case GES_LEFT_FLAG: 
                delay(GES_ENTRY_TIME);
                paj7620ReadReg(0x43, 1, &data);
                if(data == GES_FORWARD_FLAG) 
                {
                    forward();
                    delay(GES_QUIT_TIME);
                }
                else if(data == GES_BACKWARD_FLAG) 
                {
                    backward();
                    delay(GES_QUIT_TIME);
                }
                else
                {
                    left();
                }          
                break;
            case GES_UP_FLAG:
                delay(GES_ENTRY_TIME);
                paj7620ReadReg(0x43, 1, &data);
                if(data == GES_FORWARD_FLAG) 
                {
                    forward();
                    delay(GES_QUIT_TIME);
                }
                else if(data == GES_BACKWARD_FLAG) 
                {
                    backward();
                    delay(GES_QUIT_TIME);
                }
                else
                {
                    up();
                }          
                break;
            case GES_DOWN_FLAG:
                delay(GES_ENTRY_TIME);
                paj7620ReadReg(0x43, 1, &data);
                if(data == GES_FORWARD_FLAG) 
                {
                    forward();
                    delay(GES_QUIT_TIME);
                }
                else if(data == GES_BACKWARD_FLAG) 
                {
                    backward();
                    delay(GES_QUIT_TIME);
                }
                else
                {
                    down();
                }          
                break;
            case GES_FORWARD_FLAG:
                forward();
                delay(GES_QUIT_TIME);
                break;
            case GES_BACKWARD_FLAG:       
                backward();
                delay(GES_QUIT_TIME);
                break;
            case GES_CLOCKWISE_FLAG:
                clockwise();
                break;
            case GES_COUNT_CLOCKWISE_FLAG:
                anti_clockwise();
                break;  
            default:
                paj7620ReadReg(0x44, 1, &data1);
                if (data1 == GES_WAVE_FLAG) 
                {
                    wave();
                }
                break;
        }
    }
    delay(100);
}

接続

接続はGroveなので簡単です!
P_20191222_205650.jpg

動作確認

だいぶ前に動かしたときの動画です。(記事にするまでが長かった・・・)


終わりに

このセンサーを使えば色々面白そうなことができるなと思っています。
次はこのセンサーを使って何かを操作してみます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした