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


Arduino Arduino_APDS9960 - readColor()用法及代碼示例

檢索從傳感器讀取的顏色。您可以檢查傳感器是否已讀取顏色,並且可以使用APDS.colorAvailable() 函數檢索顏色。

用法

Int r, g, b;
APDS.readColor(r, g, b);
Int a;
APDS.readColor(r, g, b, a);

參數

此函數需要 3 或 4 個整數變量作為將存儲讀取顏色的參數:

  • r :讀取顏色的紅色分量。
  • g :讀取顏色的綠色分量。
  • b :讀取顏色的藍色分量。
  • a 。環境光強度。

返回

None。

示例

/*
  APDS-9960 - Color Sensor

  This example reads color data from the on-board APDS-9960 sensor of the
  Nano 33 BLE Sense and prints the color RGB (red, green, blue) values
  to the Serial Monitor once a second.

  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 color reading is available.
  while (!APDS.colorAvailable()) {
    delay(5);
  }

  int r, g, b;

  // Read the color.
  APDS.readColor(r, g, b);

  // Print the values:
  Serial.print("r = ");
  Serial.println(r);
  Serial.print("g = ");
  Serial.println(g);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println();

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

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Arduino_APDS9960 - readColor()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。