光感應器#

介紹#

光感測器測量物體反射或照射到物體上的光的亮度。它輸出一個類比值,表示偵測到的光量。

VEX V5 光感測器。

本頁面使用 light_sensor 作為範例光感測器名稱。您可以根據需要將其替換為您自己配置的名稱。

以下是可用方法清單:

  • brightness – 返迴光感測器偵測到的光量百分比。

  • changed – 註冊一個函數,當光感測器的亮度值發生變化時,該函數將被呼叫。

建構函式 - 手動初始化光感測器。

  • Light – 建立一個光感測器。

亮度#

brightness 返迴光感測器偵測到的光量,類型為 integer。這可用於檢查物體是否足夠明亮,以便準確地報告其他視覺數據。

Usage:
light_sensor.brightness(units)

範圍

描述

units

可選。傳回亮度的單位: PERCENT (預設值)。

已更改#

changed 註冊一個函數,當光感測器的亮度值改變時,函數將被呼叫。

Usage:
light_sensor.changed(callback, arg)

參數

描述

callback

先前定義的 函數,當光感測器的亮度值改變時執行。

arg

可選。一個包含要傳遞給回呼函數的參數的tuple。有關更多信息,請參閱帶參數的函數。

def my_function():
  brain.screen.print("Brightness changed")

# Call my_function whenever the brightness changes
light_sensor.changed(my_function)

建構函數#

建構函數用於手動建立 Light 對象,這是在 VEXcode 之外配置光感測器所必需的。

Light#

Light 創建一個光傳感器。

Usage:
Light(port)

範圍

描述

port

The 3-Wire Port that the Light Sensor is connected to:

  • On the V5 Brain – brain.three_wire_port.x where x is the number of the port.
  • On a 3-Wire Expander – expander.a where expander is the name of the expander instance.

# Create a Light Sensor in Port A
light_sensor = Light(brain.three_wire_port.a)