手势传感器示例

来自Microduino Wikipedia
跳转至: 导航搜索


手势传感器示例


识别检测上、下、左、右、远、近的手势

所需硬件


电路搭建

将Battery、Core、Hub堆叠在一起,使用4pin传感器线连接传感器与Hub扩展板的IIC接口,通过MicroUSB数据线接入电脑。

代码

#include <Microduino_Gesture.h>

// Global Variables
Gesture gestureSensor;

void setup() {
  // Initialize Serial port
  Serial.begin(9600);

  // Initialize Gesture (configure I2C and initial values)
  if ( gestureSensor.begin() ) {
    Serial.println(F("Gesture initialization complete"));
  } else {
    Serial.println(F("Something went wrong during Gesture init!"));
  }

  if (gestureSensor.setGestureGain(GGAIN_2X)) { //GGAIN_1X GGAIN_2X GGAIN_4X GGAIN_8X
    Serial.println(F("Gesture setGestureGain complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 setGestureGain!"));
  }

  // Start running the Gesture gesture sensor engine
  if ( gestureSensor.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if ( gestureSensor.isGestureAvailable() ) {
    switch ( gestureSensor.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}
  • 该例程效果为:下载程序后,保持USB线连接,打开串口(波特率设置为9600),观察返回值。
    • 分别向上、下、左、右、远、近挥手,测试是否识别到相应手势
    • 标准动作:(以向左挥手为例)从sensor右边,水平匀速挥手到sensor左边
    • 推荐距离:手与sensor垂直距离10cm内为最佳
    • 向右挥手,观察串口返回值为RIGHT
    • 注:动作是否标准会影响识别的准确度,使用最佳距离在10cm以内



返回Sensor-Gesture库语法手册