Arduino Tutorial

This document exists to give interested people a brief run-down of how we do our Arduino interfacing with Objects in Space. It is a simple tutorial. More details will be in place once the game is out.

Note that this is NOT intended to be a full Arduino tutorial – basic understanding of uploading Arduino sketches and tinkering is assumed in most cases.

The Arduino & Introduction

Arduino Mega
Arduino Mega

We have found that, while we’ve used Arduino Nanos and Arduino Unos in the past, Arduinos based on the Mega 2560 chip are the best for consoles, due to speed and number of pins – many consoles like the sort you’ll see later on use far more pins for input & output than an Uno can manage.

Plugging them into a computer, software can access them via a virtual serial port, which is how Objects in Space communicates with the Arduino.

The full protocol is available here, but we are also going to be releasing as open-source our Arduino module (written in C, obviously) to run your consoles without having to write all the nuanced stuff. The following tutorials, when we get into code, use this module to simplify the process.

You are, of course, quite welcome to write your own interface code or modify ours to make it more efficient.

LEDs

The simplest form of data we use is a simple boolean for a LED being on or off. These are also the most common.

At a minimum, a LED needs a resister, a positive lead on a standard digital/pwm Arduino port and the negative lead running, obviously, to ground. Here we have a 5mm blue LED and a 150 Ohm resister hooked up to port 5.

Arduino with LED
Arduino with LED
Buttons

The most common buttons we use are momentary (MOM) buttons but the module works with toggle-switches too, of the sort shown below.

Arduino with Switch
Arduino with Switch

No resister is required for a switch, but when initialising them it is important to enable pullup mode to avoid bounce errors with the switch. This is done as follows:

pinMode(pinNumber, INPUT_PULLUP);
Code

The OiS Arduino library is fairly simple, and is designed to abstract the code requirements for Objects in Space consoles down to some very simple work.

A very simple full Arduino console is shown below.

It has a single LED  and a single toggle switch. The LED (on pin 5) will turn on when the main engine is firing, the button (on pin 6) will turn on or off the main engine, depending on the switch’s position.

#include <ois.h>
#define NUM_OUTPUTS 1
#define NUM_INPUTS 1

const int engineBurningLED = 5;
const int burnEngineButton = 6;

#define OIS_CONSOLE_DESCRIPTION "Sample Console now active."

#define CHK_MAIN_ENGINE_BURNING "MAIN_ENGINE_BURNING"
#define CMD_BURN_MAIN_ENGINE "BURN_MAIN_ENGINE"
#define CMD_STOP_MAIN_ENGINE "STOP_MAIN_ENGINE"

void setupIO() {
  ois_setupBoolRequest(engineBurningLED, CHK_MAIN_ENGINE_BURNING);

  // When the burnEngineButton is turned off, the first command is executed. When turned on, the second command is executed
  ois_sendCommandToggleRequest(burnEngineButton, CMD_STOP_MAIN_ENGINE, CMD_BURN_ENGINE);
}

void custom_setup() {
  // Custom configuration goes here

}

void setup() {
  custom_setup();
  ois_setup();
  Serial.begin(9600);
}

void loop() {
  ois_mainLoop();
}
Custom / Non-Basic Inputs

In some cases, we wanted rather odd, specific functions to allow our boolean inputs or numerical inputs to affect something like, say, a fan, or a 7-segment LED display.

To do this, we create custom functions like this

void turnFanOn()
{
  digitalWrite(fanINAPin, LOW);
  digitalWrite(fanINBPin, HIGH);
  ois_sendDebugMessage("Fan going on");
}

void turnFanOff()
{
  digitalWrite(fanINAPin, HIGH);
  digitalWrite(fanINBPin, HIGH);
  ois_sendDebugMessage("Fan going off");
}

And when we call the functions, we set custom function calls for the data, as follows:

ois_setupBoolRequest(CHK_MODULE_REACTOR_FUNCTIONING, (void*)turnFanOn, (void*)turnFanOff);
Finished Consoles

Using this interface, a large number of buttons, LEDs and even some custom interface forms (like a fan on the power panel to spin when the reactor is running), you can make quite a complex ship control panel.

Our second effort at building ship control panels for our demo version of the game is  here.

All in, they took a few hundred dollars worth of parts and several weeks on and off to make.

The first road-test of the Mark II physical controllers
Objects in Space with consoles.