Moving Explor3r – Part I

Let’s start creating more interesting Lego robots and see what features from Kotlin we can use. In this post we are going to build a very simple robot called Explor3r. The building instructions are included in this awesome book: The LEGO Mindstorms EV3 Discovery Book by Laurens Valk . Buy it if you can, or you can also find the instructions to build Explor3r here.

Once you finish your robot should look like this:

Let’s write some code in Kotlin to make Explor3r move:

package com.kotlinrobots.usingwhen

import lejos.hardware.motor.EV3LargeRegulatedMotor
import lejos.hardware.port.MotorPort
import lejos.utility.Stopwatch

fun main(args: Array<String>) {

    val motorA = EV3LargeRegulatedMotor(MotorPort.A)
    val motorD = EV3LargeRegulatedMotor(MotorPort.D)
    val watch = Stopwatch()

    motorA.speed = 360 // 1 RPM
    motorD.speed = 360 // 1 RPM


    while (watch.elapsed() < 10000) {
        println("Milliseconds : ${watch.elapsed()}")

        when (watch.elapsed()) {
            in 3000..5000, in 7000..9000 -> {
            else -> {


This is what the code is doing line by line:

Lines 9,10: We are declaring two variables that represent the large motors in our robot.
Line 11: StopWatch is a timer
Lines 21-30: The timer is set to 10 seconds and Explor3r will move backward between second 3 and 5, and between second 7 and 9. Any second outside those ranges, the robot will move forward.
Line 33,34: Do not forget to stop the motors at the end.

In later posts, we will see how to improve this code and make it more idiomatic.

Here is the video:

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *