44 ready-to-run examples

From beginner blink to advanced state machines. Every example runs headlessly with hil-cli simulate.

Your first programs

No board yet? Every example runs on the built-in virtual Arduino: add --hw → hil-cli simulate --hw blink.hil, or go interactive with HIL_PORT=virtual hil-cli run.
hello-world.hil
Send a message to the serial monitor
beginner
LISP
; Your first HIL-OS program (LOG "Hello, HIL-OS!") (LOG "Welcome to Hardware-in-the-Loop programming.") (LOG "Edit this file, then run: hil-cli simulate hello-world.hil")
View code
blink.hil
Classic Arduino blink, LED on pin 13 on/off every 500ms
beginner
LISP
(LOG "Starting blink on pin 13...") (WHILE 1 (DO (WRITE D 13 1) (LOG "LED ON") (DELAY 500) (WRITE D 13 0) (LOG "LED OFF") (DELAY 500) ) )
View code
variables.hil
SET variables, expressions, and variable-driven loops
beginner
LISP
(SET led_pin 13) (SET delay_ms 300) (SET counter 0) (LOG "Variables initialized.") (LOG "led_pin = " led_pin) (SET x 10) (SET y 3) (SET sum (+ x y)) (SET product (* x y)) (LOG "x + y = " sum) (LOG "x * y = " product) (WHILE (< counter 5) (DO (WRITE D led_pin 1) (DELAY delay_ms) (WRITE D led_pin 0) (DELAY delay_ms) (SET counter (+ counter 1)) (LOG "Blink #" counter) ) ) (LOG "Done! Total blinks: " counter)
View code

Arithmetic, comparison, logic, math

arithmetic.hil
All arithmetic operators: + - * / %
beginner
LISP
(SET a 17) (SET b 5) (LOG "a = " a) (LOG "b = " b) (LOG "a + b = " (+ a b)) (LOG "a - b = " (- a b)) (LOG "a * b = " (* a b)) (LOG "a / b = " (/ a b)) (LOG "a % b = " (% a b)) (SET result (* (+ a b) (- a b))) (LOG "(a+b)*(a-b) = " result) (LOG "10 / 0 = " (/ 10 0)) (SET n 42) (LOG "n % 2 = " (% n 2)) (IF (== (% n 2) 0) (LOG "n is even") (LOG "n is odd") )
View code
comparison.hil
All comparison operators: == != > < <= >=
beginner
LISP
(SET x 42) (LOG "x = " x) (LOG "(== x 42) = " (== x 42)) (LOG "(== x 0) = " (== x 0)) (LOG "(!= x 42) = " (!= x 42)) (LOG "(!= x 0) = " (!= x 0)) (LOG "(> x 10) = " (> x 10)) (LOG "(< x 10) = " (< x 10)) (LOG "(>= x 42) = " (>= x 42)) (LOG "(>= x 43) = " (>= x 43)) (LOG "(<= x 42) = " (<= x 42)) (LOG "(<= x 41) = " (<= x 41)) (SET temp 28) (IF (>= temp 30) (LOG "WARNING: Temperature too high!") (IF (<= temp 15) (LOG "WARNING: Temperature too low!") (LOG "Temperature is within range.") ) )
View code
boolean-logic.hil
Logical operators: AND, OR, NOT with truth tables
beginner
LISP
(SET a 1) (SET b 0) (LOG "AND truth table:") (LOG " (AND 1 1) = " (AND 1 1)) (LOG " (AND 1 0) = " (AND 1 0)) (LOG " (AND 0 0) = " (AND 0 0)) (LOG "OR truth table:") (LOG " (OR 1 1) = " (OR 1 1)) (LOG " (OR 1 0) = " (OR 1 0)) (LOG " (OR 0 0) = " (OR 0 0)) (LOG "NOT truth table:") (LOG " (NOT 0) = " (NOT 0)) (LOG " (NOT 1) = " (NOT 1)) (SET temp 25) (SET humidity 70) (LOG "temp > 20 AND humidity > 60: " (AND (> temp 20) (> humidity 60))) (SET light 500) (SET motion 1) (IF (AND (> light 400) (== motion 1)) (LOG "Conditions met: bright and motion detected") (LOG "Conditions not met") ) (SET fan_on 0) (IF (NOT fan_on) (LOG "Fan is OFF — turning it on now") (LOG "Fan is already ON") )
View code
math-functions.hil
MAP, CONSTRAIN, RANDOM, MIN, MAX, ABS
intermediate
LISP
(SET raw_adc 512) (SET pwm_val (MAP raw_adc 0 1023 0 255)) (LOG "ADC 512 maps to PWM: " pwm_val) (SET unconstrained 500) (SET clamped (CONSTRAIN unconstrained 0 255)) (LOG "500 constrained to [0,255] = " clamped) (SET dice (RANDOM 1 6)) (LOG "Dice roll: " dice) (SET x 15) (SET y 42) (LOG "MIN(15, 42) = " (MIN x y)) (LOG "MAX(15, 42) = " (MAX x y)) (LOG "ABS(-42) = " (ABS -42)) (LOG "ABS(10) = " (ABS 10)) (SET sensor (READ A 0)) (SET output (CONSTRAIN (MAP sensor 0 1023 0 255) 0 255)) (LOG "Sensor: " sensor " -> PWM: " output) (SET setpoint 100) (SET current 75) (SET error (ABS (- setpoint current))) (LOG "Error magnitude: " error)
View code

IF, WHILE, FOR, BREAK, COND

if-else.hil
Conditional branching with IF (single and nested)
beginner
LISP
(SET temp 25) (LOG "Temperature: " temp " C") (IF (> temp 30) (LOG "It is hot!") ) (IF (> temp 30) (LOG "Hot — fan ON") (LOG "Cool — fan OFF") ) (SET level (READ A 0)) (IF (> level 800) (LOG "NIGHT: LED ON, street lights active") (IF (> level 400) (LOG "DUSK: LED ON dimly") (LOG "DAY: LED OFF") ) ) (SET status (IF (> temp 25) 1 0)) (LOG "Status: " status)
View code
while-loop.hil
WHILE loops: condition-checked iteration
beginner
LISP
(SET count 5) (LOG "Countdown:") (WHILE (> count 0) (DO (LOG count "...") (SET count (- count 1)) ) ) (LOG "LIFTOFF!") (SET threshold 300) (SET reading 500) (SET attempts 0) (WHILE (> reading threshold) (DO (SET reading (- reading 100)) (SET attempts (+ attempts 1)) (LOG "Attempt #" attempts ": reading=" reading) ) ) (LOG "Threshold reached after " attempts " attempts")
View code
for-loop.hil
FOR loops: counter-based iteration
intermediate
LISP
(LOG "Counting 0 to 4:") (FOR i 0 5 (LOG "i = " i) ) (SET sum 0) (FOR i 0 10 (SET sum (+ sum i)) ) (LOG "Sum 0..9 = " sum) (SET product 1) (FOR i 1 6 (SET product (* product i)) ) (LOG "1*2*3*4*5 = " product) (LOG "LED chase on pins 9,10,11:") (FOR i 0 3 (DO (WRITE D (+ 9 i) 1) (DELAY 200) (WRITE D (+ 9 i) 0) ) ) (LOG "Chase complete")
View code
break-continue.hil
BREAK and CONTINUE: loop control
intermediate
LISP
(LOG "--- BREAK Demo ---") (SET i 0) (WHILE (< i 100) (DO (IF (== i 5) (DO (LOG "Breaking at i=5!") (BREAK) ) ) (SET i (+ i 1)) ) (LOG "WHILE exited at i=" i)) (LOG "--- CONTINUE Demo ---") (FOR i 0 10 (DO (IF (== (% i 2) 0) (CONTINUE) ) (LOG "Odd number: " i) ) ) (LOG "--- Nested BREAK Demo ---") (SET outer_count 0) (SET break_count 0) (WHILE (< outer_count 3) (DO (SET inner 0) (WHILE (< inner 10) (DO (IF (== inner 2) (BREAK)) (SET inner (+ inner 1)) (SET break_count (+ break_count 1)) ) ) (SET outer_count (+ outer_count 1)) ) ) (LOG "Inner iterations per outer: 2 (break at 2)") (LOG "Total inner steps: " break_count)
View code
cond-multi-branch.hil
COND: multi-branch conditional (classic LISP)
intermediate
LISP
(SET command 2) (COND ((== command 1) (LOG "Command: START motor")) ((== command 2) (LOG "Command: STOP motor")) ((== command 3) (LOG "Command: REVERSE motor")) (1 (LOG "Command: UNKNOWN — no action")) ) (SET state 0) (LOG "--- State Machine ---") (SET tick 0) (WHILE (< tick 5) (DO (SET tick (+ tick 1)) (COND ((== state 0) (DO (LOG "State: IDLE -> RUNNING") (SET state 1) ) ) ((== state 1) (DO (LOG "State: RUNNING -> tick=" tick) (IF (>= tick 3) (SET state 2)) ) ) ((== state 2) (DO (LOG "State: STOPPED -> IDLE") (SET state 0) ) ) ) ) ) (LOG "State machine demo complete.")
View code

DEFUN and recursion

defun-basics.hil
Define reusable functions with parameters
intermediate
LISP
(DEFUN BLINK (pin count delay_ms) (WHILE (> count 0) (DO (WRITE D pin 1) (DELAY delay_ms) (WRITE D pin 0) (DELAY delay_ms) (SET count (- count 1)) ) ) ) (DEFUN CELSIUS_TO_FAHRENHEIT (c) (+ (/ (* c 9) 5) 32) ) (DEFUN MAX3 (a b c) (IF (AND (>= a b) (>= a c)) a (IF (>= b c) b c) ) ) (BLINK 13 3 200) (LOG "Blink done.") (SET temp_c 25) (SET temp_f (CELSIUS_TO_FAHRENHEIT temp_c)) (LOG temp_c "C = " temp_f "F") (SET m (MAX3 10 42 7)) (LOG "Max(10, 42, 7) = " m)
View code
recursion.hil
Factorial, Fibonacci, and power via recursion
advanced
LISP
(DEFUN FACTORIAL (n) (IF (<= n 1) 1 (* n (FACTORIAL (- n 1)))) ) (DEFUN FIB (n) (IF (<= n 2) n (+ (FIB (- n 1)) (FIB (- n 2)))) ) (DEFUN POWER (base exp) (IF (== exp 0) 1 (* base (POWER base (- exp 1)))) ) (DEFUN PULSE (pin count) (IF (> count 0) (DO (WRITE D pin 1) (DELAY 100) (WRITE D pin 0) (DELAY 100) (PULSE pin (- count 1)) ) ) ) (LOG "5! = " (FACTORIAL 5)) (LOG "fib(10) = " (FIB 10)) (LOG "2^8 = " (POWER 2 8)) (PULSE 13 5) (LOG "Pulse sequence complete.")
View code

LAMBDA, closures, and higher-order functions

lambda-basic.hil
Anonymous functions with LAMBDA and closures
intermediate
LISP
;; Lambda: create an anonymous function (SET square (LAMBDA (x) (* x x))) (LOG "5 squared = " (square 5)) ;; Immediately-invoked: define AND call in one breath (LOG "3^2 + 4^2 = " ((LAMBDA (a b) (+ (* a a) (* b b))) 3 4)) ;; Closure factory — the returned function REMEMBERS n (SET make-adder (LAMBDA (n) (LAMBDA (x) (+ x n)))) (SET add5 (make-adder 5)) (LOG "5 + 5 = " (add5 5)) (LOG "10 + 5 = " (add5 10)) ;; Lambda returns a function (SET make-multiplier (LAMBDA (factor) (LAMBDA (x) (* x factor)))) (SET double (make-multiplier 2)) (SET triple (make-multiplier 3)) (LOG "double(7) = " (double 7)) (LOG "triple(7) = " (triple 7)) ;; Stateful closure: private persistent state, no globals (DEFUN MAKE-COUNTER () (DO (SET n 0) (LAMBDA () (DO (SET n (+ n 1)) n)))) (SET tick (MAKE-COUNTER)) (tick) (tick) (LOG "tick third call = " (tick))
View code
lambda-hardware.hil
Passing lambda functions as callbacks for hardware events
intermediate
LISP
;; Lambdas as hardware callbacks and behavior strategies ;; Strategy pattern: pick a blink style at runtime (SET fast-style (LAMBDA () (DO (WRITE D 13 1) (DELAY 60) (WRITE D 13 0) (DELAY 60)))) (SET calm-style (LAMBDA () (DO (WRITE D 13 1) (DELAY 400) (WRITE D 13 0) (DELAY 400)))) ;; Higher-order: run any style N times (DEFUN RUN-STYLE (style times) (IF (> times 0) (DO (style) (RUN-STYLE style (- times 1))))) (RUN-STYLE calm-style 3) (RUN-STYLE fast-style 10) ;; Closure capturing a pin — one factory, many LEDs (SET make-led (LAMBDA (pin) (LAMBDA (state) (WRITE D pin state)))) (SET led13 (make-led 13)) (SET led12 (make-led 12)) (led13 1) (led12 1) (DELAY 500) (led13 0) (led12 0) ;; Register a lambda as an event callback (ON-EVENT 2 FALLING (LAMBDA () (TOGGLE D 13))) (LOG "Lambda hardware demo complete.")
View code

Read the physical world

lm35-temperature.hil
LM35 temperature sensor with overheat alarm
intermediate
LISP
(SET sensor_pin 0) (SET led_pin 13) (SET buzzer_pin 11) (SET alarm_threshold 30) (LOG "=== LM35 Temperature Monitor ===") (WHILE 1 (DO (SET temp (LM35 sensor_pin)) (LOG "Temperature: " temp " C") (IF (>= temp alarm_threshold) (DO (LOG "!! OVERHEAT WARNING !!") (WRITE D led_pin 1) (TONE buzzer_pin 1000 200) ) (WRITE D led_pin 0) ) (DELAY 1000) ) )
View code
ultrasonic-distance.hil
HC-SR04 distance sensor with proximity buzzer
intermediate
LISP
(SET trig_pin 7) (SET echo_pin 8) (SET led_pin 13) (SET buzzer_pin 11) (SET warning_dist 20) (LOG "=== Ultrasonic Distance Sensor ===") (WHILE 1 (DO (SET dist (ULTRASONIC trig_pin echo_pin)) (LOG "Distance: " dist " cm") (IF (AND (<= dist warning_dist) (> dist 0)) (DO (LOG "!! OBJECT TOO CLOSE !!") (WRITE D led_pin 1) (TONE buzzer_pin (MAP dist 0 20 2000 500) 100) ) (WRITE D led_pin 0) ) (DELAY 100) ) )
View code
ldr-night-light.hil
Light-dependent resistor with hysteresis
intermediate
LISP
(SET ldr_pin 0) (SET led_pin 13) (SET dark_threshold 800) (SET bright_threshold 400) (SET is_on 0) (LOG "=== Smart Night Light ===") (WHILE 1 (DO (SET light (READ A ldr_pin)) (IF (AND (>= light dark_threshold) (== is_on 0)) (DO (WRITE D led_pin 1) (SET is_on 1) (LOG "NIGHTFALL (LDR=" light ") — LED ON") ) ) (IF (AND (<= light bright_threshold) (== is_on 1)) (DO (WRITE D led_pin 0) (SET is_on 0) (LOG "SUNRISE (LDR=" light ") — LED OFF") ) ) (DELAY 500) ) )
View code
dht-humidity.hil
DHT22 temperature and humidity sensor
intermediate
LISP
(SET data_pin 4) (SET sensor_type 22) (LOG "=== DHT22 Sensor Reading ===") (WHILE 1 (DO (DHT-READ data_pin sensor_type) (LOG "DHT22 reading complete on pin " data_pin) (DELAY 2000) ) )
View code

Servo, PWM, NeoPixel, Relay

servo-sweep.hil
Servo motor sweeps 0 to 180 degrees
intermediate
LISP
(SET servo_pin 9) (LOG "=== Servo Sweep Demo ===") (FOR angle 0 181 (DO (SERVO servo_pin angle) (DELAY 15) ) ) (LOG "Sweep complete — returning to 0.") (SERVO servo_pin 0) (DELAY 500)
View code
pwm-fan-control.hil
Temperature-based PWM fan speed control
intermediate
LISP
(SET fan_pin 9) (SET sensor_pin 0) (LOG "=== PWM Fan Controller ===") (WHILE 1 (DO (SET temp (LM35 sensor_pin)) (IF (>= temp 35) (DO (WRITE P fan_pin 255) (LOG "CRITICAL: Fan 100% — Temp: " temp "C") ) (IF (>= temp 28) (DO (SET speed (CONSTRAIN (MAP temp 28 35 128 255) 0 255)) (WRITE P fan_pin speed) (LOG "WARM: Fan " speed "/255 — Temp: " temp "C") ) (IF (>= temp 22) (DO (SET speed (CONSTRAIN (MAP temp 22 28 50 128) 0 255)) (WRITE P fan_pin speed) (LOG "MILD: Fan " speed "/255 — Temp: " temp "C") ) (DO (WRITE P fan_pin 0) (LOG "COOL: Fan OFF — Temp: " temp "C") ) ) ) ) (DELAY 1000) ) )
View code
neopixel-colors.hil
NeoPixel RGB color cycling and fade
intermediate
LISP
(SET neo_pin 6) (LOG "=== NeoPixel Color Demo ===") (NEO-SET neo_pin 0 255 0) (NEO-SHOW neo_pin) (LOG "Red") (DELAY 1000) (NEO-SET neo_pin 255 0 0) (NEO-SHOW neo_pin) (LOG "Green") (DELAY 1000) (NEO-SET neo_pin 0 0 255) (NEO-SHOW neo_pin) (LOG "Blue") (DELAY 1000) (LOG "Fading through RGB...") (FOR r 0 255 (DO (NEO-SET neo_pin r 0 0) (NEO-SHOW neo_pin)) ) (FOR g 0 255 (DO (NEO-SET neo_pin 0 g 0) (NEO-SHOW neo_pin)) ) (FOR b 0 255 (DO (NEO-SET neo_pin 0 0 b) (NEO-SHOW neo_pin)) ) (NEO-SET neo_pin 0 0 0) (NEO-SHOW neo_pin) (LOG "Color demo complete.")
View code
relay-switch.hil
Button-toggled relay with debounce
intermediate
LISP
(SET relay_pin 7) (SET button_pin 2) (SET state 0) (LOG "=== Relay Toggle Switch ===") (PIN-MODE button_pin 0) (PIN-MODE relay_pin 1) (WRITE D relay_pin 1) (WHILE 1 (DO (SET btn (READ D button_pin)) (IF (== btn 1) (DO (SET state (IF (== state 0) 1 0)) (WRITE D relay_pin (IF (== state 1) 0 1)) (IF (== state 1) (LOG "Relay ON — device powered") (LOG "Relay OFF — device disconnected") ) (DELAY 300) ) ) (DELAY 50) ) )
View code

LCD, I2C, SPI, SD

lcd-display.hil
16x2 LCD display with counter
intermediate
LISP
(LOG "=== LCD Display Demo ===") (LCD-INIT 7 6 5 4 3 2) (LCD-CLEAR) (LCD-PRINT 0 0 "HIL-OS v11") (SET count 0) (WHILE (< count 10) (DO (LCD-CLEAR) (LCD-PRINT 0 0 "HIL-OS v11") (LCD-PRINT 1 0 "Count: " count) (DELAY 500) (SET count (+ count 1)) ) ) (LCD-CLEAR) (LCD-PRINT 0 0 "Demo Complete!") (LOG "LCD demo finished.")
View code
i2c-rtc.hil
I2C communication with DS3231 RTC
advanced
LISP
(SET rtc_addr 0x68) (LOG "=== I2C RTC Reader ===") (I2C-WRITE rtc_addr 0x00 0x00) (SET seconds (I2C-READ rtc_addr 0x00)) (LOG "Raw seconds: " seconds) (I2C-WRITE rtc_addr 0x01 0x00) (SET minutes (I2C-READ rtc_addr 0x01)) (LOG "Raw minutes: " minutes) (WHILE 1 (DO (SET ts (RTC-READ)) (LOG "RTC timestamp: " ts) (DELAY 5000) ) )
View code

Complete real-world applications

morse-sos.hil
Emergency SOS beacon using LED + buzzer
advanced
LISP
(LOG ">> Initiating Emergency SOS Beacon...") (SET led_pin 13) (SET buz_pin 11) (SET freq 750) (SET unit_ms 100) (SET dash_ms (* unit_ms 3)) (DEFUN DOT () (DO (WRITE D led_pin 1) (TONE buz_pin freq unit_ms) (WRITE D led_pin 0) (DELAY unit_ms) ) ) (DEFUN DASH () (DO (WRITE D led_pin 1) (TONE buz_pin freq dash_ms) (WRITE D led_pin 0) (DELAY unit_ms) ) ) (DEFUN LETTER_S () (DO (DOT) (DOT) (DOT) (DELAY (* unit_ms 2)) )) (DEFUN LETTER_O () (DO (DASH) (DASH) (DASH) (DELAY (* unit_ms 2)) )) (SET broadcasts 3) (WHILE (> broadcasts 0) (DO (LOG " -> Transmitting: S O S") (LETTER_S) (LETTER_O) (LETTER_S) (DELAY (* unit_ms 4)) (SET broadcasts (- broadcasts 1)) ) ) (LOG ">> Beacon transmission complete.")
View code
thermostat.hil
Digital thermostat with hysteresis control
intermediate
LISP
(SET temp_sensor 0) (SET fan_pin 13) (SET buzzer_pin 11) (SET fan_state 0) (SET high_threshold 28) (SET low_threshold 26) (LOG "=== Digital Thermostat ===") (LOG "High: " high_threshold "C | Low: " low_threshold "C") (WHILE 1 (DO (SET temp (LM35 temp_sensor)) (IF (AND (>= temp high_threshold) (== fan_state 0)) (DO (WRITE D fan_pin 1) (SET fan_state 1) (LOG ">>> HIGH TEMP: " temp "C — FAN ON") (TONE buzzer_pin 500 100) ) ) (IF (AND (<= temp low_threshold) (== fan_state 1)) (DO (WRITE D fan_pin 0) (SET fan_state 0) (LOG "<<< LOW TEMP: " temp "C — FAN OFF") ) ) (DELAY 1000) ) )
View code
state-machine.hil
Traffic light controller with COND state machine
advanced
LISP
(SET red 13) (SET yellow 12) (SET green 11) (SET state 0) (SET timer 0) (LOG "=== Traffic Light Controller ===") (WRITE D red 0) (WRITE D yellow 0) (WRITE D green 0) (SET cycles 0) (WHILE (< cycles 5) (DO (COND ((== state 0) (DO (WRITE D green 1) (WRITE D yellow 0) (WRITE D red 0) (LOG "GREEN — Go (" timer "s)") (IF (>= timer 3) (DO (SET state 1) (SET timer 0))) (DELAY 1000) (SET timer (+ timer 1)) ) ) ((== state 1) (DO (WRITE D green 0) (WRITE D yellow 1) (WRITE D red 0) (LOG "YELLOW — Caution (" timer "s)") (IF (>= timer 1) (DO (SET state 2) (SET timer 0))) (DELAY 1000) (SET timer (+ timer 1)) ) ) ((== state 2) (DO (WRITE D green 0) (WRITE D yellow 0) (WRITE D red 1) (LOG "RED — Stop (" timer "s)") (IF (>= timer 3) (DO (SET state 0) (SET timer 0) (SET cycles (+ cycles 1)))) (DELAY 1000) (SET timer (+ timer 1)) ) ) ) ) ) (WRITE D red 0) (WRITE D yellow 0) (WRITE D green 0) (LOG "Shut down after " cycles " cycles.")
View code
pid-controller.hil
PID temperature controller with closed-loop feedback
advanced
LISP
(SET sensor_pin 0) (SET heater_pin 9) (SET setpoint 25) (LOG "=== PID Temperature Controller ===") (LOG "Target: " setpoint "C") (PID-INIT 0 heater_pin sensor_pin 2.0 0.5 0.1) (CLOSED-LOOP 0 setpoint) (LOG "PID engaged.") (SET cycles 30) (WHILE (> cycles 0) (DO (SET temp (LM35 sensor_pin)) (SET error (ABS (- setpoint temp))) (LOG "Temp: " temp "C | Error: " error "C") (IF (> error 5) (LOG "WARNING: Large error — PID may need tuning!") ) (DELAY 1000) (SET cycles (- cycles 1)) ) ) (OPEN-LOOP 0 0) (LOG "PID disengaged. Heater OFF.")
View code
parking-sensor.hil
Ultrasonic parking sensor with progressive buzzer
advanced
LISP
(SET trig 7) (SET echo 8) (SET buz 11) (SET led 13) (SET safe_dist 50) (SET warn_dist 30) (SET danger_dist 10) (LOG "=== Parking Sensor ===") (WHILE 1 (DO (SET dist (ULTRASONIC trig echo)) (IF (> dist safe_dist) (WRITE D led 0) (IF (> dist warn_dist) (DO (WRITE D led 1) (TONE buz (MAP dist warn_dist safe_dist 800 400) 100) (LOG "WARN: " dist "cm") ) (IF (> dist danger_dist) (DO (WRITE D led 1) (TONE buz (MAP dist danger_dist warn_dist 2000 1000) 50) (LOG "DANGER: " dist "cm") ) (DO (WRITE D led 1) (TONE buz 3000 200) (LOG "CRITICAL: " dist "cm — STOP!") ) ) ) ) (DELAY 100) ) )
View code
robot-arm.hil
3-DOF robot arm with pick and place
advanced
LISP
(SET shoulder 9) (SET elbow 10) (SET gripper 11) (LOG "=== Robot Arm Demo ===") (SERVO shoulder 90) (SERVO elbow 90) (SERVO gripper 90) (DELAY 500) (DEFUN GRIP_CLOSE () (DO (LOG "Gripper: closing") (FOR angle 90 10 (SERVO gripper angle)) ) ) (DEFUN GRIP_OPEN () (DO (LOG "Gripper: opening") (FOR angle 10 90 (SERVO gripper angle)) ) ) (LOG "--- Pick and Place ---") (LOG "Reaching forward...") (FOR angle 90 30 (DO (SERVO shoulder angle) (DELAY 15))) (LOG "Lowering arm...") (FOR angle 90 140 (DO (SERVO elbow angle) (DELAY 15))) (GRIP_CLOSE) (DELAY 300) (LOG "Lifting...") (FOR angle 140 90 (DO (SERVO elbow angle) (DELAY 15))) (LOG "Rotating...") (FOR angle 30 150 (DO (SERVO shoulder angle) (DELAY 15))) (FOR angle 90 140 (DO (SERVO elbow angle) (DELAY 15))) (GRIP_OPEN) (DELAY 300) (LOG "Returning home...") (FOR angle 140 90 (DO (SERVO elbow angle) (DELAY 15))) (FOR angle 150 90 (DO (SERVO shoulder angle) (DELAY 15))) (LOG "Pick and Place complete!")
View code
data-logger.hil
SD card data logger with LCD display
advanced
LISP
(SET sensor_pin 0) (SET sd_cs 10) (SET log_interval 5000) (SET alarm_temp 30) (LCD-INIT 7 6 5 4 3 2) (LCD-CLEAR) (LCD-PRINT 0 0 "Data Logger v1.0") (LOG "=== Data Logger ===") (SD-WRITE sd_cs "timestamp,temp_c,status") (DELAY 100) (SET entry 0) (WHILE 1 (DO (SET temp (LM35 sensor_pin)) (SET ts (* entry 5)) (SET status "NORMAL") (IF (>= temp alarm_temp) (SET status "ALERT")) (SD-WRITE sd_cs ts "," temp "," status) (LCD-CLEAR) (LCD-PRINT 0 0 "Temp: " temp " C") (LCD-PRINT 1 0 "Log #" entry " " status) (LOG "[" ts "ms] " temp "C [" status "]") (IF (== status "ALERT") (DO (TONE 11 1000 200) (WRITE D 13 1) (DELAY 200) (WRITE D 13 0)) ) (SET entry (+ entry 1)) (DELAY log_interval) ) )
View code
menu-system.hil
Interactive serial menu with COND dispatch
advanced
LISP
(SET led 13) (SET buz 11) (LOG "========================================") (LOG " HIL-OS Interactive Menu System") (LOG "========================================") (LOG "") (LOG " 1. Blink LED (fast)") (LOG " 2. Blink LED (slow)") (LOG " 3. Play scale") (LOG " 4. Sensor read") (LOG " 5. Math demo") (LOG " 6. Rainbow LEDs") (SET selection 4) (COND ((== selection 1) (DO (LOG ">> Fast blink") (BLINK led 10 100))) ((== selection 2) (DO (LOG ">> Slow blink") (BLINK led 5 500))) ((== selection 3) (DO (LOG ">> Playing scale") (TONE buz 262 200) (DELAY 250) (TONE buz 294 200) (DELAY 250) (TONE buz 330 200) (DELAY 250) (TONE buz 349 200) (DELAY 250) (TONE buz 392 200) (DELAY 250) (TONE buz 440 400) ) ) ((== selection 4) (DO (LOG ">> Sensor readings:") (LOG " LM35: " (LM35 0) "C") (LOG " LDR: " (READ A 0)) (LOG " Ultra: " (ULTRASONIC 7 8) "cm") ) ) ((== selection 5) (DO (LOG ">> Math operations:") (LOG " 17 + 5 = " (+ 17 5)) (LOG " 17 - 5 = " (- 17 5)) (LOG " 17 * 5 = " (* 17 5)) (LOG " 17 / 5 = " (/ 17 5)) (LOG " 17 % 5 = " (% 17 5)) (LOG " MAX(17,5) = " (MAX 17 5)) (LOG " MIN(17,5) = " (MIN 17 5)) (LOG " ABS(-42) = " (ABS -42)) ) ) ((== selection 6) (DO (LOG ">> Rainbow LED demo") (SET i 0) (WHILE (< i 5) (DO (WRITE P led (MAP i 0 4 50 255)) (DELAY 300) (SET i (+ i 1)) ) ) (WRITE P led 0) ) ) (1 (LOG ">> Invalid selection!")) ) (LOG "Menu demo complete.")
View code
fibonacci-music.hil
Music generated from Fibonacci sequence
advanced
LISP
(LOG ">> Initiating Golden Ratio Soundscape...") (DEFUN FIB (n) (IF (<= n 2) n (+ (FIB (- n 1)) (FIB (- n 2)))) ) (DEFUN FIB_TO_FREQ (n) (COND ((== n 1) 262) ((== n 2) 294) ((== n 3) 330) ((== n 5) 349) ((== n 8) 392) ((== n 13) 440) ((== n 21) 494) ((== n 34) 523) (1 587) ) ) (SET n 1) (WHILE (<= n 10) (DO (SET fib_val (FIB n)) (SET freq (FIB_TO_FREQ fib_val)) (SET duration (MIN (* fib_val 50) 500)) (LOG "Note " n ": fib=" fib_val " freq=" freq "Hz") (TONE 11 freq duration) (DELAY (* duration 2)) (SET n (+ n 1)) ) ) (LOG ">> Golden Ratio Soundscape complete.")
View code
interrupt-driven-system.hil
Hardware interrupts and event-driven system
advanced
LISP
(LOG "=== Interrupt-Driven System ===") (DEFUN ESTOP () (DO (LOG "!!! E-STOP TRIGGERED !!!") (WRITE D 13 0) (WRITE D 9 0) (TONE 11 2000 500) (EXIT) ) ) (DEFUN LIGHT_SHIFT () (DO (LOG "Ambient light shift detected") (TONE 11 440 100) ) ) (ON-EVENT 2 2 "ESTOP") (ON-CHANGE A 0 150 "LIGHT_SHIFT") (LOG "Interrupts armed.") (SET running 1) (SET cycle 0) (WHILE (> running 0) (DO (SET cycle (+ cycle 1)) (WRITE D 13 1) (DELAY 500) (WRITE D 13 0) (DELAY 500) (LOG "Cycle " cycle " — running") (IF (>= cycle 20) (DO (LOG "Auto-shutdown.") (SET running 0)) ) ) ) (LOG "System halted.")
View code

Advanced IoT & Hardware Examples

INTERMEDIATE

Temperature Alert System

Click to expand

Reads DHT22 sensor, lights NeoPixel red/orange/green based on temperature, beeps on overheating.

Packages needed: hilos-sensors, hilos-displays

HIL LISP
(use-package hilos-sensors) (use-package hilos-displays) (defun setup () (serial-begin 9600) (dht-begin 2 DHT11) (neopixel-setup 6 1) (pin-mode 13 OUTPUT)) (defun loop () (let ((temp (dht-read-temp)) (hum (dht-read-humidity))) (serial-print "Temp: ") (serial-print temp) (serial-print "C Hum: ") (serial-print hum) (serial-println "%") (cond ((> temp 30) (neopixel-set 0 255 0 0) (tone 8 1000 200)) ((> temp 25) (neopixel-set 0 255 165 0) (tone 8 500 200)) (t (neopixel-set 0 0 255 0))) (neopixel-show) (wait 2000)))

Deploy: hil-cli compile example.hil && hil-cli upload

INTERMEDIATE

IoT Sensor Network Node

Click to expand

Connects to WiFi and the HIL IoT network, publishes temperature and humidity every 5 seconds. Other devices can subscribe to these topics.

Packages needed: hilos-sensors, hilos-iot

HIL LISP
(use-package hilos-sensors) (use-package hilos-iot) (defun setup () (serial-begin 115200) (iot-connect "MyWiFi" "password123" "wss://hilp.mediahubnetwork.net/ws") (dht-begin 2 DHT11) (iot-heartbeat 30)) (defun loop () (let ((temp (dht-read-temp)) (hum (dht-read-humidity))) (iot-publish "temperature" temp) (iot-publish "humidity" hum) (iot-publish-json "sensor" "temp" (to-string temp) "hum" (to-string hum)) (serial-print "Published: temp=") (serial-print temp) (serial-print " hum=") (serial-println hum)) (iot-loop) (wait 5000))

Deploy: hil-cli compile example.hil && hil-cli upload

INTERMEDIATE

Servo Robot Arm

Click to expand

Controls a 4-DOF robot arm with servos. Sweeps through preset positions.

Packages needed: hilos-motors

HIL LISP
(use-package hilos-motors) (defvar *base-servo* 3) (defvar *shoulder* 5) (defvar *elbow* 6) (defvar *gripper* 9) (defun setup () (serial-begin 9600) (servo-attach *base-servo*) (servo-attach *shoulder*) (servo-attach *elbow*) (servo-attach *gripper*) (serial-println "Robot arm ready")) (defun move-to (base shoulder elbow gripper) (servo-write *base-servo* base) (servo-write *shoulder* shoulder) (servo-write *elbow* elbow) (servo-write *gripper* gripper)) (defun loop () (move-to 90 90 90 90) (wait 1000) (move-to 45 120 60 30) (wait 1000) (move-to 135 60 120 150) (wait 1000) (move-to 90 90 90 90) (wait 2000))

Deploy: hil-cli compile example.hil && hil-cli upload

INTERMEDIATE

Smart Light Controller with Button

Click to expand

Button cycles through 4 modes: off, white, warm, rainbow. Uses debounce logic.

Packages needed: hilos-displays

HIL LISP
(use-package hilos-displays) (defvar *neo-pin* 6) (defvar *btn-pin* 7) (defvar *mode* 0) (defvar *last-press* 0) (defun setup () (pin-mode *btn-pin* INPUT_PULLUP) (neopixel-setup *neo-pin* 12) (serial-begin 9600)) (defun set-all (r g b) (dotimes (i 12) (neopixel-set i r g b)) (neopixel-show)) (defun rainbow () (dotimes (i 12) (neopixel-set i (wheel (* i 21)))) (neopixel-show)) (defun loop () (let ((now (millis))) (when (and (= (digital-read *btn-pin*) LOW) (> (- now *last-press*) 300)) (setq *last-press* now) (setq *mode* (mod (+ *mode* 1) 4)) (serial-print "Mode: ") (serial-println *mode*) (cond ((= *mode* 0) (set-all 0 0 0)) ((= *mode* 1) (set-all 255 255 255)) ((= *mode* 2) (set-all 255 100 0)) ((= *mode* 3) (rainbow))))))

Deploy: hil-cli compile example.hil && hil-cli upload

INTERMEDIATE

Ultrasonic Distance Alarm

Click to expand

Measures distance with HC-SR04, beeps faster as objects get closer. LED indicates danger zone.

Packages needed: hilos-sensors

HIL LISP
(use-package hilos-sensors) (defvar *trig* 9) (defvar *echo* 10) (defvar *buzzer* 11) (defvar *led* 13) (defun setup () (serial-begin 9600) (pin-mode *buzzer* OUTPUT) (pin-mode *led* OUTPUT)) (defun loop () (let ((dist (hcsr04-cm *trig* *echo*))) (serial-print "Distance: ") (serial-print dist) (serial-println " cm") (cond ((< dist 10) (digital-write *led* HIGH) (tone *buzzer* 2000 100)) ((< dist 30) (digital-write *led* HIGH) (tone *buzzer* 1000 100)) (t (digital-write *led* LOW) (no-tone *buzzer*))) (wait 100)))

Deploy: hil-cli compile example.hil && hil-cli upload

INTERMEDIATE

EEPROM Data Logger

Click to expand

Logs temperature and humidity to EEPROM. Data persists after power off.

Packages needed: hilos-sensors

HIL LISP
(use-package hilos-sensors) (defvar *log-addr* 0) (defun setup () (serial-begin 9600) (dht-begin 2 DHT11) (serial-println "Data Logger Ready") (serial-println "Logging every 10 seconds...")) (defun log-data (temp hum) (eeprom-write *log-addr* (floor temp)) (setq *log-addr* (+ *log-addr* 1)) (eeprom-write *log-addr* (floor hum)) (setq *log-addr* (+ *log-addr* 1)) (serial-print "Logged at address ") (serial-println *log-addr*)) (defun loop () (let ((temp (dht-read-temp)) (hum (dht-read-humidity))) (serial-print "T=") (serial-print temp) (serial-print " H=") (serial-println hum) (log-data temp hum)) (wait 10000))

Deploy: hil-cli compile example.hil && hil-cli upload

Game-engine style bulk transfer (LZ4)

compression-demo.hil
LZ4-style compressed batches — move large data over serial up to 18x faster
advanced
LISP
; Game-engine inspired bulk data transfer. ; Inspired by how Unreal (Oodle/ZSTD) and Unity (LZ4) ; compress streaming data. Fully optional — plain ; commands work exactly as before. (LOG "=== HIL Compression Demo ===") ; ───────────────────────────────────────────── ; 1. COMPRESSED BATCH — the big win. ; BATCH-BEGIN normally fires commands one-by-one. ; With compression enabled, the whole batch is ; buffered, LZ4-compressed, and sent as tiny frames ; (~7x less serial traffic on real hardware). ; ───────────────────────────────────────────── (COMPRESS-START LZ4) ; optional arg: LZ4 | DELTA | RLE (BATCH-BEGIN) (FOR i 0 49 (DO (WRITE D 13 1) (DELAY 5) (WRITE D 13 0) (WRITE D 12 i) ) ) (BATCH-END) (COMPRESS-STATS) ; show raw vs wire bytes + ratio (COMPRESS-RESET) ; zero the counters (COMPRESS-END) ; ───────────────────────────────────────────── ; 2. MANUAL BULK SEND — push any payload. ; Data too small to benefit is automatically ; sent uncompressed instead (safe fallback). ; ───────────────────────────────────────────── (COMPRESS-START RLE) ; RLE loves repeated values (COMPRESS-SEND "NEO-SET 6 255 0 0\nNEO-SET 6 0 255 0\n") (COMPRESS-STATS) (COMPRESS-END) ; ───────────────────────────────────────────── ; 3. STREAM MODE — game-netcode style frames. ; Telemetry appends to a frame buffer that ; flushes every N ms, compressed per frame. ; ───────────────────────────────────────────── (STREAM-START 50 256) ; flush every 50ms / 256-byte frames (FOR i 0 99 (DO (READ A 0) (STREAM-FLUSH) ) ) (STREAM-STOP) (LOG "=== Compression demo complete ===")
View code
compression-batch.hil
Minimal example: compress a 100-command batch into a single frame
intermediate
LISP
; The minimal compressed batch: ; 100 WRITE commands → ~2 wire frames. (COMPRESS-START LZ4) (BATCH-BEGIN) (FOR i 0 99 (WRITE D 13 i) ) (BATCH-END) (COMPRESS-STATS) ; [COMPRESS] raw=1178 comp=463 ratio=39% sent=5 ; That batch used ~1178 serial bytes before; ; now it takes ~463. At 9600 baud: 1.2s → 480ms. (COMPRESS-END)
View code