Skip to content

Getting Started

West edited this page Jun 7, 2026 · 2 revisions

This page walks through the process of creating a simple Mosscap sketch from scratch.

Note: This page assumes that you have installed the extension and have played around with the demo for a bit.

Setup

You'll want a specific location to store all of your sketches on your computer. A good place for this might be your Desktop or Documents folder. Either way, you should create a folder to hold all of your sketches. You can call this folder whatever you want, but it may be smart to name it something related to Mosscap, so that it's easy to find later.

Next, you'll want to create a folder within that folder to hold your first sketch, call this folder Getting-Started or something similar. Open this folder in VS Code.

A sketch consists of a single source (.cpp) file, and any number of header files. In Mosscap, this file is called ardMain.cpp and is what contains the familiar setup() and loop() functions that you would find in any other Arduino sketch. Create this file, and add the following skeleton code:

#include <mosscap/defaultTank.h>

void setup() {

}

void loop() {

}

Next, run the extension command to initialize the environment, and make those red squiggles disappear:

Hit Ctrl/Cmd+Shift+P and type: Mosscap: Initialize Environment. Then, hit enter.

Finally, start the simulator, just to make sure everything is working:

Hit Ctrl/Cmd+Shift+P and type: Mosscap: Start Simulator. Then, hit enter.

What Does #include <mosscap/defaultTank.h> Do?

Great question! This a feature that provides a preconfigured simulation environment to make setup easy. It wires the robot's two motors, and a button, accordingly:

  • The left motor is put on pins 11, 10, 12
  • The right motor is put on pins 21, 20, 22
  • The button is wired to pin 1

Write Some Code

Our goal here is to write a simple sketch that will use the motors to drive the robot forward when the button is pressed.

If you are already comfortable writing Arduino code, give this a try on your own. If not, you can follow the steps below.

Step 1: Create Motors

To be able to talk to the motors on our robot, we need to represent them somehow in code. Mosscap provides a Motor library that lets us do just that. We can use it by adding the #include <Motor.h> line at the top of our sketch. This allows us to create motor objects that are bound to pins on the Arduino:

#include <Motor.h>

Motor left(11, 10, 12);
Motor right(21, 20, 22);

Now, we have objects that we can interact with in the code. For example, we could make the robot move back and forth using the following code:

void loop() {
    // Tell both motors to run forward
    left.run(200);
    right.run(200);

    // Wait for half a second (500 milliseconds)
    delay(500);

    // Tell both motors to run backwards
    left.run(-200);
    right.run(-200);

    // Wait for another half second
    delay(500);
}

If you run the simulator, should see our little robot moving back and forth for all eternity. We're almost there!

Step 2: Add Button

We already know that the button is wired to pin 1 in the simulator. From our sketch, we need to set the pin mode of pin 1 to be an input. This will allow us to read its value (either 1 or 0, pressed or not pressed) later on:

void setup() {
    pinMode(1, INPUT);
}

Now, every time the code loop()s we can check if the button is pressed. If it is, we should run the motors. If it isn't, then we should stop the motors:

void loop() {
    // If the button is pressed (i.e. digitalRead(1) == true)
    if (digitalRead(1)) {
        // Run the motors forward
        left.run(200);
        right.run(200);
    } else {
        // If the button is not pressed, stop the motors
        left.run(0);
        right.run(0);
    }
}

Done

Congratulations! You've completed the first exercise and successfully created your first Mosscap sketch. As a quick review, we:

  1. Created a sketch and set up our environment
  2. Added motors to our sketch to allow us to move the robot
  3. Added the button to our sketch to allow us to move the robot in response to user input

For reference, the complete code for this exercise can be found below:

#include <mosscap/defaultTank.h>

#include <Motor.h>

Motor left(11, 10, 12);
Motor right(21, 20, 22);

void setup() {
    pinMode(1, INPUT);
}

void loop() {
    // If the button is pressed (i.e. digitalRead(1) == true)
    if (digitalRead(1)) {
        // Run the motors forward
        left.run(200);
        right.run(200);
    } else {
        // If the button is not pressed, stop the motors
        left.run(0);
        right.run(0);
    }
}

Clone this wiki locally