Wheel IDE

Online demo »

Buttons

Button example

This example shows how to read the EV3 buttons. The program has a main loop in which the buttons are read. If the button value is different from the last value then a switch statement is used to check which button is pressed.

Code

; Wheel, copyright (c) 2017 - present by Arno van der Vegt
; Distributed under an MIT license: https://arnovandervegt.github.io/wheel/license.txt
#project "EV3 Button example"

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

#display "EV3"

proc drawUp()
    drawRect(71, 21, 38, 18)
end

proc drawDown()
    drawRect(71, 89, 38, 18)
end

proc drawCenter()
    drawRect(71, 45, 38, 38)
end

proc drawLeft()
    drawRect(47, 45, 18, 38)
end

proc drawRight()
    drawRect(115, 45, 18, 38)
end

proc drawScreen()
    setFill(FALSE)
    setFillColor(BLACK)
    ; Up
    drawRect(70, 20, 40, 20)
    ; Down
    drawRect(70, 88, 40, 20)
    ; Center
    drawRect(70, 44, 40, 40)
    ; Left
    drawRect(46, 44, 20, 40)
    ; Right
    drawRect(114, 44, 20, 40)
    updateScreen()
end

proc main()
    selectDevice(DEVICE_EV3)
    drawScreen()
    setFill(TRUE)
    number lastButton = 0
    repeat
        number b = button()   ; Read the EV3 button...
        if lastButton != b
            number left   = WHITE
            number right  = WHITE
            number center = WHITE
            number up     = WHITE
            number down   = WHITE
            select b
                case BUTTON_LEFT:
                    left = BLACK

                case BUTTON_RIGHT:
                    right = BLACK

                case BUTTON_CENTER:
                    center = BLACK

                case BUTTON_UP:
                    up = BLACK

                case BUTTON_DOWN:
                    down = BLACK
            end
            lastButton = b
            setFillColor(up)
            drawUp()
            setFillColor(down)
            drawDown()
            setFillColor(center)
            drawCenter()
            setFillColor(left)
            drawLeft()
            setFillColor(right)
            drawRight()
            updateScreen()
        end
    end
end

Screenshot

See also: button