當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。