Wheel IDE

Online demo »

Sensor, values

Sensor value name example

This demo shows how to read and display sensor values, the following sensors are displayed:

Code

#project "Sensor value demo"

#include "lib/modules/standard.whl"
#include "lib/modules/device.whl"
#include "lib/modules/math.whl"
#include "lib/modules/screen.whl"
#include "lib/modules/sensor.whl"

#display "EV3, EV3 Sensors"

number touchValue      = FALSE
number colorValue      = 0
number ultrasonicValue = 0

; Draw the touch value
proc drawTouchValue()
    setFill(FALSE)
    setFillColor(TRUE)
    drawRect(27, 43, 38, 38)
    setFill(TRUE)
    setFillColor(touchValue)
    drawRect(31, 47, 30, 30)
end

; Draw the reflected color value
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)
    drawNumber(86, 57, colorValue)
end

; Draw a the ultrasonic sensor value
proc drawUltrasonicValue()
    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_2)
    number x = 129
    if ultrasonicValue >= 10
        x -= 4
    end
    if ultrasonicValue >= 100
        x -= 4
    end
    drawNumber(x, 57, ultrasonicValue)
end

proc main()
    ; Select the EV3 device in the simulator...
    selectDevice(DEVICE_EV3)

    number value

    drawTouchValue()
    drawColorValue()
    drawUltrasonicValue()
    updateScreen()

    sensorSetType(INPUT_1, SENSOR_TYPE_TOUCH)
    sensorSetType(INPUT_2, SENSOR_TYPE_COLOR)
    sensorSetMode(INPUT_2, COLOR_COLOR)
    sensorSetType(INPUT_3, SENSOR_TYPE_ULTRASONIC)
    sensorSetMode(INPUT_3, ULTRASONIC_CM)

    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)
        value = round(value)
        if value != ultrasonicValue
            ultrasonicValue = value
            drawUltrasonicValue()
        end

        updateScreen()
    end
end