当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino Arduino_APDS9960 - readProximity()用法及代码示例


从传感器检索接近度读数。您可以检查传感器是否已读取接近度,并且可以使用APDS.proximityAvailable() 函数检索。

用法

APDS.readProximity()

参数

None。

返回

检测到的接近度可能在 0 到 255 之间,其中 0 是最近的,255 是最远的。如果发生错误,该函数将返回 -1。

示例

/*
  APDS-9960 - Proximity Sensor

  This example reads proximity data from the on-board APDS-9960 sensor of the
  Nano 33 BLE Sense and prints the proximity value to the Serial Monitor
  every 100 ms.

  The circuit:
  - Arduino Nano 33 BLE Sense.

  This example code is in the public domain.
*/

#include <Arduino_APDS9960.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS-9960 sensor.");
  }
}

void loop() {
  // Check if a proximity reading is available.
  if (APDS.proximityAvailable()) {
    // Read the proximity where:
    // - 0   => close
    // - 255 => far
    // - -1  => error
    int proximity = APDS.readProximity();

    // Print value to the Serial Monitor.
    Serial.println(proximity);
  }

  // Wait a bit before reading again.
  delay(100);
}

相关用法


注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 Arduino_APDS9960 - readProximity()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。