Drawing with Kotlin : Spirograph

In this new post, we will learn how to build a spirograph and make it draw by using Kotlin. The code is pretty basic as we only need to move two motors with different speeds.

Here is the code:

package com.kotlinrobots.spirograph

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

fun main(args: Array<String>) {

    val penMotor = EV3LargeRegulatedMotor(MotorPort.D)
    val tableMotor = EV3LargeRegulatedMotor(MotorPort.A)

    penMotor.speed = 750
    tableMotor.speed = 5

    tableMotor.forward()
    penMotor.forward()

    Delay.msDelay(80000)

    penMotor.close()
    tableMotor.close()
}

Basically, we have two motors that need to rotate with different speeds or the same if you like. We can create different shapes by changing the speed values and the position of the pen.

Here are two shapes the spirograph drew:

Source Code

Miguel

Leave a Reply

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