Wheel IDE

Online demo »

Motor, move

Motor move example

This example shows how to turn the EV3 motors on or off and control the direction of a motor.

Code

#project "Motor move demo"

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

#display "EV3, EV3 Motors"

number active     = 0       ; The last active motor
number lastActive = 0       ; The last known motor position

; Draw four rectangles and fill the active motor rectangle
proc drawScreen()
    number i
    number x = 3
    for i = 0 to 3
        if i == active
            setFillColor(BLACK)
        else
            setFillColor(WHITE)
        end
        setFill(TRUE)
        drawRect(x, 42, 40, 40)
        setFillColor(BLACK)
        setFill(FALSE)
        x += 1
        drawRect(x, 43, 38, 38)
        x += 43
    end
end

; Draw the active motor position
proc drawMotorPos()
    number n = motorRead(active)
    setFill(TRUE)
    setFillColor(WHITE)
    drawRect(68, 14, 48, 9)
    setFill(FALSE)
    setFillColor(BLACK)
    drawRect(66, 12, 52, 13)
    drawNumber(68, 14, n)
end

proc main()
    drawScreen()
    drawMotorPos()
    repeat
        number b       = button()   ; Read the EV3 button...
        number pressed = FALSE      ; Save if up or down was pressed...

        select b
            case BUTTON_LEFT:
                ; Select the previous motor...
                if active == 0
                    active = 3
                else
                    active -= 1
                end
            case BUTTON_RIGHT:
                ; Select the next motor...
                if active == 3
                    active = 0
                else
                    active += 1
                end
            case BUTTON_UP:
                pressed = TRUE
                ; Select the maximum speed...
                motorSetSpeed(active, 100)
            case BUTTON_DOWN:
                pressed = TRUE
                ; Select the minimum speed...
                motorSetSpeed(active, -100)
        end

        if pressed
            ; If the up or down button was pressed then turn the active motor on:
            motorOn(active)
            drawMotorPos()
        else
            ; There was no up or down button pressed, turn the active motor off:
            motorStop(active, MOTOR_BRAKE)
        end

        if lastActive != active
            ; The was a different motor output selected, update the screen...
            drawScreen()
            drawMotorPos()
            sleep(500)
            lastActive = active
        end
    end
end