; Define a function that converts Celsius to Fahrenheit
(DEFUN C_TO_F (c)
(+ (/ (* c 9) 5) 32)
)
; Use it
(LOG "0C = " (C_TO_F 0) "F")
(LOG "100C = " (C_TO_F 100) "F")
(LOG "25C = " (C_TO_F 25) "F")
; Function with multiple parameters
(DEFUN CLAMP (val low high)
(IF (< val low)
low
(IF (> val high) high val)
)
)
; Test it
(LOG "Clamp 50 to [0,25]: " (CLAMP 50 0 25))
(LOG "Clamp 10 to [0,25]: " (CLAMP 10 0 25))
(LOG "Clamp 15 to [0,25]: " (CLAMP 15 0 25))
Tutorial 5: Blink an LED (Real Hardware)
Run on real Arduino hardware.
1
Wire it up
Connect an LED + 220Ω resistor to pin 13 on your Arduino.
blink.hil
(SET led 13)
(SET count 10)
(WHILE (> count 0)
(DO
(WRITE D led 1)
(LOG "LED ON")
(DELAY 500)
(WRITE D led 0)
(LOG "LED OFF")
(DELAY 500)
(SET count (- count 1))
)
)
(LOG "Done blinking!")
2
Connect and run
hil-cli ports
hil-cli run blink.hil -port COM3
Tutorial 6: Read a Sensor
Read analog data and make decisions.
sensor.hil
; LM35 temperature sensor on A0
; LED on D13, Buzzer on D11
(SET sensor 0)
(SET led 13)
(SET buz 11)
(SET alarm_temp 30)
(LOG "Temperature Monitor")
(LOG "Alarm threshold: " alarm_temp "C")
(WHILE 1
(DO
; Read temperature
(SET temp (LM35 sensor))
(LOG "Temp: " temp "C")
; Map temperature to LED brightness
(SET brightness (CONSTRAIN (MAP temp 0 50 0 255) 0 255))
(WRITE P led brightness)
; Alarm if too hot
(IF (>= temp alarm_temp)
(DO
(LOG "!! OVERHEAT !!")
(TONE buz 1000 200)
)
)
(DELAY 1000)
)
)
Tutorial 7: State Machines
Build complex behavior with COND and states.
traffic-light.hil
; Traffic Light Controller
; Red=D13, Yellow=D12, Green=D11
(SET red 13)
(SET yellow 12)
(SET green 11)
(SET state 0)
(SET timer 0)
; All LEDs off
(WRITE D red 0)
(WRITE D yellow 0)
(WRITE D green 0)
; Run 3 cycles
(SET cycles 0)
(WHILE (< cycles 3)
(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))
)
)
)
)
)
(LOG "Done! " cycles " cycles complete.")
The CRUMB simulator renders your circuit with all connected components, LEDs, sensors, servos, displays, and runs the transpiled Arduino C code in real-time.
Tip: Install CRUMB from the Microsoft Store or the official website. It's free for personal use.
Next Steps
◇ Language Reference
Complete list of all operators, functions, and syntax.