Wheel IDE

Online demo »

Distance sensor

Powered Up Distance example

This example shows how to read values from the distance sensor.

The following call selects the Move Hub in the simulator, this is only necessary to run this demo without an actual connection to a device...

If you are using a Move Hub then this exemple expects the distance sensor to be connected to port "C". If you are using the Hub -which has only two ports- then you can change the value POWERED_UP_PORT_C to POWERED_UP_PORT_A and connect the sensor to port "A"...

Code

#project "Powered Up Distance sensor example"

#include "lib/modules/standard.whl"
#include "lib/modules/device.whl"
#include "lib/modules/poweredUp.whl"
#include "lib/modules/light.whl"
#include "lib/modules/sensor.whl"

#display "Hub"

proc main()
    ; Select the device in the simulator...
    poweredUpSetDevice(LAYER_1, POWERED_UP_MOVE_HUB)

    ; Use port "C"...
    number inputPort = POWERED_UP_PORT_C

    sensorLayerSetMode(LAYER_1, inputPort, POWERED_UP_SENSOR_MODE_DISTANCE)

    number lastValue = -1

    repeat
        ; Read the color value:
        number value = sensorLayerRead(LAYER_1, inputPort)

        ; Check if the value is changed:
        if value != lastValue
            ; The value is changed!
            lastValue = value

            ; Display the new distance value...
            clearConsole()
            printS("Distance:")
            printN(value)
        end
    end
end
See also: light