About this time of year I usually try to present one or more problems on the uLisp Forum, to provide a challenge during the festive season. This year the problem is to write the shortest uLisp program that pulsates the built-in LED smoothly on and off.
Introduction
The classic “Hello World” program in the Arduino ecosystem is Blink: a four-line C program that blinks the LED provided on most Arduino boards. If Blink doesn’t run on your board it’s probably dead (or it’s one of the few boards without an LED).
The Lisp equivalent of the Arduno blink is as follows:
(defun blink ()
(pinmode :led-builtin t)
(loop
(digitalwrite :led-builtin t)
(delay 1000)
(digitalwrite :led-builtin nil)
(delay 1000)))
A slightly more interesting variant of blink is possible if the built-in LED is connected to an output that supports analogue output, using PWM. It’s then possible to pulse the LED smoothly on and off. Here’s a typical program:
(defun pulse ()
(loop
(dotimes (x 256) (analogwrite :led-builtin (- 255 x)) (delay 8))
(dotimes (x 256) (analogwrite :led-builtin x) (delay 8))))
This pulse program outputs a triangular waveform to the LED, repeatedly decreasing the value on the PWM output from 255 to 0, and then increasing it back up again from 0 to 255:
This will work on the following uLisp platforms that support PWM output to the built-in LED:
Arduino Mega 2560, ATmega4809 Curiosity Nano, Arduino Zero, Adafruit Metro M4 Grand Central, Adafruit Metro M4, Adafruit Feather M4, Adafruit PyBadge, Adafruit PyGamer, Adafruit Clue, Adafruit ItsyBitsy nRF52840, Circuit Playground Bluefruit, XIAO nRF52840, Maxim MAX32620FTHR, Teensy 4.0, Teensy 4.1, Raspberry Pi Pico, Raspberry Pi Pico W, Adafruit Feather RP2040, Seeed Studio XIAO RP2040, LILYGO T-Display RP2040, Adafruit Huzzah Feather ESP8266, Adafruit ESP32 Feather, Arduino Uno R4 Minima, and Arduino Uno R4 Wi-Fi.
The puzzle
The above pulse program takes 47 uLisp objects, which you can measure by looking at the space remaining, shown before the ‘>’ prompt, before and after defining the function, and taking the difference.
The problem is to create the smallest possible pulse program with the same behaviour. If you’re getting it below 40 uLisp objects you’re doing well! I’ll give my best answer at the beginning of the New Year.
Previous puzzles
If you missed my previous Lisp puzzles here are links to them:
A small collection of uLisp challenges – with answers
Another collection of uLisp puzzles – with answers
2nd January 2024: I’ve changed the size of the original pulse program to 47 objects, not 50 as originally stated, to correspond to the correct value for 32-bit platforms.