Skip to content

1. Your first program: a traffic light

In this tutorial you open an example program in VS Code: a traffic light for an ESP32, with three LEDs and a button to cross. You run it on a board, or with no board in Velxio, which simulates the ESP32 and the parts wired to it in your web browser. Then you read the program, change it and run it again.

Do Start here: VS Code first: it installs MicroGoose and sets up VS Code.

Get the example

MicroGoose comes with examples. This one makes a folder lights holding the program and the circuit it expects:

microgoose new lights --example velxio/traffic
created lights/main.goose
editor:  wrote lights/.vscode/settings.json
editor:  wrote lights/.vscode/tasks.json
next:    microgoose run lights/main.goose --target esp32   (with no board, in the Velxio simulator: microgoose velxio lights/main.goose)
         or open the folder lights in VS Code: Ctrl+Shift+B runs it
         (the Goose extension, once: code --install-extension ...)

The folder holds:

  • main.goose: the program, in the Goose language.
  • diagram.json: the circuit, which says which part is wired to which pin.
  • .vscode: what VS Code needs to check and run the program.

The example is for the ESP32 DevKit V1, the board Velxio simulates. On an ESP32-S3-DevKitC-1, add --target s3 to the command above.

Open it in VS Code

code lights

Then click main.goose in the Explorer on the left.

VS Code with the traffic light example open: the Explorer shows lights with .vscode, diagram.json and main.goose, and the editor shows the program

The traffic light, open in VS Code.

Run it

Wire three LEDs and a button on a breadboard, as diagram.json does:

Part ESP32 DevKit V1 ESP32-S3-DevKitC-1
red LED, through a 220 Ω resistor GPIO25 GPIO5
yellow LED, through a 220 Ω resistor GPIO26 GPIO6
green LED, through a 220 Ω resistor GPIO27 GPIO7
button, its other side to GND GPIO14 GPIO15

Each LED's long leg goes toward the pin, through the resistor, and its short leg to GND. On the ESP32-S3, change the four let lines at the top of the program to the S3's pins.

Plug the board in with a USB cable that carries data, and press Ctrl+Shift+B. MicroGoose builds the firmware, flashes it to the board, and shows what it prints for 30 seconds in the terminal below the program. The first time, it downloads the ESP32 toolchain first, which takes several minutes. Later builds take seconds.

VS Code after Ctrl+Shift+B: the traffic light program above, and the terminal below showing its output from the ESP32: cycle 1: green, yellow, red: walk
Ctrl+Shift+B: built, flashed, and running on an ESP32.

The green LED lights, then yellow, then red, and the terminal shows:

MicroGoose traffic light: press the button to cross
cycle 1: green
  yellow
  red: walk
  red and yellow
cycle 2: green

Press the button while the light is green. Green ends early, and the terminal shows button pressed after how many milliseconds green had been on, then yellow. The board keeps running after the 30 seconds. To see its output again, choose Terminal > Run Task... > MicroGoose: monitor the board, which restarts it.

Choose Terminal > Run Task... > MicroGoose: files for Velxio (velxio.dev), or in a terminal:

microgoose velxio lights/main.goose

MicroGoose turns the program into C, compiles it with Espressif's ESP-IDF for the ESP32, and writes the files Velxio loads. The first time, it downloads the ESP32 toolchain first, which takes several minutes. Later builds take seconds. It ends like this:

wrote velxio/lights-esp32.bin
wrote velxio/lights-circuit.zip
in Velxio:
  1. open https://velxio.dev/editor
  2. File > Import project: lights-circuit.zip (the board and its circuit)
  3. File > Upload firmware: lights-esp32.bin
  4. press Run; Serial shows print()

lights-esp32.bin is the firmware: everything a real ESP32's flash chip would hold. lights-circuit.zip is the circuit, as a Velxio project. Both are in a new folder, velxio, inside the folder the build ran in.

  1. Open velxio.dev/editor. If a box opens (what's new, or Start a new project), close it.
  2. Choose File > Import project and pick velxio/lights-circuit.zip. An ESP32 board appears, wired to three LEDs and a blue button. The code pane on the left shows an Arduino sketch: ignore it. The firmware you upload next takes its place.
  3. Choose File > Upload firmware and pick velxio/lights-esp32.bin. Velxio may say the firmware "uses WiFi but does not name any network". You can ignore that: this program never turns WiFi on.
  4. Press Run (the green triangle in the toolbar).
  5. Open the Serial monitor (the screen icon at the top right) to see what the program prints.

The traffic light running in Velxio: an ESP32 wired to red, yellow and green LEDs and a button, with the serial monitor showing the program's output
The traffic light in Velxio.

The green LED lights, then yellow, then red. After the chip's own start-up messages, the Serial monitor shows:

MicroGoose traffic light: press the button to cross
cycle 1: green
  yellow
  red: walk
  red and yellow
cycle 2: green

Click the blue cross button while the light is green. Green ends early:

cycle 3: green
  button pressed after 7701 ms: changing
  yellow

The number of milliseconds is how long green had been on when you clicked. Velxio's clock runs at its own pace, a little faster or slower than your watch, so the lights won't keep exact seconds there. On a board they do.

Read the program

main.goose is open in VS Code. Its main parts:

import mcu;

let RED = 25;
let YELLOW = 26;
let GREEN = 27;
let BUTTON = 14;

import mcu; brings in the functions for the board's hardware. The let lines name the pins: the red LED is wired to the ESP32's pin 25 (GPIO25), and so on.

fn lights(r: i32, y: i32, g: i32) {
    pin_write(RED, r);
    pin_write(YELLOW, y);
    pin_write(GREEN, g);
}

A function of your own: lights(0, 0, 1) turns red and yellow off and green on. pin_write(pin, 1) puts 3.3 volts on the pin, which lights the LED wired to it; pin_write(pin, 0) puts 0 volts on it.

To see what a function takes, hover over it in VS Code. F12 opens the place it is defined: for pin_write, that is MicroGoose's mcu.goose, which lists all the board's functions.

VS Code showing a tooltip over pin_write: extern fn pin_write(pin: i32, level: i32)

Hovering over pin_write.
fn main() {
    pin_output(RED);
    pin_output(YELLOW);
    pin_output(GREEN);
    pin_input(BUTTON, PULL_UP);
    pin_watch(BUTTON, FALLING, 20000);     // 20 ms of contact bounce ignored

main() is where the program starts. It makes the three LED pins outputs, and the button's pin an input. pin_watch tells the chip to note each press of the button as an event, which the program can wait for.

    while true {
        cycle++;
        while event_wait(0) >= 0 {}        // presses while it was red don't count
        lights(0, 0, 1);
        print("cycle ", cycle, ": green");
        let t0 = millis();
        if wait_for_button(8000) {
            print("  button pressed after ", millis() - t0, " ms: changing");
            delay_ms(800);
        }
        lights(0, 1, 0);
        print("  yellow");
        delay_ms(1500);

while true repeats forever. print writes to the serial port, which Velxio shows in its Serial monitor. delay_ms(1500) waits 1.5 seconds. wait_for_button(8000), a function further up in the file, waits up to 8 seconds for a button press, and says whether one came.

Change it

Make green shorter. In lights/main.goose, change the 8 seconds to 3:

        if wait_for_button(3000) {

Then make the yellow light blink three times instead of staying on. Replace these three lines:

        lights(0, 1, 0);
        print("  yellow");
        delay_ms(1500);

with a loop:

        print("  yellow, blinking");
        for i in 3 {
            lights(0, 1, 0);
            delay_ms(250);
            lights(0, 0, 0);
            delay_ms(250);
        }

for i in 3 runs its block three times, with i set to 0, 1 and 2.

Press Ctrl+Shift+B again: VS Code saves the file, and the build takes seconds this time. With no board, run the Velxio task again; in Velxio, press Stop (the red square), upload the new velxio/lights-esp32.bin with File > Upload firmware, and press Run. The circuit stays as it was, so you don't import it again.

A mistake shows before you build: VS Code underlines it as you type and lists it in Problems (Ctrl+Shift+M). For example, leaving out the ; after the first delay_ms(250) gives:

';' expected after expression, found 'lights'

on the line after it, lights(0, 0, 0);: Goose noticed the missing ; when the next line started. A misspelled name looks like this:

VS Code underlining a misspelled function name, pin_writ, with the Problems panel below saying unknown function: pin_writ, at line 19

A misspelled pin_write, underlined, and listed in Problems.

Fix it, and press Ctrl+Shift+B again.

What you did

  • Opened an example in VS Code, and ran it on a board with one key, or in a simulator, with LEDs and a button you could see and press.
  • Read a Goose program, and looked its functions up.
  • Changed it, saw a mistake before building, and ran it again in seconds.

Next, tutorial 2 goes through the Goose language itself.