Wheel IDE

Online demo »

Sensor

This file contains procedures to read input from EV3 and Powered Up sensors.

Constants

The sensor module.
Source: lib/modules/sensor.whl, line: 10
MODULE_SENSOR7

Module calls for reading sensors.
Source: lib/modules/sensor.whl, line: 13
SENSOR_SET_TYPE0
SENSOR_GET_TYPE1
SENSOR_SET_MODE2
SENSOR_RESET3
SENSOR_READ4

The different sensor types.
Source: lib/modules/sensor.whl, line: 20
SENSOR_TYPE_NXT_TOUCH1
SENSOR_TYPE_NXT_LIGHT2
SENSOR_TYPE_NXT_SOUND3
SENSOR_TYPE_NXT_COLOR4
SENSOR_TYPE_NXT_ULTRASONIC5
 SENSOR_TYPE_NXT_TEMPERATURE6
SENSOR_TYPE_TOUCH16
SENSOR_TYPE_COLOR29
SENSOR_TYPE_ULTRASONIC30
SENSOR_TYPE_GYRO32
SENSOR_TYPE_INFRARED33
SENSOR_TYPE_SPIKE_DISTANCE40
SENSOR_TYPE_SPIKE_COLOR41
SENSOR_TYPE_SPIKE_FORCE42

The sensor read value is invalid.
Source: lib/modules/sensor.whl, line: 36
SENSOR_VALUE_INVALID-1

Sensor input ports.
Source: lib/modules/sensor.whl, line: 39
INPUT_10
INPUT_21
INPUT_32
INPUT_43

Color sensor modes.
Source: lib/modules/sensor.whl, line: 45
COLOR_REFLECTED0
COLOR_AMBIENT1
COLOR_COLOR2
COLOR_REFLECTED_RAW3
COLOR_RGB_RAW4
COLOR_CALIBRATION5

Color values.
Source: lib/modules/sensor.whl, line: 53
COLOR_NONE0
COLOR_BLACK1
COLOR_BLUE2
COLOR_GREEN3
COLOR_YELLOW4
COLOR_RED5
COLOR_WHITE6
COLOR_BROWN7
COLOR_VIOLET8
COLOR_AZURE9

Button values, used for the infrared sensor.
Source: lib/modules/sensor.whl, line: 65
BUTTON_00
BUTTON_11
BUTTON_22
BUTTON_33
BUTTON_44
BUTTON_55
BUTTON_66
BUTTON_77
BUTTON_88
BUTTON_99
BUTTON_1010
BUTTON_1111

Infrared sensor modes.
Source: lib/modules/sensor.whl, line: 79
ULTRASONIC_CM0
ULTRASONIC_INCH1
ULTRASONIC_LISTEN2
ULTRASONIC_SI_CM3
ULTRASONIC_SI_INCH4
ULTRASONIC_DC_CM5
ULTRASONIC_DC_INCH6

Gyro sensor modes.
Source: lib/modules/sensor.whl, line: 88
GYRO_ANGLE0
GYRO_RATE1
GYRO_FAST2
GYRO_RATE_AND_ANGLE3
GYRO_CALIBRATION4

Infrared sensor modes.
Source: lib/modules/sensor.whl, line: 95
IR_PROXIMITY0
IR_SEEKER1
IR_REMOTE2
IR_REMOTE_ADVANCED3
IR_NOT_UTILIZED4
IR_CALIBRATION5

Procedures

sensorLayerSetType
Source: lib/modules/sensor.whl, line: 103
E

Set the sensor type.

proc sensorLayerSetType(number layer, number port, number type)
Parameters:
NameTypeDescription
layernumberThe layer, the daisy chained device. 0 is the primary brick.
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
typenumberThe type of sensor: SENSOR_TYPE_TOUCH, SENSOR_TYPE_COLOR, SENSOR_TYPE_INFRARED, SENSOR_TYPE_ULTRASONIC, SENSOR_TYPE_GYRO or SENSOR_TYPE_SOUND.

sensorLayerGetType
Source: lib/modules/sensor.whl, line: 114
E P

get the sensor type.

proc sensorLayerGetType(number layer, number port)
Parameters:
NameTypeDescription
layernumberThe layer, the daisy chained device. 0 is the primary brick.
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
Return: The type of connected sensor.


sensorLayerSetMode
Source: lib/modules/sensor.whl, line: 124
E P

Some sensors like the color or infrared sensor can have different modes. With this function you can select the mode.

proc sensorLayerSetMode(number layer, number port, number mode)
Parameters:
NameTypeDescription
layernumberThe layer, the daisy chained device. 0 is the primary brick.
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
modenumber

sensorLayerReset
Source: lib/modules/sensor.whl, line: 135
E

Reset the sensor, this function can only be used for the qyro sensor.

proc sensorLayerReset(number layer, number port)
Parameters:
NameTypeDescription
layernumberThe layer, the daisy chained device. 0 is the primary brick.
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.

sensorLayerRead
Source: lib/modules/sensor.whl, line: 144
E P

Read the sensor value.

proc sensorLayerRead(number layer, number port)
Parameters:
NameTypeDescription
layernumberThe layer, the daisy chained device. 0 is the primary brick.
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
Return: The sensor value.


sensorSetType
Source: lib/modules/sensor.whl, line: 154
E

Set the sensor type.

proc sensorSetType(number port, number type)
Parameters:
NameTypeDescription
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
typenumberThe type of sensor: SENSOR_TYPE_TOUCH, SENSOR_TYPE_COLOR, SENSOR_TYPE_INFRARED, SENSOR_TYPE_ULTRASONIC, SENSOR_TYPE_GYRO or SENSOR_TYPE_SOUND.

sensorGetType
Source: lib/modules/sensor.whl, line: 163
E P

Get the sensor type from the primary device.

proc sensorGetType(number port)
Parameters:
NameTypeDescription
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
Return: The type of connected sensor.


sensorSetMode
Source: lib/modules/sensor.whl, line: 171
E P

Some sensors line the color or infrared sensor can have different modes. With this function you can select the mode.

proc sensorSetMode(number port, number mode)
Parameters:
NameTypeDescription
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
modenumberThe sensor mode.

sensorReset
Source: lib/modules/sensor.whl, line: 180
E

Reset the sensor, this function can only be used for the qyro sensor.

proc sensorReset(number port)
Parameters:
NameTypeDescription
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.

sensorRead
Source: lib/modules/sensor.whl, line: 187
E P

Read the sensor value.

proc sensorRead(number port)
Parameters:
NameTypeDescription
portnumberThe input port, the first output is 0. Constants: INPUT_1, INPUT_2, INPUT_3, INPUT_4.
Return: The sensor value.

Touch, color and infrared sensor

#project "Sensor demo"

#include "lib/modules/standard.whl"
#include "lib/modules/screen.whl"
#include "lib/modules/button.whl"
#include "lib/modules/sensor.whl"

string colors[8]     = ["non", "blk", "blu", "grn", "yel", "red", "wht", "brn"]
number touchValue    = FALSE
number colorValue    = COLOR_NONE
number infraredValue = BUTTON_0

proc drawTouchValue()
    setFill(FALSE)
    setFillColor(TRUE)
    drawRect(27, 43, 38, 38)
    setFill(TRUE)
    setFillColor(touchValue)
    drawRect(31, 47, 30, 30)
end

proc drawColorValue()
    setFill(FALSE)
    setFillColor(TRUE)
    drawRect(71, 43, 38, 38)
    setFill(TRUE)
    setFillColor(WHITE)
    drawRect(72, 44, 36, 36)
    setFill(FALSE)
    setFillColor(BLACK)
    setTextSize(TEXT_SIZE_2)
    drawText(78, 56, colors[colorValue])
end

proc drawInfraredValue()
    setFill(FALSE)
    setFillColor(TRUE)
    drawRect(115, 43, 38, 38)
    setFill(TRUE)
    setFillColor(WHITE)
    drawRect(116, 44, 36, 36)
    setFill(FALSE)
    setFillColor(BLACK)
    setTextSize(TEXT_SIZE_3)
    drawNumber(124, 55, infraredValue)
end

proc main()
    number value

    drawTouchValue()
    drawColorValue()
    drawInfraredValue()

    sensorSetType(INPUT_1, SENSOR_TYPE_TOUCH)
    sensorSetType(INPUT_2, SENSOR_TYPE_COLOR)
    sensorSetType(INPUT_3, SENSOR_TYPE_INFRARED)

    repeat
        value = sensorRead(INPUT_1)
        if value != touchValue
            touchValue = value
            drawTouchValue()
        end

        value = sensorRead(INPUT_2)
        if value != colorValue
            colorValue = value
            drawColorValue()
        end

        value = sensorRead(INPUT_3)
        if value != infraredValue
            infraredValue = value
            drawInfraredValue()
        end
    end
end