Minimal Kotlin Pi4J example

For full documentation, visit the Kotlin Docs

This is a minimal working example, make sure to check it out from the link above for the full introduction and comments.

const val PIN_BUTTON = 24 // PIN 18 = BCM 24
const val PIN_LED = 22 // PIN 15 = BCM 22
var pressCount = 0

console {
  title("<-- The Pi4J Project -->", "Minimal Example project")
  pi4j {
    digitalInput(PIN_BUTTON) {
        id("button")
        name("Press button")
        pull(PullResistance.PULL_DOWN)
        debounce(3000L)
        piGpioProvider()
      }.onLow {
        pressCount++
        +"Button was pressed for the ${pressCount}th time"
      }
    }
    
    digitalOutput(PIN_LED) {
        id("led")
        name("LED Flasher")
        shutdown(DigitalState.LOW)
        initial(DigitalState.LOW)
        piGpioProvider()
      }.run {
        while (pressCount < 5) {
          if (isHigh) {
            +"LED low"
            low()
          } else {
            +"LED high"
            high()
          }
          Thread.sleep((500L / (pressCount + 1)))
        }
      }
    }
  }
}