Volt Docs

Language

Types and variables

Numbers, text, true and false, durations, your own enums and structs, and how values are declared and converted.

Types

TypeHolds
intA whole number, 32-bit: 42, -7, 0x27
floatA number with a fraction: 3.14, 0.5
booltrue or false
stringText: "hello"
byteA whole number from 0 to 255
DurationA length of time: 100us, 50ms, 2s, 10min, 1hour

Your own types are enums and structs, below.

Declaring values

let declares a value that never changes; var declares one that can. The type comes after a colon:

main.volt
enum Mode { idle, running, fault }

let limit: int = 100

var mode: Mode = Mode.idle

var ratio: float = 0.5

var armed: bool = false

setup {
    let half: int = int(float(limit) * ratio)
    log("boot", "half={half}")
    if mode == Mode.idle && !armed {
        mode = Mode.running
    }
}

Values declared outside any block are shared by the whole program, and always need a starting value. Values declared inside a block belong to that block.

Names can’t start with let, var or else: variance reads as var iance. Pick another name, like spread.

Converting between numbers

Volt never converts between int and float behind your back. Use float(n) and int(x); int drops the fraction:

main.volt
setup {
    let reading: int = 2048
    let volts: float = float(reading) * 3.3 / 4095.0
    let whole: int = int(volts)
    log("adc", "{volts}V, about {whole}V")
}

Durations

A duration is a number with a unit, written together: 500ms, not 500 ms. A plain number is never a duration, so every 500 is an error, and the fix offers 500ms or 500s.

UnitMeans
usmicroseconds
msmilliseconds
s, sec, second, secondsseconds
min, minute, minutesminutes
hr, hour, hourshours

Fractions work: 1.5s is 1500 milliseconds.

Text

Strings can include values with {…}, and + joins a string with anything:

main.volt
setup {
    let line: string = "set 42"
    if starts_with(line, "set ") {
        let n: int = to_int("42")
        log("cmd", "value " + n + " from " + len(line) + " chars")
        log("cmd", "or, the same: value {n}")
    }
}

len, contains, starts_with, ends_with, to_int and to_float are built in; compare strings with == and !=. A joined string is cut at 127 characters.

Enums

An enum is a fixed set of names:

main.volt
enum Light { red, amber, green }

var now: Light = Light.red

every 2s {
    match(now) {
        .red -> now = Light.green
        .green -> now = Light.amber
        .amber -> now = Light.red
    }
}

Structs

A struct groups values under one name:

main.volt
struct Reading {
    celsius: float
    humidity: float
}

setup {
    log("boot", "ready")
}

Operators

Arithmetic+ - * / %
Comparison== != < > <= >=
Logic&& || !
Assignment= += -= *= /=

/ between two ints is whole-number division: 7 / 2 is 3.